jenkins-bot has submitted this change and it was merged.
Change subject: (bug 65758) WbQuantity may represent various value types
......................................................................
(bug 65758) WbQuantity may represent various value types
- we get a string from Wikibase; evaluate it to the right
number type.
- closing braket for WbQuantity.__repr__() string
- test suite added
Change-Id: I4dbc8a267fa37ba613001b8dfeb64458e2e287fe
---
M pywikibot/__init__.py
M tests/wikibase_tests.py
2 files changed, 43 insertions(+), 11 deletions(-)
Approvals:
Ricordisamoa: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index c9594e4..390432b 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -331,7 +331,7 @@
raise NotImplementedError('Currently only unit-less quantities are
supported')
if unit is None:
unit = '1'
- self.amount = long(amount)
+ self.amount = amount
self.unit = unit
upperError = lowerError = 0
if isinstance(error, tuple):
@@ -355,11 +355,11 @@
@staticmethod
def fromWikibase(wb):
- amount = long(wb[u'amount'])
- upperBound = long(wb[u'upperBound'])
- lowerBound = long(wb[u'lowerBound'])
+ amount = eval(wb['amount'])
+ upperBound = eval(wb['upperBound'])
+ lowerBound = eval(wb['lowerBound'])
error = (upperBound - amount, amount - lowerBound)
- return WbQuantity(amount, wb[u'unit'], error)
+ return WbQuantity(amount, wb['unit'], error)
def __str__(self):
return str(self.toWikibase())
@@ -368,7 +368,8 @@
return self.__dict__ == other.__dict__
def __repr__(self):
- return u"WbQuantity(amount=%(amount)d, upperBound=%(upperBound)d,
lowerBound=%(lowerBound)d, unit=%(unit)s" % self.__dict__
+ return (u"WbQuantity(amount=%(amount)s, upperBound=%(upperBound)s, "
+ u"lowerBound=%(lowerBound)s, unit=%(unit)s)" % self.__dict__)
def deprecated(instead=None):
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index a8b52e1..0f3cd5c 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -64,14 +64,45 @@
# test WbQuantity
q = pywikibot.WbQuantity(amount=1234, error=1)
- self.assertEqual(q.toWikibase(), {'amount': 1234, 'lowerBound':
1233, 'upperBound': 1235, 'unit': '1', })
+ self.assertEqual(q.toWikibase(),
+ {'amount': 1234, 'lowerBound': 1233,
+ 'upperBound': 1235, 'unit': '1', })
q = pywikibot.WbQuantity(amount=5, error=(2, 3))
- self.assertEqual(q.toWikibase(), {'amount': 5, 'lowerBound': 2,
'upperBound': 7, 'unit': '1', })
- self.assertRaises(ValueError, pywikibot.WbQuantity, amount=None, error=1)
- self.assertRaises(NotImplementedError, pywikibot.WbQuantity, amount=789,
unit='invalid_unit')
+ self.assertEqual(q.toWikibase(),
+ {'amount': 5, 'lowerBound': 2,
'upperBound': 7,
+ 'unit': '1', })
+ q = pywikibot.WbQuantity(amount=0.044405586)
+ self.assertEqual(q.toWikibase(),
+ {'amount': 0.044405586, 'lowerBound':
0.044405586,
+ 'upperBound': 0.044405586, 'unit': '1',
})
+ # test other WbQuantity methods
+ self.assertEqual("%s" % q,
+ "{'amount': 0.044405586, 'lowerBound':
0.044405586, "
+ "'unit': '1', 'upperBound':
0.044405586}")
+ self.assertEqual("%r" % q,
+ "WbQuantity(amount=0.044405586, "
+ "upperBound=0.044405586, lowerBound=0.044405586, "
+ "unit=1)")
+ self.assertEqual(q, q)
+
+ # test WbQuantity.fromWikibase() instantiating
+ q = pywikibot.WbQuantity.fromWikibase({u'amount': u'+0.0229',
+ u'lowerBound': u'0',
+ u'upperBound': u'1',
+ u'unit': u'1'})
+ self.assertEqual(q.toWikibase(),
+ {'amount': 0.0229, 'lowerBound': 0,
'upperBound': 1,
+ 'unit': '1', })
+
+ # test WbQuantity error handling
+ self.assertRaises(ValueError, pywikibot.WbQuantity, amount=None,
+ error=1)
+ self.assertRaises(NotImplementedError, pywikibot.WbQuantity, amount=789,
+ unit='invalid_unit')
# test WikibasePage.__cmp__
- self.assertEqual(pywikibot.ItemPage.fromPage(mainpage), pywikibot.ItemPage(repo,
'q5296'))
+ self.assertEqual(pywikibot.ItemPage.fromPage(mainpage),
+ pywikibot.ItemPage(repo, 'q5296'))
def testItemPageExtensionability(self):
class MyItemPage(pywikibot.ItemPage):
--
To view, visit
https://gerrit.wikimedia.org/r/135397
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I4dbc8a267fa37ba613001b8dfeb64458e2e287fe
Gerrit-PatchSet: 6
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Legoktm <legoktm.wikipedia(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Ricordisamoa <ricordisamoa(a)openmailbox.org>
Gerrit-Reviewer: Russell Blau <russblau(a)imapmail.org>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>