Conditions | 37 |
Total Lines | 168 |
Code Lines | 146 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like atramhasis.mappers.map_concept() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """ |
||
31 | def map_concept(concept, concept_json, skos_manager): |
||
32 | """ |
||
33 | Map a concept from json to the database. |
||
34 | |||
35 | :param skosprovider_sqlalchemy.models.Thing concept: A concept or |
||
36 | collection as known to the database. |
||
37 | :param dict concept_json: A dict representing the json sent to our REST |
||
38 | service. |
||
39 | :param skos_manager: A skos_manager to acces db operations |
||
40 | :returns: The :class:`skosprovider_sqlalchemy.models.Thing` enhanced |
||
41 | with the information from the json object. |
||
42 | """ |
||
43 | concept_json_type = concept_json.get('type', None) |
||
44 | if concept.type != concept_json_type: |
||
45 | |||
46 | if concept_json_type == 'concept': |
||
47 | members = concept.members |
||
48 | concept = skos_manager.change_type( |
||
49 | concept, |
||
50 | concept.concept_id, |
||
51 | concept.conceptscheme_id, |
||
52 | concept_json_type, |
||
53 | concept.uri |
||
54 | ) |
||
55 | for member in members: |
||
56 | if member.type == 'concept': |
||
57 | concept.narrower_concepts.add(member) |
||
58 | elif member.type == 'collection': |
||
59 | concept.narrower_collections.add(member) |
||
60 | elif concept_json_type == 'collection': |
||
61 | narrower_concepts = concept.narrower_concepts |
||
62 | narrower_collections = concept.narrower_collections |
||
63 | concept = skos_manager.change_type( |
||
64 | concept, |
||
65 | concept.concept_id, |
||
66 | concept.conceptscheme_id, |
||
67 | concept_json_type, |
||
68 | concept.uri |
||
69 | ) |
||
70 | for narrower_concept in narrower_concepts: |
||
71 | concept.members.add(narrower_concept) |
||
72 | for narrower_collection in narrower_collections: |
||
73 | concept.members.add(narrower_collection) |
||
74 | elif concept_json_type == 'collection': |
||
75 | concept.members.clear() |
||
76 | elif concept_json_type == 'concept': |
||
77 | concept.narrower_collections.clear() |
||
78 | concept.narrower_concepts.clear() |
||
79 | if concept.type in ('concept', 'collection'): |
||
80 | concept.labels[:] = [] |
||
81 | labels = concept_json.get('labels', []) |
||
82 | for label in labels: |
||
83 | label = Label( |
||
84 | label=label.get('label', ''), |
||
85 | labeltype_id=label.get('type', ''), |
||
86 | language_id=label.get('language', ''), |
||
87 | ) |
||
88 | concept.labels.append(label) |
||
89 | concept.notes[:] = [] |
||
90 | notes = concept_json.get('notes', []) |
||
91 | for n in notes: |
||
92 | note = Note(note=n.get('note', ''), notetype_id=n.get('type', ''), language_id=n.get('language', '')) |
||
93 | if is_html(note.note): |
||
94 | note.markup = 'HTML' |
||
95 | concept.notes.append(note) |
||
96 | concept.sources[:] = [] |
||
97 | sources = concept_json.get('sources', []) |
||
98 | for s in sources: |
||
99 | source = Source(citation=s.get('citation', '')) |
||
100 | if is_html(source.citation): |
||
101 | source.markup = 'HTML' |
||
102 | concept.sources.append(source) |
||
103 | |||
104 | concept.member_of.clear() |
||
105 | member_of = concept_json.get('member_of', []) |
||
106 | for memberof in member_of: |
||
107 | try: |
||
108 | memberof_collection = skos_manager.get_thing( |
||
109 | concept_id=memberof['id'], |
||
110 | conceptscheme_id=concept.conceptscheme_id) |
||
111 | except NoResultFound: |
||
112 | memberof_collection = Collection(concept_id=memberof['id'], conceptscheme_id=concept.conceptscheme_id) |
||
113 | concept.member_of.add(memberof_collection) |
||
114 | |||
115 | if concept.type == 'concept': |
||
116 | concept.related_concepts.clear() |
||
117 | related = concept_json.get('related', []) |
||
118 | for related in related: |
||
119 | try: |
||
120 | related_concept = skos_manager.get_thing( |
||
121 | concept_id=related['id'], |
||
122 | conceptscheme_id=concept.conceptscheme_id) |
||
123 | except NoResultFound: |
||
124 | related_concept = Concept(concept_id=related['id'], conceptscheme_id=concept.conceptscheme_id) |
||
125 | concept.related_concepts.add(related_concept) |
||
126 | |||
127 | concept.broader_concepts.clear() |
||
128 | broader = concept_json.get('broader', []) |
||
129 | for broader in broader: |
||
130 | try: |
||
131 | broader_concept = skos_manager.get_thing( |
||
132 | concept_id=broader['id'], |
||
133 | conceptscheme_id=concept.conceptscheme_id) |
||
134 | except NoResultFound: |
||
135 | broader_concept = Concept(concept_id=broader['id'], conceptscheme_id=concept.conceptscheme_id) |
||
136 | concept.broader_concepts.add(broader_concept) |
||
137 | narrower = concept_json.get('narrower', []) |
||
138 | for narrower in narrower: |
||
139 | try: |
||
140 | narrower_concept = skos_manager.get_thing( |
||
141 | concept_id=narrower['id'], |
||
142 | conceptscheme_id=concept.conceptscheme_id) |
||
143 | except NoResultFound: |
||
144 | narrower_concept = Concept(concept_id=narrower['id'], conceptscheme_id=concept.conceptscheme_id) |
||
145 | concept.narrower_concepts.add(narrower_concept) |
||
146 | |||
147 | matches = [] |
||
148 | matchdict = concept_json.get('matches', {}) |
||
149 | for match_type, uris in matchdict.items(): |
||
150 | db_type = match_type + "Match" |
||
151 | match_type = skos_manager.get_match_type(db_type) |
||
152 | for uri in uris: |
||
153 | concept_id = concept_json.get('id', -1) |
||
154 | try: |
||
155 | match = skos_manager.get_match(uri=uri, matchtype_id=match_type.name, |
||
156 | concept_id=concept_id) |
||
157 | except NoResultFound: |
||
158 | match = Match() |
||
159 | match.matchtype = match_type |
||
160 | match.uri = uri |
||
161 | matches.append(match) |
||
162 | concept.matches = matches |
||
163 | |||
164 | narrower_collections = concept_json.get('subordinate_arrays', []) |
||
165 | for narrower in narrower_collections: |
||
166 | try: |
||
167 | narrower_collection = skos_manager.get_thing( |
||
168 | concept_id=narrower['id'], |
||
169 | conceptscheme_id=concept.conceptscheme_id) |
||
170 | except NoResultFound: |
||
171 | narrower_collection = Collection(concept_id=narrower['id'], |
||
172 | conceptscheme_id=concept.conceptscheme_id) |
||
173 | concept.narrower_collections.add(narrower_collection) |
||
174 | |||
175 | if concept.type == 'collection': |
||
176 | members = concept_json.get('members', []) |
||
177 | for member in members: |
||
178 | try: |
||
179 | member_concept = skos_manager.get_thing( |
||
180 | concept_id=member['id'], |
||
181 | conceptscheme_id=concept.conceptscheme_id) |
||
182 | except NoResultFound: |
||
183 | member_concept = Concept(concept_id=member['id'], conceptscheme_id=concept.conceptscheme_id) |
||
184 | concept.members.add(member_concept) |
||
185 | |||
186 | concept.broader_concepts.clear() |
||
187 | broader_concepts = concept_json.get('superordinates', []) |
||
188 | for broader in broader_concepts: |
||
189 | try: |
||
190 | broader_concept = skos_manager.get_thing( |
||
191 | concept_id=broader['id'], |
||
192 | conceptscheme_id=concept.conceptscheme_id) |
||
193 | except NoResultFound: |
||
194 | broader_concept = Concept(concept_id=broader['id'], conceptscheme_id=concept.conceptscheme_id) |
||
195 | concept.broader_concepts.add(broader_concept) |
||
196 | if 'infer_concept_relations' in concept_json: |
||
197 | concept.infer_concept_relations = concept_json['infer_concept_relations'] |
||
198 | return concept |
||
199 | |||
262 |