Make comparing contexts easier

This commit is contained in:
Vgr E. Barry 2016-12-19 12:34:01 -05:00
parent 59e7c13a0b
commit 9014d5c35c
3 changed files with 29 additions and 12 deletions

View File

@ -91,6 +91,16 @@ class Channel(IRCContext):
def __repr__(self):
return "{self.__class__.__name__}({self.name!r})".format(self=self)
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
return self._compare(other, Channel, "name", "key", "client", "state", "modes", "timestamp")
@property
def key(self):
return self._key
def queue(self, name, params, args):
if self._pending is None:
Event(name, params).dispatch(*args)

View File

@ -121,6 +121,24 @@ class IRCContext:
return self.name
raise ValueError("Format specificer {0} has undefined semantics".format(format_spec))
def __eq__(self, other):
return self._compare(other, __class__) # This will always return False
def _compare(self, other, cls, *attributes):
"""Compare two instances and return a proper value."""
if not isinstance(other, cls):
return NotImplemented
done = False
for attr in attributes:
if getattr(self, attr) is None or getattr(other, attr) is None:
continue
done = True
if getattr(self, attr) != getattr(other, attr):
return False
return done
def lower(self):
temp = type(self)(lower(name), client)
temp.ref = self.ref or self

View File

@ -262,18 +262,7 @@ class User(IRCContext):
return hash((self.ident, self.host))
def __eq__(self, other):
if not isinstance(other, User):
return NotImplemented
done = False
for a, b in ((self.nick, other.nick), (self.ident, other.ident), (self.host, other.host), (self.realname, other.realname), (self.account, other.account)):
if a is None or b is None:
continue
done = True
if a != b:
return False
return done
return self._compare(other, __class__, "nick", "ident", "host", "realname", "account")
def lower(self):
temp = type(self)(self.client, lower(self.nick), lower(self.ident), lower(self.host), lower(self.realname), lower(self.account))