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