Ricordisamoa has uploaded a new change for review.

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

Change subject: improved checks for precision when initializing WbTime objects
......................................................................

improved checks for precision when initializing WbTime objects

also used the 'self' keyword instead of the hardcoded class name
and added two tests

Change-Id: Ieffc18ebac19050ceb4d71246a042db1d4b29db3
---
M pywikibot/__init__.py
M tests/wikibase_tests.py
2 files changed, 12 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/75/129975/1

diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 784ca29..3ef3769 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -224,21 +224,21 @@
         """
         if year is None:
             raise ValueError('no year given')
-        self.precision = WbTime.PRECISION['second']
+        self.precision = self.PRECISION['second']
         if second is None:
-            self.precision = WbTime.PRECISION['minute']
+            self.precision = self.PRECISION['minute']
             second = 0
         if minute is None:
-            self.precision = WbTime.PRECISION['hour']
+            self.precision = self.PRECISION['hour']
             minute = 0
         if hour is None:
-            self.precision = WbTime.PRECISION['day']
+            self.precision = self.PRECISION['day']
             hour = 0
         if day is None:
-            self.precision = WbTime.PRECISION['month']
+            self.precision = self.PRECISION['month']
             day = 1
         if month is None:
-            self.precision = WbTime.PRECISION['year']
+            self.precision = self.PRECISION['year']
             month = 1
         self.year = long(year)
         self.month = month
@@ -253,10 +253,10 @@
 
         # if precision is given it overwrites the autodetection above
         if precision is not None:
-            if isinstance(precision, int):
+            if isinstance(precision, int) and precision in 
self.PRECISION.values():
                 self.precision = precision
-            elif precision in WbTime.PRECISION:
-                self.precision = WbTime.PRECISION[precision]
+            elif precision in self.PRECISION:
+                self.precision = self.PRECISION[precision]
             else:
                 raise ValueError('Invalid precision: "%s"' % precision)
 
@@ -274,7 +274,7 @@
         """
         Function which converts the the data to a UTC date/time string
         """
-        return WbTime.FORMATSTR.format(self.year, self.month, self.day,
+        return self.FORMATSTR.format(self.year, self.month, self.day,
                                         self.hour, self.minute, self.second)
 
     def toWikibase(self):
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index fc0e267..d1338ad 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -49,6 +49,8 @@
         # test WbTime
         t = pywikibot.WbTime(year=2010, hour=12, minute=43)
         self.assertEqual(t.toTimestr(), '+00000002010-01-01T12:43:00Z')
+        self.assertRaises(ValueError, pywikibot.WbTime, precision=15)
+        self.assertRaises(ValueError, pywikibot.WbTime, 
precision='invalid_precision')
 
         # test WikibasePage.__cmp__
         self.assertEqual(pywikibot.ItemPage.fromPage(mainpage), 
pywikibot.ItemPage(repo, 'q5296'))

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ieffc18ebac19050ceb4d71246a042db1d4b29db3
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Ricordisamoa <ricordisa...@live.it>

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

Reply via email to