Skip to content

Commit e963aaf

Browse files
committed
[rdf/QueryCreator] Minor PEP8 fixes
- anomalous-backslash-in-string - fixme - invalid-name - line-too-long - trailing-whitespace
1 parent 6580324 commit e963aaf

1 file changed

Lines changed: 101 additions & 81 deletions

File tree

odml/rdf/query_creator.py

Lines changed: 101 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ class BaseQueryCreator:
1313

1414
__metaclass__ = ABCMeta
1515

16-
possible_query_variables = {'d': 'Document', 's': 'Section',
17-
'p': 'Property', 'v': 'Bag URI', 'value': 'Value'}
16+
possible_query_variables = {"d": "Document", "s": "Section",
17+
"p": "Property", "v": "Bag URI", "value": "Value"}
1818

19-
possible_q_dict_keys = ['Doc', 'Sec', 'Prop']
19+
possible_q_dict_keys = ["Doc", "Sec", "Prop"]
2020

2121
def __init__(self, q_dict=None):
2222
"""
2323
:param q_dict: dictionary with query parameters
2424
"""
2525
self.q_dict = q_dict if q_dict else {}
26-
self.query = ''
26+
self.query = ""
2727
super(BaseQueryCreator, self).__init__()
2828

2929
@abstractmethod
3030
def get_query(self, q_str, q_parser):
3131
pass
32-
32+
3333
@abstractmethod
3434
def _prepare_query(self):
3535
pass
@@ -71,69 +71,75 @@ def parse_query_string(self, q_str):
7171
having_pattern = re.compile("HAVING(.*)")
7272
having_group = re.search(having_pattern, q_str).group(1).strip()
7373
if having_group:
74-
if 'Search' in self.q_dict.keys():
75-
raise ValueError('Search values are already parsed')
74+
if "Search" in self.q_dict.keys():
75+
raise ValueError("Search values are already parsed")
7676
self._parse_having(having_group)
7777
else:
78-
raise ValueError('Search values in having part were not specified')
78+
raise ValueError("Search values in having part were not specified")
7979

8080
return self.q_dict
8181

8282
def _parse_find(self, find_part):
8383
"""
84-
Parses find string part into list of specific keys to whih search values would be apllied
85-
e.g. 'sec(name, type) prop(name)' into {'Sec': ['name', 'type'], 'Prop': ['name']} .
86-
87-
:param find_part: string which represent list of searchable odML data model objects
88-
like document(doc), sections(sec) or properties(prop).
89-
e.g. 'sec(name, type) prop(name)'
84+
Parses find string part into list of specific keys to which search values
85+
would be applied. e.g. 'sec(name, type) prop(name)'
86+
into {'Sec': ['name', 'type'], 'Prop': ['name']}.
87+
88+
:param find_part: string which represent list of searchable odML data model
89+
objects like document(doc), sections(sec) or properties(prop).
90+
e.g. 'sec(name, type) prop(name)'
9091
"""
91-
doc_pattern = re.compile("(doc|document)\(.*?\)")
92+
doc_pattern = re.compile("(doc|document)[(].*?[)]")
9293
doc = re.search(doc_pattern, find_part)
9394
if doc:
9495
self._parse_doc(doc)
9596

96-
sec_pattern = re.compile("(sec|section)\(.*?\)")
97+
sec_pattern = re.compile("(sec|section)[(].*?[)]")
9798
sec = re.search(sec_pattern, find_part)
9899
if sec:
99100
self._parse_sec(sec)
100101

101-
prop_pattern = re.compile("(prop|property)\(.*?\)")
102+
prop_pattern = re.compile("(prop|property)[(].*?[)]")
102103
prop = re.search(prop_pattern, find_part)
103104
if prop:
104105
self._parse_prop(prop)
105106

106107
def _parse_doc(self, doc):
107-
p = re.compile("[\(|, ](id|author|date|version|repository|sections)[\)|,]")
108+
re_obj = re.compile("[(, ](id|author|date|version|repository|sections)[),]")
108109
if doc:
109-
self.q_dict['Doc'] = re.findall(p, doc.group(0))
110+
self.q_dict["Doc"] = re.findall(re_obj, doc.group(0))
110111

111112
def _parse_sec(self, sec):
112-
p = re.compile("[\(|, ](id|name|definition|type|repository|reference|sections|properties)[\)|,]")
113+
attr_list = "id|name|definition|type|repository|reference|sections|properties"
114+
pattern = "[(, ](%s)[),]" % attr_list
115+
re_obj = re.compile(pattern)
113116
if sec:
114-
self.q_dict['Sec'] = re.findall(p, sec.group(0))
117+
self.q_dict["Sec"] = re.findall(re_obj, sec.group(0))
115118

116119
def _parse_prop(self, prop):
117-
p = re.compile("[\(|, ](id|name|definition|dtype|unit|uncertainty|reference|value_origin)[\)|,]")
120+
attr_list = "id|name|definition|dtype|unit|uncertainty|reference|value_origin"
121+
pattern = "[(, ](%s)[),]" % attr_list
122+
re_obj = re.compile(pattern)
118123
if prop:
119-
self.q_dict['Prop'] = re.findall(p, prop.group(0))
124+
self.q_dict["Prop"] = re.findall(re_obj, prop.group(0))
120125

121126
def _parse_having(self, having_part):
122127
"""
123-
Parses search value string into list of specific values
128+
Parses search value string into list of specific values.
124129
e.g. 'Stimulus, Contrast, Date' into list [Stimulus, Contrast, Date].
125-
130+
126131
:param having_part: string with search values, e.g. 'Stimulus, Contrast'
127-
Also spaces errors in the string like 'Stimulus, , Contrast' will be ignored.
132+
Also spaces errors in the string like 'Stimulus, , Contrast'
133+
will be ignored.
128134
"""
129135
search_values_list = []
130136
search_params = re.compile("(.*?)(?:,|$)")
131137
if having_part:
132138
search_values = re.findall(search_params, having_part)
133-
for v in search_values:
134-
if v.strip():
135-
search_values_list.append(v.strip())
136-
self.q_dict['Search'] = search_values_list
139+
for val in search_values:
140+
if val.strip():
141+
search_values_list.append(val.strip())
142+
self.q_dict["Search"] = search_values_list
137143

138144

139145
class QueryParser(BaseQueryParser):
@@ -144,62 +150,71 @@ def __init__(self):
144150
def parse_query_string(self, q_str):
145151
"""
146152
:param q_str: query string
147-
Example: doc(author:D. N. Adams) section(name:Stimulus) prop(name:Contrast, value:20, unit:%)
153+
Example: doc(author:D. N. Adams) section(name:Stimulus)
154+
prop(name:Contrast, value:20, unit:%)
148155
:return: dict object
149156
Example: {'Sec': [('name', 'Stimulus')],
150157
'Doc': [('author', 'D. N. Adams')],
151158
'Prop': [('name', 'Contrast'), ('value':[20]), ('unit':'%')]}
152159
"""
153-
doc_pattern = re.compile("(doc|document)\(.*?\)")
160+
doc_pattern = re.compile("(doc|document)[(].*?[)]")
154161
doc = re.search(doc_pattern, q_str)
155162
if doc:
156163
self._parse_doc(doc)
157164

158-
sec_pattern = re.compile("(sec|section)\(.*?\)")
165+
sec_pattern = re.compile("(sec|section)[(].*?[)]")
159166
sec = re.search(sec_pattern, q_str)
160167
if sec:
161168
self._parse_sec(sec)
162169

163-
prop_pattern = re.compile("(prop|property)\(.*?\)")
170+
prop_pattern = re.compile("(prop|property)[(].*?[)]")
164171
prop = re.search(prop_pattern, q_str)
165172
if prop:
166173
self._parse_prop(prop)
167-
174+
168175
return self.q_dict
169176

170177
def _parse_doc(self, doc):
171-
p = re.compile("[, |\(](id|author|date|version|repository|sections):(.*?)[,|\)]")
178+
attr_list = "id|author|date|version|repository|sections"
179+
pattern = "[, (](%s):(.*?)[,)]" % attr_list
180+
re_obj = re.compile(pattern)
172181
if doc:
173-
self.q_dict['Doc'] = re.findall(p, doc.group(0))
182+
self.q_dict["Doc"] = re.findall(re_obj, doc.group(0))
174183

175184
def _parse_sec(self, sec):
176-
p = re.compile("[, |\(](id|name|definition|type|repository|reference|sections|properties):(.*?)[,|\)]")
185+
attr_list = "id|name|definition|type|repository|reference|sections|properties"
186+
pattern = "[, (](%s):(.*?)[,)]" % attr_list
187+
re_obj = re.compile(pattern)
177188
if sec:
178-
self.q_dict['Sec'] = re.findall(p, sec.group(0))
189+
self.q_dict["Sec"] = re.findall(re_obj, sec.group(0))
179190

180191
def _parse_prop(self, prop):
181-
p = re.compile("[, |\(](id|name|definition|dtype|unit|uncertainty|reference|value_origin):(.*?)[,|\)]")
192+
attr_list = "id|name|definition|dtype|unit|uncertainty|reference|value_origin"
193+
pattern = "[, (](%s):(.*?)[,)]" % attr_list
194+
re_obj = re.compile(pattern)
182195
if prop:
183-
self.q_dict['Prop'] = re.findall(p, prop.group(0))
196+
self.q_dict["Prop"] = re.findall(re_obj, prop.group(0))
184197

185-
p_value = re.compile("value:\[(.*)]")
198+
p_value = re.compile(r"value:\[(.*)]")
186199

187200
value_group = re.findall(p_value, prop.group(0))
188201
if value_group:
189202
values = re.split(", ?", value_group[0])
190-
self.q_dict['Prop'].append(('value', values))
203+
self.q_dict["Prop"].append(("value", values))
191204

192205

193206
class QueryCreator(BaseQueryCreator):
194-
"""
195-
Class for simplifying the creation of prepared SPARQL queries
196-
207+
"""
208+
Class for simplifying the creation of prepared SPARQL queries.
209+
197210
Usage:
198-
q = "doc(author:D. N. Adams) section(name:Stimulus) prop(name:Contrast, value:20, unit:%)"
199-
prepared_query = QueryCreator().get_query(q, QueryParser())
200-
201-
q = "FIND sec(name, type) prop(name) HAVING Recording, Recording-2012-04-04-ab, Date"
202-
prepared_query = QueryCreator().get_query(q, QueryParser2())
211+
query = "doc(author:D. N. Adams) section(name:Stimulus)
212+
prop(name:Contrast, value:20, unit:%)"
213+
prepared_query = QueryCreator().get_query(query, QueryParser())
214+
215+
query = "FIND sec(name, type) prop(name) HAVING Recording,
216+
Recording-2012-04-04-ab, Date"
217+
prepared_query = QueryCreator().get_query(query, QueryParser2())
203218
"""
204219

205220
def __init__(self, q_dict=None):
@@ -211,20 +226,20 @@ def __init__(self, q_dict=None):
211226
def get_query(self, q_str=None, q_parser=None):
212227
"""
213228
:param q_parser: one of possible query parsers.
214-
:param q_str: doc(author:D. N. Adams) section(name:Stimulus) prop(name:Contrast, value:20, unit:%)
229+
:param q_str: doc(author:D. N. Adams) section(name:Stimulus)
230+
prop(name:Contrast, value:20, unit:%)
215231
:return rdflib prepare query.
216232
"""
217-
# TODO find out if the validation for the q_str is important
218-
# We can possibly warn about not used parts and print the parsed dictionary
219233
if not self.q_dict:
220234
if not q_str:
221235
raise AttributeError("Please fulfill q_str param (query string)")
222236
elif not q_parser:
223237
raise AttributeError("Please fulfill q_parser param (query parser)")
224238
self.q_dict = q_parser.parse_query_string(q_str)
225239
self._prepare_query()
226-
return prepareQuery(self.query, initNs={"odml": Namespace("https://g-node.org/odml-rdf#"),
227-
"rdf": RDF})
240+
241+
use_ns = {"odml": Namespace("https://g-node.org/odml-rdf#"), "rdf": RDF}
242+
return prepareQuery(self.query, initNs=use_ns)
228243

229244
def _prepare_query(self):
230245
"""
@@ -233,52 +248,57 @@ def _prepare_query(self):
233248
"""
234249

235250
odml_uri = "https://g-node.org/odml-rdf#"
236-
self.query = 'SELECT * WHERE {\n'
251+
self.query = "SELECT * WHERE {\n"
237252

238-
if 'Doc' in self.q_dict.keys():
239-
doc_attrs = self.q_dict['Doc']
253+
if "Doc" in self.q_dict.keys():
254+
doc_attrs = self.q_dict["Doc"]
240255
if len(doc_attrs) > 0:
241-
self.query += '?d rdf:type odml:Document .\n'
256+
self.query += "?d rdf:type odml:Document .\n"
242257
for i in doc_attrs:
243258
if len(i) > 2:
244-
raise ValueError("Attributes in the query \"{}\" are not valid.".format(i))
259+
msg = "Attributes in the query \"{}\" are not valid.".format(i)
260+
raise ValueError(msg)
245261
else:
246262
attr = Document.rdf_map(i[0])
247263
if attr:
248-
self.query += '?d {0} \"{1}\" .\n'.format(re.sub(odml_uri,
249-
"odml:", attr), i[1])
250-
if 'Sec' in self.q_dict.keys():
251-
sec_attrs = self.q_dict['Sec']
264+
re_sub = re.sub(odml_uri, "odml:", attr)
265+
self.query += "?d {0} \"{1}\" .\n".format(re_sub, i[1])
266+
267+
if "Sec" in self.q_dict.keys():
268+
sec_attrs = self.q_dict["Sec"]
252269
if len(sec_attrs) > 0:
253-
self.query += '?d odml:hasSection ?s .\n' \
254-
'?s rdf:type odml:Section .\n'
270+
self.query += "?d odml:hasSection ?s .\n"
271+
self.query += "?s rdf:type odml:Section .\n"
255272
for i in sec_attrs:
256273
if len(i) > 2:
257-
raise ValueError("Attributes in the query \"{}\" are not valid.".format(i))
274+
msg = "Attributes in the query \"{}\" are not valid.".format(i)
275+
raise ValueError(msg)
258276
else:
259277
attr = Section.rdf_map(i[0])
260278
if attr:
261-
self.query += '?s {0} \"{1}\" .\n'.format(re.sub(odml_uri,
262-
"odml:", attr), i[1])
263-
if 'Prop' in self.q_dict.keys():
264-
prop_attrs = self.q_dict['Prop']
279+
re_sub = re.sub(odml_uri, "odml:", attr)
280+
self.query += "?s {0} \"{1}\" .\n".format(re_sub, i[1])
281+
282+
if "Prop" in self.q_dict.keys():
283+
prop_attrs = self.q_dict["Prop"]
265284
if len(prop_attrs) > 0:
266-
self.query += '?s odml:hasProperty ?p .\n' \
267-
'?p rdf:type odml:Property .\n'
285+
self.query += "?s odml:hasProperty ?p .\n"
286+
self.query += "?p rdf:type odml:Property .\n"
268287
for i in prop_attrs:
269288
if len(i) > 2:
270-
raise ValueError("Attributes in the query \"{}\" are not valid.".format(i))
271-
elif i[0] == 'value':
289+
msg = "Attributes in the query \"{}\" are not valid.".format(i)
290+
raise ValueError(msg)
291+
elif i[0] == "value":
272292
values = i[1]
273293
if values:
274294
self.query += "?p odml:hasValue ?v .\n?v rdf:type rdf:Bag .\n"
275-
for v in values:
276-
self.query += '?v rdf:li \"{}\" .\n'.format(v)
295+
for val in values:
296+
self.query += "?v rdf:li \"{}\" .\n".format(val)
277297
else:
278298
attr = Property.rdf_map(i[0])
279299
if attr:
280-
self.query += '?p {0} \"{1}\" .\n'.format(re.sub(odml_uri,
281-
"odml:", attr), i[1])
300+
re_sub = re.sub(odml_uri, "odml:", attr)
301+
self.query += "?p {0} \"{1}\" .\n".format(re_sub, i[1])
282302

283-
self.query += '}\n'
303+
self.query += "}\n"
284304
return self.query

0 commit comments

Comments
 (0)