diff --git a/src/channels.py b/src/channels.py index 27abc0a..4d392fc 100644 --- a/src/channels.py +++ b/src/channels.py @@ -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) diff --git a/src/context.py b/src/context.py index 7caf654..15ca3d3 100644 --- a/src/context.py +++ b/src/context.py @@ -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 diff --git a/src/users.py b/src/users.py index eef3145..7293c00 100644 --- a/src/users.py +++ b/src/users.py @@ -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))