Implement hashing functions for Command and ServerSession.

This commit is contained in:
Ryan Stein 2017-10-29 22:39:54 -04:00
parent ee58e59e7e
commit f9526e78a8
2 changed files with 22 additions and 0 deletions

View file

@ -201,6 +201,19 @@ class Command(with_metaclass(CommandMeta, object)):
# probably got a string
return cmd in self._matchset
def __hash__(self):
"""
Python 3 requires that any class which implements __eq__ must also
implement __hash__ and that the corresponding hashes for equivalent
instances are themselves equivalent.
Technically, the following implementation is only valid for comparison
against other Commands, as our __eq__ supports comparison against
str, too.
"""
return hash('\n'.join(self._matchset))
def __ne__(self, cmd):
"""
The logical negation of __eq__. Since this is one of the most

View file

@ -433,6 +433,15 @@ class ServerSession(Session):
except AttributeError:
return False
def __hash__(self):
"""
Python 3 requires that any class which implements __eq__ must also
implement __hash__ and that the corresponding hashes for equivalent
instances are themselves equivalent.
"""
return hash(self.address)
def __ne__(self, other):
try:
return self.address != other.address