Hashar has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/202222

Change subject: Abstract WbTime and WbQuantity
......................................................................

Abstract WbTime and WbQuantity

The Wikibase classes for WbTime and WbQuantity are quite similar and I
suspect the framework will need more or them as Wikibase evolves.

Introduce the 'abstract' class WbRepresentation to factor out some
common code (__str__ __eq__) and document the expected entry point,
should help future implementations.

Make WbTime and WbQuantity to inherit the new WbRepresentation, thus
dropping duplicates __str__ and __eq__.

Change-Id: I4e23a26813b43feca7fbd0655b47d4744e36a1d9
---
M pywikibot/__init__.py
1 file changed, 26 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/22/202222/1

diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index d4b3081..1a23ea2 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -320,7 +320,31 @@
         raise NotImplementedError
 
 
-class WbTime(object):
+class WbRepresentation(object):
+
+    """Abstract class for Wikibase representations"""
+
+    def __init__(self):
+        raise NotImplementedError
+
+    def toWikibase(self):
+        """Convert representation to JSON for the Wikibase API"""
+        raise NotImplementedError
+
+    @classmethod
+    def fromWikibase(cls, json):
+        """Create a representation object based on JSON from Wikibase API"""
+        raise NotImplementedError
+
+    def __str__(self):
+        return json.dumps(self.toWikibase(), indent=4, sort_keys=True,
+                          separators=(',', ': '))
+
+    def __eq__(self, other):
+        return self.__dict__ == other.__dict__
+
+
+class WbTime(WbRepresentation):
 
     """A Wikibase time representation."""
 
@@ -439,13 +463,6 @@
                                ts[u'before'], ts[u'after'],
                                ts[u'timezone'], ts[u'calendarmodel'])
 
-    def __str__(self):
-        return json.dumps(self.toWikibase(), indent=4, sort_keys=True,
-                          separators=(',', ': '))
-
-    def __eq__(self, other):
-        return self.__dict__ == other.__dict__
-
     def __repr__(self):
         return u"WbTime(year=%(year)d, month=%(month)d, day=%(day)d, " \
             u"hour=%(hour)d, minute=%(minute)d, second=%(second)d, " \
@@ -454,7 +471,7 @@
             % self.__dict__
 
 
-class WbQuantity(object):
+class WbQuantity(WbRepresentation):
 
     """A Wikibase quantity representation."""
 
@@ -507,13 +524,6 @@
         lowerBound = eval(wb['lowerBound'])
         error = (upperBound - amount, amount - lowerBound)
         return cls(amount, wb['unit'], error)
-
-    def __str__(self):
-        return json.dumps(self.toWikibase(), indent=4, sort_keys=True,
-                          separators=(',', ': '))
-
-    def __eq__(self, other):
-        return self.__dict__ == other.__dict__
 
     def __repr__(self):
         return (u"WbQuantity(amount=%(amount)s, upperBound=%(upperBound)s, "

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4e23a26813b43feca7fbd0655b47d4744e36a1d9
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Hashar <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to