http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11701
Revision: 11701 Author: legoktm Date: 2013-06-30 01:47:43 +0000 (Sun, 30 Jun 2013) Log Message: ----------- Implement WikibasePage.__cmp__ with a unit test.
Modified Paths: -------------- branches/rewrite/pywikibot/page.py branches/rewrite/tests/page_tests.py
Modified: branches/rewrite/pywikibot/page.py =================================================================== --- branches/rewrite/pywikibot/page.py 2013-06-29 09:14:29 UTC (rev 11700) +++ branches/rewrite/pywikibot/page.py 2013-06-30 01:47:43 UTC (rev 11701) @@ -2237,6 +2237,25 @@ self.repo = self.site self._isredir = False # Wikibase pages cannot be a redirect
+ def __cmp__(self, other): + """Test for equality and inequality of WikibasePage objects. + + Page objects are "equal" if and only if they are on the same site + and have the same normalized title, including section if any. + + Page objects are sortable by namespace first, then by title. + + This is basically the same as Page.__cmp__ but slightly different. + """ + if not isinstance(other, Page): + # especially, return -1 if other is None + return -1 + if self.site != other.site: + return cmp(self.site, other.site) + if self.namespace() != other.namespace(): + return cmp(self.namespace(), other.namespace()) + return cmp(self.title(), other.title()) + def title(self, **kwargs): if self.namespace() == 0: self._link._text = self.getID()
Modified: branches/rewrite/tests/page_tests.py =================================================================== --- branches/rewrite/tests/page_tests.py 2013-06-29 09:14:29 UTC (rev 11700) +++ branches/rewrite/tests/page_tests.py 2013-06-30 01:47:43 UTC (rev 11701) @@ -315,6 +315,9 @@ claim.setTarget(pywikibot.ItemPage(repo, 'q1')) self.assertEqual(claim._formatDataValue(), {'entity-type': 'item', 'numeric-id': 1})
+ # test WikibasePage.__cmp__ + self.assertEqual(pywikibot.ItemPage.fromPage(mainpage), pywikibot.ItemPage(repo, 'q5296')) + def testItemPageExtensionability(self): class MyItemPage(pywikibot.ItemPage): pass