jenkins-bot has submitted this change and it was merged.
Change subject: Page and Link: use 'rich' comparison
......................................................................
Page and Link: use 'rich' comparison
Py3 killed __cmp__, so we have to use the rich comparison operators
(__eq__, __ne__, etc.). To prevent having to define all six of them,
we use the ComparableMixin from [1] instead. Comparison is then
done using the tuple returned from self._cmpkey().
[1]
http://python3porting.com/preparing.html
Change-Id: I2fa2b7322769beace6d3d42ca533943fe2b4c82b
---
M pywikibot/page.py
M pywikibot/tools.py
2 files changed, 39 insertions(+), 48 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py
index fe54ef6..cfb6fe8 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -15,7 +15,7 @@
from pywikibot import deprecated
from pywikibot import config
import pywikibot.site
-
+from pywikibot.tools import ComparableMixin
import hashlib
try:
@@ -42,7 +42,7 @@
# Note: Link objects (defined later on) represent a wiki-page's title, while
# Page objects (defined here) represent the page itself, including its contents.
-class Page(pywikibot.UnicodeMixin):
+class Page(pywikibot.UnicodeMixin, ComparableMixin):
"""Page: A MediaWiki page
This object only implements internally methods that do not require
@@ -223,23 +223,13 @@
return "%s(%s)" % (self.__class__.__name__,
self.title().encode(config.console_encoding))
- def __cmp__(self, other):
- """Test for equality and inequality of Page objects.
-
+ def _cmpkey(self):
+ """
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.
-
- """
- 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._link.title, other._link.title)
+ Page objects are sortable by site, namespace then title."""
+ return (self.site, self.namespace(), self.title())
def __hash__(self):
# Pseudo method that makes it possible to store Page objects as keys
@@ -2393,25 +2383,6 @@
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()
@@ -3107,7 +3078,7 @@
self.minor = minor
-class Link(object):
+class Link(ComparableMixin):
"""A Mediawiki link (local or interwiki)
Has the following attributes:
@@ -3419,23 +3390,14 @@
def __str__(self):
return self.astext().encode("ascii", "backslashreplace")
- def __cmp__(self, other):
- """Test for equality and inequality of Link objects.
-
+ def _cmpkey(self):
+ """
Link objects are "equal" if and only if they are on the same site
and have the same normalized title, including section if any.
Link objects are sortable by site, then namespace, then title.
-
"""
- if not isinstance(other, Link):
- # especially, return -1 if other is None
- return -1
- if not 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)
+ return (self.site, self.namespace, self.title)
def __unicode__(self):
return self.astext()
diff --git a/pywikibot/tools.py b/pywikibot/tools.py
index 3b5671b..31ca0ef 100644
--- a/pywikibot/tools.py
+++ b/pywikibot/tools.py
@@ -30,6 +30,35 @@
return self.__unicode__().encode('utf8')
+# From
http://python3porting.com/preparing.html
+class ComparableMixin(object):
+ def _compare(self, other, method):
+ try:
+ return method(self._cmpkey(), other._cmpkey())
+ except (AttributeError, TypeError):
+ # _cmpkey not implemented, or return different type,
+ # so I can't compare with "other".
+ return NotImplemented
+
+ def __lt__(self, other):
+ return self._compare(other, lambda s, o: s < o)
+
+ def __le__(self, other):
+ return self._compare(other, lambda s, o: s <= o)
+
+ def __eq__(self, other):
+ return self._compare(other, lambda s, o: s == o)
+
+ def __ge__(self, other):
+ return self._compare(other, lambda s, o: s >= o)
+
+ def __gt__(self, other):
+ return self._compare(other, lambda s, o: s > o)
+
+ def __ne__(self, other):
+ return self._compare(other, lambda s, o: s != o)
+
+
class ThreadedGenerator(threading.Thread):
"""Look-ahead generator class.
--
To view, visit
https://gerrit.wikimedia.org/r/130020
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I2fa2b7322769beace6d3d42ca533943fe2b4c82b
Gerrit-PatchSet: 5
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>