Tobias47n9e has uploaded a new change for review.
https://gerrit.wikimedia.org/r/246791
Change subject: Return numbers in WbQuantity-json-object as strings with sign
......................................................................
Return numbers in WbQuantity-json-object as strings with sign
The 'to Wikibase()' function currently returns a dictionary
where the values for the keys 'amount', 'upperBound' and
'lowerBound' are returned as int or float. This caused the
numbers to be deserialized into very long floats
(>50 digits) on Wikidata. These long numbers make the diffs
hard to read, and the values hard to read and edit.
The patch introduces a function that converts the numbers
to string and adds the appropriate sign to the number. So
14.1 becomes '+14.1' and -14.1 becomes '-14.1'.
Change-Id: Ib54be2898a035a99592cba1298f354bd1637a6b5
---
M pywikibot/__init__.py
1 file changed, 22 insertions(+), 3 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/91/246791/1
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index ba3aae3..623cdf6 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -507,11 +507,30 @@
self.upperBound = self.amount + upperError
self.lowerBound = self.amount - lowerError
+ def preformat_float(self, number):
+ """
+ Returns a number as a string with sign: 14.1 -> '+14.1'
+
+ In order to send cleanly formatted quantities to Wikidata
+ (diffs and editing) numbers should not be send as floats or
+ ints, but strings with sign. For positive numbers 14.1
+ is returned as '+14.1' and for -14.1 the function
+ returns '-14.1'.
+
+ @param number: a number that needs to be preformated
+ @type number: float, int
+ """
+ if number < 0:
+ num_str = '-{}'.format(str(number))
+ else:
+ num_str = '+{}'.format(str(number))
+ return num_str
+
def toWikibase(self):
"""Convert the data to a JSON object for the Wikibase API."""
- json = {'amount': self.amount,
- 'upperBound': self.upperBound,
- 'lowerBound': self.lowerBound,
+ json = {'amount': self.preformat_float(self.amount),
+ 'upperBound': self.preformat_float(self.upperBound),
+ 'lowerBound': self.preformat_float(self.lowerBound),
'unit': self.unit
}
return json
--
To view, visit https://gerrit.wikimedia.org/r/246791
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib54be2898a035a99592cba1298f354bd1637a6b5
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Tobias47n9e <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits