loglan_db.model_db.base_word   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 93
dl 0
loc 174
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A BaseWord.affixes() 0 10 1
A BaseWord.definitions() 0 3 1
A BaseWord.derivatives() 0 3 1
A BaseWord.query_derivatives() 0 29 1
A BaseWord.event_end() 0 3 1
A BaseWord.parents() 0 8 1
A BaseWord.authors() 0 3 1
A BaseWord.event_start() 0 3 1
A BaseWord.complexes() 0 10 1
A BaseWord.keys() 0 12 1
A BaseWord.type() 0 3 1
1
# -*- coding: utf-8 -*-
2
# pylint: disable=C0103
3 1
"""
4
This module contains a basic Word Model
5
"""
6
7 1
from __future__ import annotations
8
9 1
from flask_sqlalchemy import BaseQuery
10
11 1
from loglan_db import db
12 1
from loglan_db.model_db import t_name_words, \
13
    t_name_types, t_name_events
14 1
from loglan_db.model_db.base_author import BaseAuthor
15 1
from loglan_db.model_db.base_connect_tables import \
16
    t_connect_authors, t_connect_words, t_connect_keys
17 1
from loglan_db.model_db.base_definition import BaseDefinition
18 1
from loglan_db.model_db.base_event import BaseEvent
19 1
from loglan_db.model_db.base_key import BaseKey
20 1
from loglan_db.model_db.base_type import BaseType
21 1
from loglan_db.model_init import InitBase, DBBase
22
23 1
__pdoc__ = {
24
    'BaseWord.created': False, 'BaseWord.updated': False,
25
}
26
27
28 1
class BaseWord(db.Model, InitBase, DBBase):
29
    """BaseWord model"""
30 1
    __tablename__ = t_name_words
31
32 1
    id = db.Column(db.Integer, primary_key=True)
33
    """Word's internal ID number: Integer"""
34 1
    name = db.Column(db.String(64), nullable=False)
35 1
    origin = db.Column(db.String(128))
36 1
    origin_x = db.Column(db.String(64))
37 1
    match = db.Column(db.String(8))
38 1
    rank = db.Column(db.String(8))
39 1
    year = db.Column(db.Date)
40 1
    notes = db.Column(db.JSON)
41
42
    # Fields for legacy database compatibility
43 1
    id_old = db.Column(db.Integer, nullable=False)
44 1
    TID_old = db.Column(db.Integer)  # references
45
46
    # Relationships
47 1
    type_id = db.Column("type", db.ForeignKey(f'{t_name_types}.id'), nullable=False)
48 1
    _type: BaseType = db.relationship(
49
        BaseType.__name__, back_populates="_words")
50
51 1
    event_start_id = db.Column(
52
        "event_start", db.ForeignKey(f'{t_name_events}.id'), nullable=False)
53 1
    _event_start: BaseEvent = db.relationship(
54
        BaseEvent.__name__, foreign_keys=[event_start_id],
55
        back_populates="_appeared_words")
56
57 1
    event_end_id = db.Column("event_end", db.ForeignKey(f'{t_name_events}.id'))
58 1
    _event_end: BaseEvent = db.relationship(
59
        BaseEvent.__name__, foreign_keys=[event_end_id],
60
        back_populates="_deprecated_words")
61
62 1
    _authors: BaseQuery = db.relationship(
63
        BaseAuthor.__name__, secondary=t_connect_authors,
64
        back_populates="_contribution", lazy='dynamic', enable_typechecks=False)
65
66 1
    _definitions: BaseQuery = db.relationship(
67
        BaseDefinition.__name__, back_populates="_source_word", lazy='dynamic')
68
69
    # word's derivatives
70 1
    _derivatives = db.relationship(
71
        'BaseWord', secondary=t_connect_words,
72
        primaryjoin=(t_connect_words.c.parent_id == id),
73
        secondaryjoin=(t_connect_words.c.child_id == id),
74
        backref=db.backref('_parents', lazy='dynamic', enable_typechecks=False),
75
        lazy='dynamic', enable_typechecks=False)
76
77 1
    @property
78
    def type(self) -> BaseQuery:
79 1
        return self._type
80
81 1
    @property
82
    def event_start(self) -> BaseQuery:
83 1
        return self._event_start
84
85 1
    @property
86
    def event_end(self) -> BaseQuery:
87 1
        return self._event_end
88
89 1
    @property
90
    def authors(self) -> BaseQuery:
91 1
        return self._authors
92
93 1
    @property
94
    def definitions(self) -> BaseQuery:
95 1
        return self._definitions
96
97 1
    @property
98
    def derivatives(self) -> BaseQuery:
99 1
        return self._derivatives
100
101 1
    def query_derivatives(
102
            self, word_type: str = None, word_type_x: str = None,
103
            word_group: str = None) -> BaseQuery:
104
        """Query to get all derivatives of the word, depending on its parameters
105
106
        Args:
107
          word_type: str:  (Default value = None)
108
          E.g. "2-Cpx", "C-Prim", "LW"<hr>
109
110
          word_type_x: str:  (Default value = None)
111
          E.g. "Predicate", "Name", "Affix"<hr>
112
113
          word_group: str:  (Default value = None)
114
          E.g. "Cpx", "Prim", "Little"<hr>
115
116
        Returns:
117
            BaseQuery
118
        """
119
120 1
        type_values = [
121
                (BaseType.type, word_type),
122
                (BaseType.type_x, word_type_x),
123
                (BaseType.group, word_group), ]
124
125 1
        type_filters = [i[0] == i[1] for i in type_values if i[1]]
126
127 1
        return self._derivatives.join(BaseType)\
128
            .filter(self.id == t_connect_words.c.parent_id, *type_filters)\
129
            .order_by(type(self).name.asc())
130
131 1
    @property
132
    def parents(self) -> BaseQuery:
133
        """Query to get all parents for Complexes, Little words or Affixes
134
135
        Returns:
136
            BaseQuery
137
        """
138 1
        return self._parents
139
140 1
    @property
141
    def complexes(self) -> BaseQuery:
142
        """
143
        Get all word's complexes if exist
144
        Only primitives and Little Words have complexes.
145
146
        Returns:
147
            BaseQuery
148
        """
149 1
        return self.query_derivatives(word_group="Cpx")
150
151 1
    @property
152
    def affixes(self) -> BaseQuery:
153
        """
154
        Get all word's affixes if exist
155
        Only primitives have affixes.
156
157
        Returns:
158
            BaseQuery
159
        """
160 1
        return self.query_derivatives(word_type="Afx")
161
162 1
    @property
163
    def keys(self) -> BaseQuery:
164
        """Get all BaseKey object related to this BaseWord.
165
166
        Keep in mind that duplicated keys from related definitions
167
        will be counted with ```.count()``` but excluded from ```.all()``` request
168
169
        Returns:
170
            BaseQuery
171
        """
172 1
        return BaseKey.query.join(
173
            t_connect_keys, BaseDefinition, BaseWord).filter(BaseWord.id == self.id)
174