Conditions | 6 |
Total Lines | 61 |
Code Lines | 44 |
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:
1 | """ |
||
24 | def from_thing(thing): |
||
25 | """ |
||
26 | Map a :class:`skosprovider_sqlalchemy.models.Thing` to a |
||
27 | :class:`skosprovider.skos.Concept` or |
||
28 | a :class:`skosprovider.skos.Collection`, depending on the type. |
||
29 | |||
30 | :param skosprovider_sqlalchemy.models.Thing thing: Thing to map. |
||
31 | :rtype: :class:`~skosprovider.skos.Concept` or |
||
32 | :class:`~skosprovider.skos.Collection`. |
||
33 | """ |
||
34 | if thing.type and thing.type == 'collection': |
||
35 | return Collection( |
||
36 | id=thing.concept_id, |
||
37 | uri=thing.uri, |
||
38 | concept_scheme=ConceptScheme(thing.conceptscheme.uri), |
||
39 | labels=[ |
||
40 | Label(label.label, label.labeltype_id, label.language_id) |
||
41 | for label in thing.labels |
||
42 | ], |
||
43 | notes=[ |
||
44 | Note(n.note, n.notetype_id, n.language_id) |
||
45 | for n in thing.notes |
||
46 | ], |
||
47 | sources=[ |
||
48 | Source(s.citation) |
||
49 | for s in thing.sources |
||
50 | ], |
||
51 | members=[member.concept_id for member in thing.members] if hasattr(thing, 'members') else [], |
||
52 | member_of=[c.concept_id for c in thing.member_of], |
||
53 | superordinates=[broader_concept.concept_id for broader_concept in thing.broader_concepts], |
||
54 | infer_concept_relations=thing.infer_concept_relations |
||
55 | ) |
||
56 | else: |
||
57 | matches = {} |
||
58 | for m in thing.matches: |
||
59 | key = m.matchtype.name[:m.matchtype.name.find('Match')] |
||
60 | if key not in matches: |
||
61 | matches[key] = [] |
||
62 | matches[key].append(m.uri) |
||
63 | return Concept( |
||
64 | id=thing.concept_id, |
||
65 | uri=thing.uri, |
||
66 | concept_scheme=ConceptScheme(thing.conceptscheme.uri), |
||
67 | labels=[ |
||
68 | Label(label.label, label.labeltype_id, label.language_id) |
||
69 | for label in thing.labels |
||
70 | ], |
||
71 | notes=[ |
||
72 | Note(n.note, n.notetype_id, n.language_id) |
||
73 | for n in thing.notes |
||
74 | ], |
||
75 | sources=[ |
||
76 | Source(s.citation) |
||
77 | for s in thing.sources |
||
78 | ], |
||
79 | broader=[c.concept_id for c in thing.broader_concepts], |
||
80 | narrower=[c.concept_id for c in thing.narrower_concepts], |
||
81 | related=[c.concept_id for c in thing.related_concepts], |
||
82 | member_of=[c.concept_id for c in thing.member_of], |
||
83 | subordinate_arrays=[narrower_collection.concept_id for narrower_collection in thing.narrower_collections], |
||
84 | matches=matches, |
||
85 | ) |
||
161 |