John Vandenberg has uploaded a new change for review.

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

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(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/40/247040/1

diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 8261c9c..f018896 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):
@@ -368,6 +363,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,
@@ -477,6 +475,8 @@
 
     """A Wikibase quantity representation."""
 
+    _items = ('amount', 'upperBound', 'lowerBound', 'unit')
+
     def __init__(self, amount, unit=None, error=None):
         u"""
         Create a new WbQuantity object.
@@ -523,10 +523,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__

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4107523c32a83b72899b954c3b514eb37d95b84d
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jay...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to