Conditions | 21 |
Total Lines | 84 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 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 _rdf_dumper() 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 | # -*- coding: utf-8 -*- |
||
60 | def _rdf_dumper(provider, id_list=None): |
||
61 | ''' |
||
62 | Dump a provider to a format that can be passed to a |
||
63 | :class:`skosprovider.providers.RDFProvider`. |
||
64 | |||
65 | :param skosprovider.providers.VocabularyProvider provider: The provider |
||
66 | that wil be turned into an :class:`rdflib.graph.Graph`. |
||
67 | |||
68 | :param List id_list: List of id's of the data to dump. |
||
69 | |||
70 | :rtype: :class:`rdflib.graph.Graph` |
||
71 | ''' |
||
72 | graph = Graph() |
||
73 | graph.namespace_manager.bind("skos", SKOS) |
||
74 | graph.namespace_manager.bind("dcterms", DCTERMS) |
||
75 | graph.namespace_manager.bind("skos-thes", SKOS_THES) |
||
76 | graph.namespace_manager.bind("void", VOID) |
||
77 | conceptscheme = URIRef(provider.concept_scheme.uri) |
||
78 | _add_in_dataset(graph, conceptscheme, provider) |
||
79 | graph.add((conceptscheme, RDF.type, SKOS.ConceptScheme)) |
||
80 | graph.add((conceptscheme, DCTERMS.identifier, Literal(provider.metadata['id']))) |
||
81 | _add_labels(graph, provider.concept_scheme, conceptscheme) |
||
82 | _add_notes(graph, provider.concept_scheme, conceptscheme) |
||
83 | _add_sources(graph, provider.concept_scheme, conceptscheme) |
||
84 | _add_languages(graph, provider.concept_scheme, conceptscheme) |
||
85 | # Add triples using store's add method. |
||
86 | if not id_list: |
||
87 | id_list = [x['id'] for x in provider.get_all()] |
||
88 | for c in provider.get_top_concepts(): |
||
89 | graph.add((conceptscheme, SKOS.hasTopConcept, URIRef(c['uri']))) |
||
90 | for id in id_list: |
||
91 | c = provider.get_by_id(id) |
||
92 | subject = URIRef(c.uri) |
||
93 | _add_in_dataset(graph, subject, provider) |
||
94 | graph.add((subject, DCTERMS.identifier, Literal(c.id))) |
||
95 | graph.add((subject, SKOS.inScheme, conceptscheme)) |
||
96 | _add_labels(graph, c, subject) |
||
97 | _add_notes(graph, c, subject) |
||
98 | _add_sources(graph, c, subject) |
||
99 | if isinstance(c, Concept): |
||
100 | graph.add((subject, RDF.type, SKOS.Concept)) |
||
101 | for b in c.broader: |
||
102 | broader = provider.get_by_id(b) |
||
103 | if broader: |
||
104 | graph.add((subject, SKOS.broader, URIRef(broader.uri))) |
||
105 | else: |
||
106 | warnings.warn(_warning(b), UserWarning) |
||
107 | for n in c.narrower: |
||
108 | narrower = provider.get_by_id(n) |
||
109 | if narrower: |
||
110 | graph.add((subject, SKOS.narrower, URIRef(narrower.uri))) |
||
111 | else: |
||
112 | warnings.warn(_warning(n), UserWarning) |
||
113 | for r in c.related: |
||
114 | related = provider.get_by_id(r) |
||
115 | if related: |
||
116 | graph.add((subject, SKOS.related, URIRef(related.uri))) |
||
117 | else: |
||
118 | warnings.warn(_warning(r), UserWarning) |
||
119 | for s in c.subordinate_arrays: |
||
120 | subordinate_array = provider.get_by_id(s) |
||
121 | if subordinate_array: |
||
122 | graph.add((subject, SKOS_THES.subordinateArray, URIRef(subordinate_array.uri))) |
||
123 | else: |
||
124 | warnings.warn(_warning(s), UserWarning) |
||
125 | for k in c.matches.keys(): |
||
126 | for uri in c.matches[k]: |
||
127 | graph.add((subject, URIRef(SKOS + k +'Match'), URIRef(uri))) |
||
128 | elif isinstance(c, Collection): |
||
129 | graph.add((subject, RDF.type, SKOS.Collection)) |
||
130 | for m in c.members: |
||
131 | member = provider.get_by_id(m) |
||
132 | if member: |
||
133 | graph.add((subject, SKOS.member, URIRef(member.uri))) |
||
134 | else: |
||
135 | warnings.warn(_warning(m), UserWarning) |
||
136 | for s in c.superordinates: |
||
137 | superordinate = provider.get_by_id(s) |
||
138 | if superordinate: |
||
139 | graph.add((subject, SKOS_THES.superOrdinate, URIRef(superordinate.uri))) |
||
140 | else: |
||
141 | warnings.warn(_warning(s), UserWarning) |
||
142 | |||
143 | return graph |
||
144 | |||
309 |