pyLodStorage 0.2.14__tar.gz → 0.3.1__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.14/pylodstorage.egg-info → pylodstorage-0.3.1}/PKG-INFO +1 -1
  2. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/trulytabular.py +110 -42
  3. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/version.py +2 -2
  4. {pylodstorage-0.2.14 → pylodstorage-0.3.1/pylodstorage.egg-info}/PKG-INFO +1 -1
  5. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/sampledata/trulytabular.yaml +6 -1
  6. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/LICENSE +0 -0
  7. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/README.md +0 -0
  8. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/__init__.py +0 -0
  9. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/csv.py +0 -0
  10. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/entity.py +0 -0
  11. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/jsonable.py +0 -0
  12. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/jsonpicklemixin.py +0 -0
  13. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/lod.py +0 -0
  14. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/mwTable.py +0 -0
  15. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/plot.py +0 -0
  16. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/query.py +0 -0
  17. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/querymain.py +0 -0
  18. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/sample.py +0 -0
  19. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/schema.py +0 -0
  20. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/sparql.py +0 -0
  21. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/sql.py +0 -0
  22. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/storageconfig.py +0 -0
  23. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/tabulateCounter.py +0 -0
  24. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/uml.py +0 -0
  25. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/xml.py +0 -0
  26. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/lodstorage/yamlablemixin.py +0 -0
  27. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/pylodstorage.egg-info/SOURCES.txt +0 -0
  28. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/pylodstorage.egg-info/dependency_links.txt +0 -0
  29. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/pylodstorage.egg-info/entry_points.txt +0 -0
  30. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/pylodstorage.egg-info/requires.txt +0 -0
  31. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/pylodstorage.egg-info/top_level.txt +0 -0
  32. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/sampledata/__init__.py +0 -0
  33. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/sampledata/endpoints.yaml +0 -0
  34. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/sampledata/formats.yaml +0 -0
  35. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/sampledata/queries.yaml +0 -0
  36. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/sampledata/scholia.yaml +0 -0
  37. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/sampledata/wf.yaml +0 -0
  38. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/sampledata/wikidata.yaml +0 -0
  39. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/setup.cfg +0 -0
  40. {pylodstorage-0.2.14 → pylodstorage-0.3.1}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pylodstorage
3
- Version: 0.2.14
3
+ Version: 0.3.1
4
4
  Summary: python List of Dict (Table) Storage library
5
5
  Home-page: https://github.com/WolfgangFahl/pyLodStorage
6
6
  Author: Wolfgang Fahl
@@ -6,11 +6,13 @@ Created on 2022-04-14
6
6
  import datetime
7
7
  from lodstorage.sparql import SPARQL
8
8
  from lodstorage.query import Query,QueryManager,YamlPath
9
+ from lodstorage.version import Version
9
10
  from pathlib import Path
10
11
  import os
11
12
  import textwrap
12
13
  import re
13
14
 
15
+
14
16
  class Variable:
15
17
  '''
16
18
  Variable e.g. name handling
@@ -192,7 +194,16 @@ class WikidataItem:
192
194
  return text
193
195
 
194
196
  @classmethod
195
- def getLabelAndDescription(self,sparql:SPARQL, itemId:str,lang:str="en"):
197
+ def getPrefixes(cls):
198
+ prefixes="""PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
199
+ PREFIX schema: <http://schema.org/>
200
+ PREFIX wd: <http://www.wikidata.org/entity/>
201
+ PREFIX wdt: <http://www.wikidata.org/prop/direct/>
202
+ PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"""
203
+ return prefixes
204
+
205
+ @classmethod
206
+ def getLabelAndDescription(cls,sparql:SPARQL, itemId:str,lang:str="en"):
196
207
  '''
197
208
  get the label for the given item and language
198
209
 
@@ -203,23 +214,19 @@ class WikidataItem:
203
214
  Returns:
204
215
  (str,str): the label and description as a tuple
205
216
  '''
206
- query="""
207
- # get the label for the given item
208
- PREFIX wd: <http://www.wikidata.org/entity/>
209
- PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
210
- PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
211
- PREFIX schema: <http://schema.org/>
217
+ query=f"""# get the label for the given item
218
+ {cls.getPrefixes()}
212
219
  SELECT ?itemLabel ?itemDescription
213
220
  WHERE
214
- {
215
- VALUES ?item {
216
- wd:%s
217
- }
221
+ {{
222
+ VALUES ?item {{
223
+ wd:{itemId}
224
+ }}
218
225
  ?item rdfs:label ?itemLabel.
219
- filter (lang(?itemLabel) = "%s").
226
+ FILTER (lang(?itemLabel) = "{lang}").
220
227
  ?item schema:description ?itemDescription.
221
- FILTER((LANG(?itemDescription)) = "%s")
222
- }""" % (itemId,lang,lang)
228
+ FILTER((LANG(?itemDescription)) = "{lang}")
229
+ }}"""
223
230
  return sparql.getValues(query, ["itemLabel","itemDescription"])
224
231
 
225
232
  @classmethod
@@ -236,23 +243,20 @@ WHERE
236
243
  a list of potential items
237
244
  '''
238
245
  valuesClause=f' "{itemLabel}"@{lang}\n'
239
- query="""# get the items that have the given label in the given language
246
+ query=f"""# get the items that have the given label in the given language
240
247
  # e.g. we'll find human=Q5 as the oldest type for the label "human" first
241
248
  # and then the newer ones such as "race in Warcraft"
242
- PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
243
- PREFIX schema: <http://schema.org/>
244
- PREFIX wd: <http://www.wikidata.org/entity/>
245
- PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
249
+ {cls.getPrefixes()}
246
250
  SELECT ?itemId ?item ?itemLabel ?itemDescription
247
- WHERE {
248
- VALUES ?itemLabel {
249
- %s
250
- }
251
+ WHERE {{
252
+ VALUES ?itemLabel {{
253
+ {valuesClause}
254
+ }}
251
255
  BIND (xsd:integer(SUBSTR(STR(?item),33)) AS ?itemId)
252
256
  ?item rdfs:label ?itemLabel.
253
257
  ?item schema:description ?itemDescription
254
- FILTER(LANG(?itemDescription)="%s")
255
- } ORDER BY ?itemId""" % (valuesClause,lang)
258
+ FILTER(LANG(?itemDescription)="{lang}")
259
+ }} ORDER BY ?itemId"""
256
260
 
257
261
  qLod=sparql.queryAsListOfDicts(query)
258
262
  items=[]
@@ -316,8 +320,7 @@ class TrulyTabular(object):
316
320
  itemText=self.getItemText()
317
321
  query=f"""# Count all items with the given type
318
322
  # {itemText}
319
- PREFIX wd: <http://www.wikidata.org/entity/>
320
- PREFIX wdt: <http://www.wikidata.org/prop/direct/>
323
+ {WikidataItem.getPrefixes()}
321
324
  SELECT (COUNT (DISTINCT ?item) AS ?count)
322
325
  WHERE
323
326
  {{
@@ -348,7 +351,7 @@ WHERE
348
351
  def getItemText(self):
349
352
  # leads to 405 Method not allowed in SPARQLWrapper under certain circumstances
350
353
  # itemText=self.asText(long=True)
351
- itemText=self.itemQid
354
+ itemText=f"{self.itemQid}:{self.item.qlabel}"
352
355
  return itemText
353
356
 
354
357
  @classmethod
@@ -368,43 +371,83 @@ WHERE
368
371
  return qm
369
372
  return None
370
373
 
371
-
372
- def generateSparqlQuery(self,lang:str='en',naive:bool=True)->str:
374
+ def generateSparqlQuery(self,genMap:dict,listSeparator:str="⇹",naive:bool=True,lang:str='en')->str:
373
375
  '''
374
376
  generate a SPARQL Query
377
+
378
+ Args:
379
+ genMap(dict): a dictionary of generation items aggregates/ignores/labels
380
+ listSeparator(str): the symbole to use as a list separator for GROUP_CONCAT
381
+ naive(bool): if True - generate a naive straight forward SPARQL query
382
+ if False generate a proper truly tabular aggregate query
383
+ lang(str): the language to generate for
384
+
385
+ Returns:
386
+ str: the generated SPARQL Query
375
387
  '''
388
+ # The Wikidata item to generate the query for
376
389
  item=self.item
390
+ # the name of this script
377
391
  script=Path(__file__).name
392
+ # the mode of generation
378
393
  naiveText="naive" if naive else "aggregate"
394
+ # start with th preamble and PREFIX section
395
+ # select the item and it's label
379
396
  sparqlQuery=f"""# truly tabular {naiveText} query for
380
397
  # {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#>
398
+ # generated by {script} version {Version.version} on {self.isodate}
399
+ {WikidataItem.getPrefixes()}
385
400
  SELECT ?{item.varname} ?{item.varname}Label"""
401
+ # loop over all properties
386
402
  for wdProp in self.properties.values():
387
403
  if naive:
388
404
  sparqlQuery+=f"\n ?{wdProp.varname}"
389
405
  else:
390
- sparqlQuery+=f"\n (SAMPLE (?{wdProp.varname}) AS ?{wdProp.varname})"
406
+ if wdProp.pid in genMap:
407
+ genList=genMap[wdProp.pid]
408
+ for aggregate in genList:
409
+ if not aggregate in ["ignore","label"]:
410
+ distinct=""
411
+ if aggregate=="list":
412
+ aggregateFunc="GROUP_CONCAT"
413
+ aggregateParam=f';SEPARATOR="{listSeparator}"'
414
+ distinct="DISTINCT "
415
+ else:
416
+ if aggregate=="count":
417
+ distinct="DISTINCT "
418
+ aggregateFunc=aggregate.upper()
419
+ aggregateParam=""
420
+ sparqlQuery+=f"\n ({aggregateFunc} ({distinct}?{wdProp.varname}{aggregateParam}) AS ?{wdProp.varname}_{aggregate})"
421
+ elif aggregate=="label":
422
+ sparqlQuery+=f"\n ?{wdProp.varname}Label"
391
423
  sparqlQuery+=f"""
392
424
  WHERE {{
393
425
  # instanceof {item.qid}:{item.qlabel}
394
426
  ?{item.varname} wdt:P31 wd:{item.qid}.
395
427
  # label
396
- ?{item.varname} rdfs:label ?{item.varname}Label
428
+ ?{item.varname} rdfs:label ?{item.varname}Label.
397
429
  FILTER (LANG(?{item.varname}Label) = "{lang}").
398
430
  """
399
431
  for wdProp in self.properties.values():
400
- sparqlQuery+=f""" # {wdProp}
401
- OPTIONAL {{ ?{item.varname} wdt:{wdProp.pid} ?{wdProp.varname}. }}
402
- """
432
+ sparqlQuery+=f""" # {wdProp}
433
+ OPTIONAL {{
434
+ ?{item.varname} wdt:{wdProp.pid} ?{wdProp.varname}. """
435
+ if wdProp.pid in genMap:
436
+ genList=genMap[wdProp.pid]
437
+ if "label" in genList:
438
+ sparqlQuery+=f"""\n ?{wdProp.varname} rdfs:label ?{wdProp.varname}Label."""
439
+ sparqlQuery+=f"""\n FILTER (LANG(?{wdProp.varname}Label) = "{lang}")."""
440
+ sparqlQuery+="\n }\n"
403
441
  # close where Clause
404
442
  sparqlQuery+="""}\n"""
405
443
  # optionally add Aggregate
406
444
  if not naive:
407
445
  sparqlQuery+=f"GROUP BY ?{item.varname} ?{item.varname}Label"
446
+ for wdProp in self.properties.values():
447
+ if wdProp.pid in genMap:
448
+ genList=genMap[wdProp.pid]
449
+ if "ignore" in genList:
450
+ sparqlQuery+=f"\nHAVING (COUNT(?{wdProp.varname})=1)"
408
451
  return sparqlQuery
409
452
 
410
453
  def mostFrequentPropertiesQuery(self,whereClause:str=None):
@@ -414,14 +457,39 @@ WHERE {{
414
457
  Args:
415
458
  whereClause(str): an extra WhereClause to use
416
459
  '''
417
- query=self.queryManager.queriesByName["mostFrequentProperties"]
418
460
  if whereClause is None:
419
461
  whereClause=f"?item wdt:P31 wd:{self.itemQid};";
420
462
  else:
421
463
  whereClause+=";"
422
464
  itemText=self.getItemText()
423
- query.title=f"most frequently used properties for {self.item.asText(long=True)}"
424
- query.query=query.query % (itemText,whereClause,self.lang)
465
+ if self.endpoint=="https://query.wikidata.org/sparql":
466
+ optimizerHint=' hint:Query hint:optimizer "None".\n'
467
+ else:
468
+ optimizerHint=''
469
+ sparqlQuery=f"""# get the most frequently used properties for
470
+ # {itemText}
471
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
472
+ PREFIX schema: <http://schema.org/>
473
+ PREFIX wd: <http://www.wikidata.org/entity/>
474
+ PREFIX wdt: <http://www.wikidata.org/prop/direct/>
475
+ PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
476
+ SELECT ?prop ?propLabel ?count WHERE {{
477
+ {{
478
+ SELECT ?prop (COUNT(DISTINCT ?item) AS ?count) WHERE {{
479
+ {optimizerHint}
480
+ {whereClause}
481
+ ?p ?id.
482
+ ?prop wikibase:directClaim ?p.
483
+ }}
484
+ GROUP BY ?prop ?propLabel
485
+ }}
486
+ ?prop rdfs:label ?propLabel
487
+ FILTER(LANG(?propLabel) = "{self.lang}")
488
+ }}
489
+ ORDER BY DESC (?count)
490
+ """
491
+ title=f"most frequently used properties for {self.item.asText(long=True)}"
492
+ query=Query(name=f"mostFrequentProperties for {itemText}",query=sparqlQuery,title=title)
425
493
  return query
426
494
 
427
495
  def noneTabularQuery(self,wdProperty:WikidataProperty,asFrequency:bool=True):
@@ -9,7 +9,7 @@ class Version(object):
9
9
  Version handling for pyLoDStorage
10
10
  '''
11
11
  name="pylodstorage"
12
- version='0.2.14'
12
+ version='0.3.1'
13
13
  date = '2020-09-10'
14
- updated = '2022-07-27'
14
+ updated = '2022-07-28'
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.14
3
+ Version: 0.3.1
4
4
  Summary: python List of Dict (Table) Storage library
5
5
  Home-page: https://github.com/WolfgangFahl/pyLodStorage
6
6
  Author: Wolfgang Fahl
@@ -6,10 +6,15 @@
6
6
  sparql: |
7
7
  # get the most frequently used properties for
8
8
  # %s
9
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
10
+ PREFIX schema: <http://schema.org/>
11
+ PREFIX wd: <http://www.wikidata.org/entity/>
12
+ PREFIX wdt: <http://www.wikidata.org/prop/direct/>
13
+ PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
9
14
  SELECT ?prop ?propLabel ?count WHERE {
10
15
  {
11
16
  SELECT ?prop (COUNT(DISTINCT ?item) AS ?count) WHERE {
12
- hint:Query hint:optimizer "None".
17
+ #hint:Query hint:optimizer "None".
13
18
  %s
14
19
  ?p ?id.
15
20
  ?prop wikibase:directClaim ?p.
File without changes
File without changes
File without changes
File without changes