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.
Files changed (40) hide show
  1. {pylodstorage-0.2.12/pylodstorage.egg-info → pylodstorage-0.2.14}/PKG-INFO +1 -1
  2. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/query.py +1 -1
  3. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/trulytabular.py +73 -8
  4. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/version.py +1 -1
  5. {pylodstorage-0.2.12 → pylodstorage-0.2.14/pylodstorage.egg-info}/PKG-INFO +1 -1
  6. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/LICENSE +0 -0
  7. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/README.md +0 -0
  8. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/__init__.py +0 -0
  9. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/csv.py +0 -0
  10. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/entity.py +0 -0
  11. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/jsonable.py +0 -0
  12. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/jsonpicklemixin.py +0 -0
  13. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/lod.py +0 -0
  14. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/mwTable.py +0 -0
  15. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/plot.py +0 -0
  16. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/querymain.py +0 -0
  17. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/sample.py +0 -0
  18. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/schema.py +0 -0
  19. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/sparql.py +0 -0
  20. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/sql.py +0 -0
  21. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/storageconfig.py +0 -0
  22. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/tabulateCounter.py +0 -0
  23. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/uml.py +0 -0
  24. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/xml.py +0 -0
  25. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/lodstorage/yamlablemixin.py +0 -0
  26. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/pylodstorage.egg-info/SOURCES.txt +0 -0
  27. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/pylodstorage.egg-info/dependency_links.txt +0 -0
  28. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/pylodstorage.egg-info/entry_points.txt +0 -0
  29. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/pylodstorage.egg-info/requires.txt +0 -0
  30. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/pylodstorage.egg-info/top_level.txt +0 -0
  31. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/__init__.py +0 -0
  32. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/endpoints.yaml +0 -0
  33. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/formats.yaml +0 -0
  34. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/queries.yaml +0 -0
  35. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/scholia.yaml +0 -0
  36. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/trulytabular.yaml +0 -0
  37. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/wf.yaml +0 -0
  38. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/sampledata/wikidata.yaml +0 -0
  39. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/setup.cfg +0 -0
  40. {pylodstorage-0.2.12 → pylodstorage-0.2.14}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pylodstorage
3
- Version: 0.2.12
3
+ Version: 0.2.14
4
4
  Summary: python List of Dict (Table) Storage library
5
5
  Home-page: https://github.com/WolfgangFahl/pyLodStorage
6
6
  Author: Wolfgang Fahl
@@ -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
- pLabel=f"{reverseToken}wdt:{self.pid}"
33
- return pLabel
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, "label"):
38
- text=f"{self.label} ({self.pid})"
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.pLabel=record["propertyLabel"]
140
+ prop.plabel=record["propertyLabel"]
119
141
  prop.url=url
120
- wdProperties[prop.pLabel]=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.pLabel
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.pLabel}
504
+ statsRow={"property":wdProperty.plabel}
440
505
  total=0
441
506
  nttotal=0
442
507
  maxCount=0
@@ -9,7 +9,7 @@ class Version(object):
9
9
  Version handling for pyLoDStorage
10
10
  '''
11
11
  name="pylodstorage"
12
- version='0.2.12'
12
+ version='0.2.14'
13
13
  date = '2020-09-10'
14
14
  updated = '2022-07-27'
15
15
  description='python List of Dict (Table) Storage library'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pylodstorage
3
- Version: 0.2.12
3
+ Version: 0.2.14
4
4
  Summary: python List of Dict (Table) Storage library
5
5
  Home-page: https://github.com/WolfgangFahl/pyLodStorage
6
6
  Author: Wolfgang Fahl
File without changes
File without changes
File without changes
File without changes