pyLodStorage 0.2.12__tar.gz → 0.2.14__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pylodstorage-0.2.12/pylodstorage.egg-info → pylodstorage-0.2.14}/PKG-INFO +1 -1
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/query.py +1 -1
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/trulytabular.py +73 -8
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/version.py +1 -1
- {pylodstorage-0.2.12 → pylodstorage-0.2.14/pylodstorage.egg-info}/PKG-INFO +1 -1
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/LICENSE +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/README.md +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/__init__.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/csv.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/entity.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/jsonable.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/jsonpicklemixin.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/lod.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/mwTable.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/plot.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/querymain.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/sample.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/schema.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/sparql.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/sql.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/storageconfig.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/tabulateCounter.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/uml.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/xml.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/yamlablemixin.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/pylodstorage.egg-info/SOURCES.txt +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/pylodstorage.egg-info/dependency_links.txt +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/pylodstorage.egg-info/entry_points.txt +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/pylodstorage.egg-info/requires.txt +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/pylodstorage.egg-info/top_level.txt +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/__init__.py +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/endpoints.yaml +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/formats.yaml +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/queries.yaml +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/scholia.yaml +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/trulytabular.yaml +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/wf.yaml +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/wikidata.yaml +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/setup.cfg +0 -0
- {pylodstorage-0.2.12 → pylodstorage-0.2.14}/setup.py +0 -0
|
@@ -336,7 +336,7 @@ class Query(object):
|
|
|
336
336
|
str: the "try it!" url for the given query
|
|
337
337
|
'''
|
|
338
338
|
# https://stackoverflow.com/a/9345102/1497139
|
|
339
|
-
quoted=urllib.parse.quote(self.query)
|
|
339
|
+
quoted=urllib.parse.quote(str(self.query))
|
|
340
340
|
quoted=f"#{quoted}"
|
|
341
341
|
url=f"{baseurl}/{quoted}"
|
|
342
342
|
return url
|
|
@@ -3,12 +3,34 @@ Created on 2022-04-14
|
|
|
3
3
|
|
|
4
4
|
@author: wf
|
|
5
5
|
'''
|
|
6
|
+
import datetime
|
|
6
7
|
from lodstorage.sparql import SPARQL
|
|
7
8
|
from lodstorage.query import Query,QueryManager,YamlPath
|
|
9
|
+
from pathlib import Path
|
|
8
10
|
import os
|
|
9
11
|
import textwrap
|
|
10
12
|
import re
|
|
11
13
|
|
|
14
|
+
class Variable:
|
|
15
|
+
'''
|
|
16
|
+
Variable e.g. name handling
|
|
17
|
+
'''
|
|
18
|
+
@classmethod
|
|
19
|
+
def validVarName(cls,varStr:str)->str:
|
|
20
|
+
'''
|
|
21
|
+
convert the given potential variable name string to a valid
|
|
22
|
+
variable name
|
|
23
|
+
|
|
24
|
+
see https://stackoverflow.com/a/3305731/1497139
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
varStr(str): the string to convert
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
str: a valid variable name
|
|
31
|
+
'''
|
|
32
|
+
return re.sub('\W|^(?=\d)','_', varStr)
|
|
33
|
+
|
|
12
34
|
class WikidataProperty():
|
|
13
35
|
'''
|
|
14
36
|
a WikidataProperty
|
|
@@ -29,13 +51,13 @@ class WikidataProperty():
|
|
|
29
51
|
get me as a Predicate
|
|
30
52
|
'''
|
|
31
53
|
reverseToken="^" if self.reverse else ""
|
|
32
|
-
|
|
33
|
-
return
|
|
54
|
+
plabel=f"{reverseToken}wdt:{self.pid}"
|
|
55
|
+
return plabel
|
|
34
56
|
|
|
35
57
|
def __str__(self):
|
|
36
58
|
text=self.pid
|
|
37
|
-
if hasattr(self, "
|
|
38
|
-
text=f"{self.
|
|
59
|
+
if hasattr(self, "plabel"):
|
|
60
|
+
text=f"{self.plabel} ({self.pid})"
|
|
39
61
|
return text
|
|
40
62
|
|
|
41
63
|
@classmethod
|
|
@@ -115,9 +137,10 @@ SELECT ?property ?propertyLabel WHERE {
|
|
|
115
137
|
url=record["property"]
|
|
116
138
|
pid=re.sub(r"http://www.wikidata.org/entity/(.*)",r"\1",url)
|
|
117
139
|
prop=WikidataProperty(pid)
|
|
118
|
-
prop.
|
|
140
|
+
prop.plabel=record["propertyLabel"]
|
|
119
141
|
prop.url=url
|
|
120
|
-
wdProperties[prop.
|
|
142
|
+
wdProperties[prop.plabel]=prop
|
|
143
|
+
prop.varname=Variable.validVarName(prop.plabel)
|
|
121
144
|
pass
|
|
122
145
|
return wdProperties
|
|
123
146
|
|
|
@@ -140,6 +163,7 @@ class WikidataItem:
|
|
|
140
163
|
self.sparql=sparql
|
|
141
164
|
if sparql is not None:
|
|
142
165
|
self.qlabel,self.description=WikidataItem.getLabelAndDescription(sparql, self.qid, self.lang)
|
|
166
|
+
self.varname=Variable.validVarName(self.qlabel)
|
|
143
167
|
|
|
144
168
|
def __str__(self):
|
|
145
169
|
return self.asText(long=False)
|
|
@@ -238,6 +262,7 @@ WHERE {
|
|
|
238
262
|
item=WikidataItem(qid)
|
|
239
263
|
item.url=url
|
|
240
264
|
item.qlabel=record["itemLabel"]
|
|
265
|
+
item.varname=Variable.validVarName(item.qlabel)
|
|
241
266
|
item.description=record["itemDescription"]
|
|
242
267
|
items.append(item)
|
|
243
268
|
return items
|
|
@@ -274,6 +299,7 @@ class TrulyTabular(object):
|
|
|
274
299
|
self.queryManager=TrulyTabular.getQueryManager(debug=self.debug)
|
|
275
300
|
self.properties=WikidataProperty.getPropertiesByIds(self.sparql,propertyIds,lang)
|
|
276
301
|
self.properties.update(WikidataProperty.getPropertiesByLabels(self.sparql, propertyLabels, lang))
|
|
302
|
+
self.isodate=datetime.datetime.now().isoformat()
|
|
277
303
|
self.error=None
|
|
278
304
|
|
|
279
305
|
def __str__(self):
|
|
@@ -342,6 +368,45 @@ WHERE
|
|
|
342
368
|
return qm
|
|
343
369
|
return None
|
|
344
370
|
|
|
371
|
+
|
|
372
|
+
def generateSparqlQuery(self,lang:str='en',naive:bool=True)->str:
|
|
373
|
+
'''
|
|
374
|
+
generate a SPARQL Query
|
|
375
|
+
'''
|
|
376
|
+
item=self.item
|
|
377
|
+
script=Path(__file__).name
|
|
378
|
+
naiveText="naive" if naive else "aggregate"
|
|
379
|
+
sparqlQuery=f"""# truly tabular {naiveText} query for
|
|
380
|
+
# {item.qid}:{item.qlabel}
|
|
381
|
+
# generated by {script} on {self.isodate}
|
|
382
|
+
PREFIX wd: <http://www.wikidata.org/entity/>
|
|
383
|
+
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
|
|
384
|
+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
|
|
385
|
+
SELECT ?{item.varname} ?{item.varname}Label"""
|
|
386
|
+
for wdProp in self.properties.values():
|
|
387
|
+
if naive:
|
|
388
|
+
sparqlQuery+=f"\n ?{wdProp.varname}"
|
|
389
|
+
else:
|
|
390
|
+
sparqlQuery+=f"\n (SAMPLE (?{wdProp.varname}) AS ?{wdProp.varname})"
|
|
391
|
+
sparqlQuery+=f"""
|
|
392
|
+
WHERE {{
|
|
393
|
+
# instanceof {item.qid}:{item.qlabel}
|
|
394
|
+
?{item.varname} wdt:P31 wd:{item.qid}.
|
|
395
|
+
# label
|
|
396
|
+
?{item.varname} rdfs:label ?{item.varname}Label
|
|
397
|
+
FILTER (LANG(?{item.varname}Label) = "{lang}").
|
|
398
|
+
"""
|
|
399
|
+
for wdProp in self.properties.values():
|
|
400
|
+
sparqlQuery+=f""" # {wdProp}
|
|
401
|
+
OPTIONAL {{ ?{item.varname} wdt:{wdProp.pid} ?{wdProp.varname}. }}
|
|
402
|
+
"""
|
|
403
|
+
# close where Clause
|
|
404
|
+
sparqlQuery+="""}\n"""
|
|
405
|
+
# optionally add Aggregate
|
|
406
|
+
if not naive:
|
|
407
|
+
sparqlQuery+=f"GROUP BY ?{item.varname} ?{item.varname}Label"
|
|
408
|
+
return sparqlQuery
|
|
409
|
+
|
|
345
410
|
def mostFrequentPropertiesQuery(self,whereClause:str=None):
|
|
346
411
|
'''
|
|
347
412
|
get the most frequently used properties
|
|
@@ -367,7 +432,7 @@ WHERE
|
|
|
367
432
|
wdProperty(WikidataProperty): the property to analyze
|
|
368
433
|
asFrequency(bool): if true do a frequency analysis
|
|
369
434
|
'''
|
|
370
|
-
propertyLabel=wdProperty.
|
|
435
|
+
propertyLabel=wdProperty.plabel
|
|
371
436
|
propertyId=wdProperty.pid
|
|
372
437
|
# work around https://github.com/RDFLib/sparqlwrapper/issues/211
|
|
373
438
|
if "described at" in propertyLabel:
|
|
@@ -436,7 +501,7 @@ ORDER BY DESC(?count)"""
|
|
|
436
501
|
dict: a statistics row
|
|
437
502
|
'''
|
|
438
503
|
ntlod=self.noneTabular(wdProperty)
|
|
439
|
-
statsRow={"property":wdProperty.
|
|
504
|
+
statsRow={"property":wdProperty.plabel}
|
|
440
505
|
total=0
|
|
441
506
|
nttotal=0
|
|
442
507
|
maxCount=0
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|