From f9526e78a8037f480860613d05f3ecaa0989af74 Mon Sep 17 00:00:00 2001 From: Ryan Stein Date: Sun, 29 Oct 2017 22:39:54 -0400 Subject: [PATCH] Implement hashing functions for Command and ServerSession. --- evennia/commands/command.py | 13 +++++++++++++ evennia/server/serversession.py | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/evennia/commands/command.py b/evennia/commands/command.py index c30c74222e..094934a02e 100644 --- a/evennia/commands/command.py +++ b/evennia/commands/command.py @@ -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 diff --git a/evennia/server/serversession.py b/evennia/server/serversession.py index acd59ad67f..c86e189b64 100644 --- a/evennia/server/serversession.py +++ b/evennia/server/serversession.py @@ -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