jenkins-bot has submitted this change and it was merged.

Change subject: Add and use SparqlElementProvider
......................................................................


Add and use SparqlElementProvider

Bug: T122706
Change-Id: I19fd9f9c18cf17856413eb1753875c91ee591100
---
M app.py
M chemistry.py
2 files changed, 49 insertions(+), 3 deletions(-)

Approvals:
  Ricordisamoa: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/app.py b/app.py
index dddd4cc..9cf9f0b 100644
--- a/app.py
+++ b/app.py
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 """
-Copyright © 2012-2015 Ricordisamoa
+Copyright © 2012-2016 Ricordisamoa
 
 This file is part of the Wikidata periodic table.
 
@@ -36,7 +36,7 @@
 app.json_encoder = CustomJSONEncoder
 
 # May be set to chemistry.ApiElementProvider (slower, but more up-to-date)
-element_provider_class = chemistry.WdqElementProvider
+element_provider_class = chemistry.SparqlElementProvider
 nuclide_provider_class = nuclides.SparqlNuclideProvider
 
 fake_globals = {'isinstance': isinstance, 'sorted': sorted}
diff --git a/chemistry.py b/chemistry.py
index af58b82..9ea7ac7 100644
--- a/chemistry.py
+++ b/chemistry.py
@@ -22,7 +22,7 @@
 from collections import defaultdict
 
 import data
-from base import BaseProvider, PropertyAlreadySetException, TableCell, 
WdqBase, get_json
+from base import BaseProvider, PropertyAlreadySetException, SparqlBase, 
TableCell, WdqBase, get_json
 
 
 class ElementProvider(BaseProvider):
@@ -133,6 +133,52 @@
         }
 
 
+class SparqlElementProvider(SparqlBase, ElementProvider):
+    """Load elements from Wikidata Sparql endpoint."""
+    def __iter__(self):
+        query = 'SELECT ?item ?symbol ?number (group_concat(?subclass_of) as 
?subclass_of) \
+WHERE {{ \
+    ?item wdt:P{symbol_pid} ?symbol . \
+    OPTIONAL {{ \
+        ?item wdt:P{subclass_pid} ?subclass_of \
+    }} \
+    OPTIONAL {{ \
+        ?item wdt:P{number_pid} ?number \
+    }} \
+}} \
+GROUP BY ?item ?symbol ?number'.format(symbol_pid=Element.symbol_pid,
+                                       subclass_pid=Element.subclass_pid,
+                                       number_pid=Element.number_pid)
+        items = self.get_sparql(query)
+        ids = 
[item['item']['value'].replace('http://www.wikidata.org/entity/', '')
+               for item in items]
+        entities = self.get_entities(ids, props='labels',
+                                     languages=self.language, 
languagefallback=1)
+        for item in items:
+            element = Element()
+            element.item_id = 
item['item']['value'].replace('http://www.wikidata.org/entity/', '')
+            if 'number' in item and item['number']['value'].isdigit():
+                element.number = int(item['number']['value'])
+            else:
+                element.number = None
+            if 'symbol' in item:
+                element.symbol = item['symbol']['value']
+            else:
+                element.symbol = None
+            if 'subclass_of' in item:
+                subclass_of = 
[int(subclass_id.replace('http://www.wikidata.org/entity/Q', ''))
+                               for subclass_id in 
item['subclass_of']['value'].split()]
+            else:
+                subclass_of = []
+            element.load_data_from_superclasses(subclass_of)
+            label = None
+            entity = entities.get(element.item_id)
+            if entity and 'labels' in entity and len(entity['labels']) == 1:
+                label = list(entity['labels'].values())[0]['value']
+            element.label = label
+            yield element
+
+
 class ApiElementProvider(ElementProvider):
     """Load elements from the Wikidata API."""
     def __iter__(self):

-- 
To view, visit https://gerrit.wikimedia.org/r/315671
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I19fd9f9c18cf17856413eb1753875c91ee591100
Gerrit-PatchSet: 2
Gerrit-Project: labs/tools/ptable
Gerrit-Branch: master
Gerrit-Owner: Ricordisamoa <ricordisa...@openmailbox.org>
Gerrit-Reviewer: ArthurPSmith <arthurpsm...@gmail.com>
Gerrit-Reviewer: Ricordisamoa <ricordisa...@openmailbox.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to