jenkins-bot has submitted this change and it was merged.
Change subject: Implement generic WbRepresentation.__repr__ ......................................................................
Implement generic WbRepresentation.__repr__
Change-Id: I4107523c32a83b72899b954c3b514eb37d95b84d --- M pywikibot/__init__.py M pywikibot/_wbtypes.py 2 files changed, 18 insertions(+), 18 deletions(-)
Approvals: XZise: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py index 8261c9c..46bdfe7 100644 --- a/pywikibot/__init__.py +++ b/pywikibot/__init__.py @@ -227,6 +227,8 @@ in the future we can use it for the GeoData extension. """
+ _items = ('lat', 'lon', 'globe') + def __init__(self, lat, lon, alt=None, precision=None, globe='earth', typ="", name="", dim=None, site=None, entity=''): """ @@ -265,13 +267,6 @@ self.site = Site().data_repository() else: self.site = site - - def __repr__(self): - string = 'Coordinate(%s, %s' % (self.lat, self.lon) - if self.globe != 'earth': - string += ', globe="%s"' % self.globe - string += ')' - return string
@property def entity(self): @@ -369,6 +364,9 @@
FORMATSTR = '{0:+012d}-{1:02d}-{2:02d}T{3:02d}:{4:02d}:{5:02d}Z'
+ _items = ('year', 'month', 'day', 'hour', 'minute', 'second', + 'precision', 'before', 'after', 'timezone', 'calendarmodel') + def __init__(self, year=None, month=None, day=None, hour=None, minute=None, second=None, precision=None, before=0, after=0, @@ -465,17 +463,12 @@ ts[u'before'], ts[u'after'], ts[u'timezone'], ts[u'calendarmodel'])
- 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, " \ - u"precision=%(precision)d, before=%(before)d, after=%(after)d, " \ - u"timezone=%(timezone)d, calendarmodel='%(calendarmodel)s')" \ - % self.__dict__ -
class WbQuantity(_WbRepresentation):
"""A Wikibase quantity representation.""" + + _items = ('amount', 'upperBound', 'lowerBound', 'unit')
def __init__(self, amount, unit=None, error=None): u""" @@ -523,10 +516,6 @@ lowerBound = eval(wb['lowerBound']) error = (upperBound - amount, amount - lowerBound) return cls(amount, wb['unit'], error) - - def __repr__(self): - return (u"WbQuantity(amount=%(amount)s, upperBound=%(upperBound)s, " - u"lowerBound=%(lowerBound)s, unit=%(unit)s)" % self.__dict__)
_sites = {} diff --git a/pywikibot/_wbtypes.py b/pywikibot/_wbtypes.py index 7175cdd..7d9d908 100644 --- a/pywikibot/_wbtypes.py +++ b/pywikibot/_wbtypes.py @@ -12,6 +12,8 @@
import json
+from pywikibot.tools import StringTypes +
class WbRepresentation(object):
@@ -33,5 +35,14 @@ return json.dumps(self.toWikibase(), indent=4, sort_keys=True, separators=(',', ': '))
+ def __repr__(self): + assert isinstance(self._items, tuple) + assert all(isinstance(item, StringTypes) for item in self._items) + + values = ((attr, getattr(self, attr)) for attr in self._items) + attrs = ', '.join('{0}={1}'.format(attr, value) + for attr, value in values) + return '{0}({1})'.format(self.__class__.__name__, attrs) + def __eq__(self, other): return self.__dict__ == other.__dict__
pywikibot-commits@lists.wikimedia.org