jenkins-bot has submitted this change and it was merged.
Change subject: Abstract Coordinate, WbTime and WbQuantity ......................................................................
Abstract Coordinate, WbTime and WbQuantity
The Wikibase classes for Coordinate, WbTime and WbQuantity are quite similar and 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 Coordinate, WbTime and WbQuantity to inherit the new WbRepresentation, thus dropping duplicates __str__ and __eq__.
Change-Id: I4e23a26813b43feca7fbd0655b47d4744e36a1d9 --- M pywikibot/__init__.py A pywikibot/_wbtypes.py 2 files changed, 43 insertions(+), 18 deletions(-)
Approvals: John Vandenberg: Looks good to me, but someone else must approve XZise: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py index d4b3081..aeafb83 100644 --- a/pywikibot/__init__.py +++ b/pywikibot/__init__.py @@ -13,7 +13,6 @@ import re import sys import threading -import json
if sys.version_info[0] > 2: from queue import Queue @@ -192,7 +191,10 @@ return newdt
-class Coordinate(object): +from pywikibot._wbtypes import WbRepresentation as _WbRepresentation + + +class Coordinate(_WbRepresentation):
""" Class for handling and storing Coordinates. @@ -320,7 +322,7 @@ raise NotImplementedError
-class WbTime(object): +class WbTime(_WbRepresentation):
"""A Wikibase time representation."""
@@ -439,13 +441,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 +449,7 @@ % self.__dict__
-class WbQuantity(object): +class WbQuantity(_WbRepresentation):
"""A Wikibase quantity representation."""
@@ -507,13 +502,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, " diff --git a/pywikibot/_wbtypes.py b/pywikibot/_wbtypes.py new file mode 100644 index 0000000..7175cdd --- /dev/null +++ b/pywikibot/_wbtypes.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +"""Wikibase data type classes.""" +# +# (C) Pywikibot team, 2013-2015 +# +# Distributed under the terms of the MIT license. +# +from __future__ import absolute_import, unicode_literals + +__version__ = '$Id$' +# + +import json + + +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__
pywikibot-commits@lists.wikimedia.org