| 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 | """ |
||
| 23 | def from_thing(thing): |
||
| 24 | """ |
||
| 25 | Map a :class:`skosprovider_sqlalchemy.models.Thing` to a |
||
| 26 | :class:`skosprovider.skos.Concept` or |
||
| 27 | a :class:`skosprovider.skos.Collection`, depending on the type. |
||
| 28 | |||
| 29 | :param skosprovider_sqlalchemy.models.Thing thing: Thing to map. |
||
| 30 | :rtype: :class:`~skosprovider.skos.Concept` or |
||
| 31 | :class:`~skosprovider.skos.Collection`. |
||
| 32 | """ |
||
| 33 | if thing.type and thing.type == 'collection': |
||
| 34 | return Collection( |
||
| 35 | id=thing.concept_id, |
||
| 36 | uri=thing.uri, |
||
| 37 | concept_scheme=ConceptScheme(thing.conceptscheme.uri), |
||
| 38 | labels=[ |
||
| 39 | Label(label.label, label.labeltype_id, label.language_id) |
||
| 40 | for label in thing.labels |
||
| 41 | ], |
||
| 42 | notes=[ |
||
| 43 | Note(n.note, n.notetype_id, n.language_id) |
||
| 44 | for n in thing.notes |
||
| 45 | ], |
||
| 46 | sources=[ |
||
| 47 | Source(s.citation) |
||
| 48 | for s in thing.sources |
||
| 49 | ], |
||
| 50 | members=[member.concept_id for member in thing.members] if hasattr(thing, 'members') else [], |
||
| 51 | member_of=[c.concept_id for c in thing.member_of], |
||
| 52 | superordinates=[broader_concept.concept_id for broader_concept in thing.broader_concepts], |
||
| 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=[ |
||
| 71 | Note(n.note, n.notetype_id, n.language_id) |
||
| 72 | for n in thing.notes |
||
| 73 | ], |
||
| 74 | sources=[ |
||
| 75 | Source(s.citation) |
||
| 76 | for s in thing.sources |
||
| 77 | ], |
||
| 78 | broader=[c.concept_id for c in thing.broader_concepts], |
||
| 79 | narrower=[c.concept_id for c in thing.narrower_concepts], |
||
| 80 | related=[c.concept_id for c in thing.related_concepts], |
||
| 81 | member_of=[c.concept_id for c in thing.member_of], |
||
| 82 | subordinate_arrays=[narrower_collection.concept_id for narrower_collection in thing.narrower_collections], |
||
| 83 | matches=matches, |
||
| 84 | ) |
||
| 145 |