Metaodi has uploaded a new change for review.
https://gerrit.wikimedia.org/r/132629
Change subject: Support unit-less quantity values
......................................................................
Support unit-less quantity values
The class WbQuantity works similar to WbTime. Currently only unit-less
quantities are supported (I couldn't find out how to add proper units).
Values are being 'converted' the way the API expects resp. responds.
Change-Id: I0a81f8c17f5f5b1b977f4d8054acb5a0574816e1
---
M pywikibot/__init__.py
M pywikibot/page.py
M tests/wikibase_tests.py
3 files changed, 53 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/29/132629/1
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index ff2f61a..3396bac 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -312,6 +312,48 @@
u"precision=%(precision)d, before=%(before)d, after=%(after)d, " \
u"timezone=%(timezone)d, calendarmodel='%(calendarmodel)s')" %
self.__dict__
+class WbQuantity(object):
+ """ A Wikibase quantity representation"""
+
+ def __init__(self, amount=None, unit=None, variance=0):
+ """
+ Creates a new WbQuantity object.
+ """
+ if amount is None:
+ raise ValueError('no amount given')
+ if unit is not None and unit != '1':
+ raise NotImplementedError('Currently only unit-less quantities are
supported')
+ if unit is None:
+ unit = '1'
+ self.amount = long(amount)
+ self.variance = variance
+ self.unit = unit
+
+ def toWikibase(self):
+ """
+ Function which converts the data to a JSON object
+ for the Wikibase API.
+ """
+ json = {'amount': self.amount,
+ 'upperBound': self.amount + self.variance,
+ 'lowerBound': self.amount - self.variance,
+ 'unit': self.unit
+ }
+ return json
+
+ @staticmethod
+ def fromWikibase(wb):
+ return WbQuantity(wb[u'amount'], ts[u'unit'], ts[u'variance'])
+
+ def __str__(self):
+ return str(self.toWikibase())
+
+ def __eq__(self, other):
+ return self.__dict__ == other.__dict__
+
+ def __repr__(self):
+ return u"WbQuantity(amount=%(amount)d, variance=%(variance)d,
unit=%(unit)s" % self.__dict__
+
def deprecated(instead=None):
"""Decorator to output a method deprecation warning.
diff --git a/pywikibot/page.py b/pywikibot/page.py
index fe54ef6..db253bd 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -2886,6 +2886,9 @@
elif claim.getType() == 'time':
claim.target = pywikibot.WbTime.fromWikibase(
data['mainsnak']['datavalue']['value'])
+ elif claim.getType() == 'quantity':
+ claim.target = pywikibot.WbTime.fromWikibase(
+ data['mainsnak']['datavalue']['value'])
else:
# This covers string, url types
claim.target = data['mainsnak']['datavalue']['value']
@@ -3069,6 +3072,8 @@
value = self.getTarget().toWikibase()
elif self.getType() == 'time':
value = self.getTarget().toWikibase()
+ elif self.getType() == 'quantity':
+ value = self.getTarget().toWikibase()
else:
raise NotImplementedError('%s datatype is not supported yet.'
% self.getType())
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 4b6af74..4bbc549 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -52,6 +52,12 @@
self.assertRaises(ValueError, pywikibot.WbTime, site=wikidata,
precision=15)
self.assertRaises(ValueError, pywikibot.WbTime, site=wikidata,
precision='invalid_precision')
+ # test WbQuantity
+ q = pywikibot.WbQuantity(amount=1234, variance=1)
+ self.assertEqual(q.toWikibase(), {'amount': 1234, 'lowerBound': 1233,
'upperBound': 1235, 'unit': '1', })
+ self.assertRaises(ValueError, pywikibot.WbQuantity, variance=1)
+ self.assertRaises(NotImplementedError, pywikibot.WbQuantity,
amount=789, unit='invalid_unit')
+
# test WikibasePage.__cmp__
self.assertEqual(pywikibot.ItemPage.fromPage(mainpage),
pywikibot.ItemPage(repo, 'q5296'))
--
To view, visit https://gerrit.wikimedia.org/r/132629
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0a81f8c17f5f5b1b977f4d8054acb5a0574816e1
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Metaodi <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits