pyLodStorage 0.3.0__tar.gz → 0.3.2__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.3.0/pylodstorage.egg-info → pylodstorage-0.3.2}/PKG-INFO +1 -1
  2. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/trulytabular.py +73 -36
  3. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/version.py +2 -2
  4. {pylodstorage-0.3.0 → pylodstorage-0.3.2/pylodstorage.egg-info}/PKG-INFO +1 -1
  5. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/sampledata/trulytabular.yaml +6 -1
  6. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/LICENSE +0 -0
  7. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/README.md +0 -0
  8. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/__init__.py +0 -0
  9. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/csv.py +0 -0
  10. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/entity.py +0 -0
  11. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/jsonable.py +0 -0
  12. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/jsonpicklemixin.py +0 -0
  13. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/lod.py +0 -0
  14. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/mwTable.py +0 -0
  15. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/plot.py +0 -0
  16. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/query.py +0 -0
  17. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/querymain.py +0 -0
  18. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/sample.py +0 -0
  19. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/schema.py +0 -0
  20. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/sparql.py +0 -0
  21. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/sql.py +0 -0
  22. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/storageconfig.py +0 -0
  23. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/tabulateCounter.py +0 -0
  24. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/uml.py +0 -0
  25. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/xml.py +0 -0
  26. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/lodstorage/yamlablemixin.py +0 -0
  27. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/pylodstorage.egg-info/SOURCES.txt +0 -0
  28. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/pylodstorage.egg-info/dependency_links.txt +0 -0
  29. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/pylodstorage.egg-info/entry_points.txt +0 -0
  30. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/pylodstorage.egg-info/requires.txt +0 -0
  31. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/pylodstorage.egg-info/top_level.txt +0 -0
  32. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/sampledata/__init__.py +0 -0
  33. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/sampledata/endpoints.yaml +0 -0
  34. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/sampledata/formats.yaml +0 -0
  35. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/sampledata/queries.yaml +0 -0
  36. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/sampledata/scholia.yaml +0 -0
  37. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/sampledata/wf.yaml +0 -0
  38. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/sampledata/wikidata.yaml +0 -0
  39. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/setup.cfg +0 -0
  40. {pylodstorage-0.3.0 → pylodstorage-0.3.2}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pylodstorage
3
- Version: 0.3.0
3
+ Version: 0.3.2
4
4
  Summary: python List of Dict (Table) Storage library
5
5
  Home-page: https://github.com/WolfgangFahl/pyLodStorage
6
6
  Author: Wolfgang Fahl
@@ -194,7 +194,16 @@ class WikidataItem:
194
194
  return text
195
195
 
196
196
  @classmethod
197
- 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"):
198
207
  '''
199
208
  get the label for the given item and language
200
209
 
@@ -205,23 +214,19 @@ class WikidataItem:
205
214
  Returns:
206
215
  (str,str): the label and description as a tuple
207
216
  '''
208
- query="""
209
- # get the label for the given item
210
- PREFIX wd: <http://www.wikidata.org/entity/>
211
- PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
212
- PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
213
- PREFIX schema: <http://schema.org/>
217
+ query=f"""# get the label for the given item
218
+ {cls.getPrefixes()}
214
219
  SELECT ?itemLabel ?itemDescription
215
220
  WHERE
216
- {
217
- VALUES ?item {
218
- wd:%s
219
- }
221
+ {{
222
+ VALUES ?item {{
223
+ wd:{itemId}
224
+ }}
220
225
  ?item rdfs:label ?itemLabel.
221
- filter (lang(?itemLabel) = "%s").
226
+ FILTER (lang(?itemLabel) = "{lang}").
222
227
  ?item schema:description ?itemDescription.
223
- FILTER((LANG(?itemDescription)) = "%s")
224
- }""" % (itemId,lang,lang)
228
+ FILTER((LANG(?itemDescription)) = "{lang}")
229
+ }}"""
225
230
  return sparql.getValues(query, ["itemLabel","itemDescription"])
226
231
 
227
232
  @classmethod
@@ -238,23 +243,20 @@ WHERE
238
243
  a list of potential items
239
244
  '''
240
245
  valuesClause=f' "{itemLabel}"@{lang}\n'
241
- 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
242
247
  # e.g. we'll find human=Q5 as the oldest type for the label "human" first
243
248
  # and then the newer ones such as "race in Warcraft"
244
- PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
245
- PREFIX schema: <http://schema.org/>
246
- PREFIX wd: <http://www.wikidata.org/entity/>
247
- PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
249
+ {cls.getPrefixes()}
248
250
  SELECT ?itemId ?item ?itemLabel ?itemDescription
249
- WHERE {
250
- VALUES ?itemLabel {
251
- %s
252
- }
251
+ WHERE {{
252
+ VALUES ?itemLabel {{
253
+ {valuesClause}
254
+ }}
253
255
  BIND (xsd:integer(SUBSTR(STR(?item),33)) AS ?itemId)
254
256
  ?item rdfs:label ?itemLabel.
255
257
  ?item schema:description ?itemDescription
256
- FILTER(LANG(?itemDescription)="%s")
257
- } ORDER BY ?itemId""" % (valuesClause,lang)
258
+ FILTER(LANG(?itemDescription)="{lang}")
259
+ }} ORDER BY ?itemId"""
258
260
 
259
261
  qLod=sparql.queryAsListOfDicts(query)
260
262
  items=[]
@@ -318,8 +320,7 @@ class TrulyTabular(object):
318
320
  itemText=self.getItemText()
319
321
  query=f"""# Count all items with the given type
320
322
  # {itemText}
321
- PREFIX wd: <http://www.wikidata.org/entity/>
322
- PREFIX wdt: <http://www.wikidata.org/prop/direct/>
323
+ {WikidataItem.getPrefixes()}
323
324
  SELECT (COUNT (DISTINCT ?item) AS ?count)
324
325
  WHERE
325
326
  {{
@@ -350,7 +351,7 @@ WHERE
350
351
  def getItemText(self):
351
352
  # leads to 405 Method not allowed in SPARQLWrapper under certain circumstances
352
353
  # itemText=self.asText(long=True)
353
- itemText=self.itemQid
354
+ itemText=f"{self.itemQid}:{self.item.qlabel}"
354
355
  return itemText
355
356
 
356
357
  @classmethod
@@ -395,9 +396,7 @@ WHERE
395
396
  sparqlQuery=f"""# truly tabular {naiveText} query for
396
397
  # {item.qid}:{item.qlabel}
397
398
  # generated by {script} version {Version.version} on {self.isodate}
398
- PREFIX wd: <http://www.wikidata.org/entity/>
399
- PREFIX wdt: <http://www.wikidata.org/prop/direct/>
400
- PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
399
+ {WikidataItem.getPrefixes()}
401
400
  SELECT ?{item.varname} ?{item.varname}Label"""
402
401
  # loop over all properties
403
402
  for wdProp in self.properties.values():
@@ -408,13 +407,17 @@ SELECT ?{item.varname} ?{item.varname}Label"""
408
407
  genList=genMap[wdProp.pid]
409
408
  for aggregate in genList:
410
409
  if not aggregate in ["ignore","label"]:
410
+ distinct=""
411
411
  if aggregate=="list":
412
412
  aggregateFunc="GROUP_CONCAT"
413
413
  aggregateParam=f';SEPARATOR="{listSeparator}"'
414
+ distinct="DISTINCT "
414
415
  else:
416
+ if aggregate=="count":
417
+ distinct="DISTINCT "
415
418
  aggregateFunc=aggregate.upper()
416
419
  aggregateParam=""
417
- sparqlQuery+=f"\n ({aggregateFunc} (?{wdProp.varname}{aggregateParam}) AS ?{wdProp.varname}_{aggregate})"
420
+ sparqlQuery+=f"\n ({aggregateFunc} ({distinct}?{wdProp.varname}{aggregateParam}) AS ?{wdProp.varname}_{aggregate})"
418
421
  elif aggregate=="label":
419
422
  sparqlQuery+=f"\n ?{wdProp.varname}Label"
420
423
  sparqlQuery+=f"""
@@ -440,11 +443,20 @@ WHERE {{
440
443
  # optionally add Aggregate
441
444
  if not naive:
442
445
  sparqlQuery+=f"GROUP BY ?{item.varname} ?{item.varname}Label"
446
+ havingCount=0
447
+ havingDelim=" "
443
448
  for wdProp in self.properties.values():
444
449
  if wdProp.pid in genMap:
445
450
  genList=genMap[wdProp.pid]
446
451
  if "ignore" in genList:
447
- sparqlQuery+=f"\nHAVING (COUNT(?{wdProp.varname})=1)"
452
+ havingCount+=1
453
+ if havingCount==1:
454
+ sparqlQuery+=f"\nHAVING ("
455
+
456
+ sparqlQuery+=f"\n {havingDelim}COUNT(?{wdProp.varname})=1"
457
+ havingDelim="&& "
458
+ if havingCount>0:
459
+ sparqlQuery+=f"\n)"
448
460
  return sparqlQuery
449
461
 
450
462
  def mostFrequentPropertiesQuery(self,whereClause:str=None):
@@ -454,14 +466,39 @@ WHERE {{
454
466
  Args:
455
467
  whereClause(str): an extra WhereClause to use
456
468
  '''
457
- query=self.queryManager.queriesByName["mostFrequentProperties"]
458
469
  if whereClause is None:
459
470
  whereClause=f"?item wdt:P31 wd:{self.itemQid};";
460
471
  else:
461
472
  whereClause+=";"
462
473
  itemText=self.getItemText()
463
- query.title=f"most frequently used properties for {self.item.asText(long=True)}"
464
- query.query=query.query % (itemText,whereClause,self.lang)
474
+ if self.endpoint=="https://query.wikidata.org/sparql":
475
+ optimizerHint=' hint:Query hint:optimizer "None".\n'
476
+ else:
477
+ optimizerHint=''
478
+ sparqlQuery=f"""# get the most frequently used properties for
479
+ # {itemText}
480
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
481
+ PREFIX schema: <http://schema.org/>
482
+ PREFIX wd: <http://www.wikidata.org/entity/>
483
+ PREFIX wdt: <http://www.wikidata.org/prop/direct/>
484
+ PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
485
+ SELECT ?prop ?propLabel ?count WHERE {{
486
+ {{
487
+ SELECT ?prop (COUNT(DISTINCT ?item) AS ?count) WHERE {{
488
+ {optimizerHint}
489
+ {whereClause}
490
+ ?p ?id.
491
+ ?prop wikibase:directClaim ?p.
492
+ }}
493
+ GROUP BY ?prop ?propLabel
494
+ }}
495
+ ?prop rdfs:label ?propLabel
496
+ FILTER(LANG(?propLabel) = "{self.lang}")
497
+ }}
498
+ ORDER BY DESC (?count)
499
+ """
500
+ title=f"most frequently used properties for {self.item.asText(long=True)}"
501
+ query=Query(name=f"mostFrequentProperties for {itemText}",query=sparqlQuery,title=title)
465
502
  return query
466
503
 
467
504
  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.3.0'
12
+ version='0.3.2'
13
13
  date = '2020-09-10'
14
- updated = '2022-07-28'
14
+ updated = '2022-07-30'
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.3.0
3
+ Version: 0.3.2
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