Make comparing contexts easier
This commit is contained in:
parent
59e7c13a0b
commit
9014d5c35c
@ -91,6 +91,16 @@ class Channel(IRCContext):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "{self.__class__.__name__}({self.name!r})".format(self=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):
|
def queue(self, name, params, args):
|
||||||
if self._pending is None:
|
if self._pending is None:
|
||||||
Event(name, params).dispatch(*args)
|
Event(name, params).dispatch(*args)
|
||||||
|
@ -121,6 +121,24 @@ class IRCContext:
|
|||||||
return self.name
|
return self.name
|
||||||
raise ValueError("Format specificer {0} has undefined semantics".format(format_spec))
|
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):
|
def lower(self):
|
||||||
temp = type(self)(lower(name), client)
|
temp = type(self)(lower(name), client)
|
||||||
temp.ref = self.ref or self
|
temp.ref = self.ref or self
|
||||||
|
13
src/users.py
13
src/users.py
@ -262,18 +262,7 @@ class User(IRCContext):
|
|||||||
return hash((self.ident, self.host))
|
return hash((self.ident, self.host))
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if not isinstance(other, User):
|
return self._compare(other, __class__, "nick", "ident", "host", "realname", "account")
|
||||||
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
|
|
||||||
|
|
||||||
def lower(self):
|
def lower(self):
|
||||||
temp = type(self)(self.client, lower(self.nick), lower(self.ident), lower(self.host), lower(self.realname), lower(self.account))
|
temp = type(self)(self.client, lower(self.nick), lower(self.ident), lower(self.host), lower(self.realname), lower(self.account))
|
||||||
|
Loading…
Reference in New Issue
Block a user