commit 873d2ecf8931b70181cd6d7c7271a7ce3670ec9b Author: Damian Johnson atagar@torproject.org Date: Sat Jul 2 12:20:34 2016 -0700
Move _hash_attr() to stem.util
On reflection more fitting in the util module. --- stem/__init__.py | 26 -------------------------- stem/descriptor/remote.py | 10 ++++------ stem/exit_policy.py | 6 +++--- stem/manual.py | 7 ++++--- stem/util/__init__.py | 26 ++++++++++++++++++++++++++ stem/version.py | 5 +++-- 6 files changed, 40 insertions(+), 40 deletions(-)
diff --git a/stem/__init__.py b/stem/__init__.py index 56d294d..1f61075 100644 --- a/stem/__init__.py +++ b/stem/__init__.py @@ -860,29 +860,3 @@ HSAuth = stem.util.enum.UppercaseEnum( 'STEALTH_AUTH', 'UNKNOWN', ) - - -def _hash_attr(obj, *attributes, **kwargs): - """ - Provide a hash value for the given set of attributes. - - :param Object obj: object to be hashed - :param list attributes: attribute names to take into account - :param class parent: parent object to include in the hash value - """ - - my_hash = 0 if kwargs.get('parent') == None else kwargs.get('parent').__hash__(obj) - - for attr in attributes: - my_hash *= 1024 - - attr_value = getattr(obj, attr) - - if attr_value is not None: - if isinstance(attr_value, dict): - for k, v in attr_value.items(): - my_hash = (my_hash + hash(k)) * 1024 + hash(v) - else: - my_hash += hash(attr_value) - - return my_hash diff --git a/stem/descriptor/remote.py b/stem/descriptor/remote.py index 69f4d39..f25d58e 100644 --- a/stem/descriptor/remote.py +++ b/stem/descriptor/remote.py @@ -91,8 +91,6 @@ import threading import time import zlib
-import stem - try: # account for urllib's change between python 2.x and 3.x import urllib.request as urllib @@ -102,7 +100,7 @@ except ImportError: import stem.descriptor
from stem import Flag -from stem.util import connection, log, str_tools, tor_tools +from stem.util import _hash_attr, connection, log, str_tools, tor_tools
# Tor has a limited number of descriptors we can fetch explicitly by their # fingerprint or hashes due to a limit on the url length by squid proxies. @@ -719,7 +717,7 @@ class Directory(object): self.fingerprint = fingerprint
def __hash__(self): - return stem._hash_attr(self, 'address', 'or_port', 'dir_port', 'fingerprint') + return _hash_attr(self, 'address', 'or_port', 'dir_port', 'fingerprint')
def __eq__(self, other): return hash(self) == hash(other) if isinstance(other, Directory) else False @@ -772,7 +770,7 @@ class DirectoryAuthority(Directory): self.is_bandwidth_authority = is_bandwidth_authority
def __hash__(self): - return stem._hash_attr(self, 'nickname', 'v3ident', 'is_bandwidth_authority', parent = Directory) + return _hash_attr(self, 'nickname', 'v3ident', 'is_bandwidth_authority', parent = Directory)
def __eq__(self, other): return hash(self) == hash(other) if isinstance(other, DirectoryAuthority) else False @@ -1066,7 +1064,7 @@ class FallbackDirectory(Directory): return results
def __hash__(self): - return stem._hash_attr(self, 'orport_v6', parent = Directory) + return _hash_attr(self, 'orport_v6', parent = Directory)
def __eq__(self, other): return hash(self) == hash(other) if isinstance(other, FallbackDirectory) else False diff --git a/stem/exit_policy.py b/stem/exit_policy.py index a0b3064..e84a456 100644 --- a/stem/exit_policy.py +++ b/stem/exit_policy.py @@ -70,13 +70,13 @@ from __future__ import absolute_import import socket import zlib
-import stem import stem.prereq import stem.util.connection import stem.util.enum import stem.util.str_tools
from stem import str_type +from stem.util import _hash_attr
try: # added in python 3.2 @@ -1014,7 +1014,7 @@ class ExitPolicyRule(object):
def __hash__(self): if self._hash is None: - self._hash = stem._hash_attr(self, 'is_accept', 'address', 'min_port', 'max_port') * 1024 + hash(self.get_mask(False)) + self._hash = _hash_attr(self, 'is_accept', 'address', 'min_port', 'max_port') * 1024 + hash(self.get_mask(False))
return self._hash
@@ -1060,7 +1060,7 @@ class MicroExitPolicyRule(ExitPolicyRule):
def __hash__(self): if self._hash is None: - self._hash = stem._hash_attr(self, 'is_accept', 'min_port', 'max_port') + self._hash = _hash_attr(self, 'is_accept', 'min_port', 'max_port')
return self._hash
diff --git a/stem/manual.py b/stem/manual.py index fe83487..70cae73 100644 --- a/stem/manual.py +++ b/stem/manual.py @@ -52,13 +52,14 @@ import shutil import sys import tempfile
-import stem import stem.prereq import stem.util.conf import stem.util.enum import stem.util.log import stem.util.system
+from stem.util import _hash_attr + try: # added in python 2.7 from collections import OrderedDict @@ -112,7 +113,7 @@ class ConfigOption(object): self.description = description
def __hash__(self): - return stem._hash_attr(self, 'name', 'category', 'usage', 'summary', 'description') + return _hash_attr(self, 'name', 'category', 'usage', 'summary', 'description')
def __eq__(self, other): return hash(self) == hash(other) if isinstance(other, ConfigOption) else False @@ -468,7 +469,7 @@ class Manual(object): conf.save(path)
def __hash__(self): - return stem._hash_attr(self, 'name', 'synopsis', 'description', 'commandline_options', 'signals', 'files', 'config_options') + return _hash_attr(self, 'name', 'synopsis', 'description', 'commandline_options', 'signals', 'files', 'config_options')
def __eq__(self, other): return hash(self) == hash(other) if isinstance(other, Manual) else False diff --git a/stem/util/__init__.py b/stem/util/__init__.py index beb0ee6..63609d9 100644 --- a/stem/util/__init__.py +++ b/stem/util/__init__.py @@ -40,3 +40,29 @@ def datetime_to_unix(timestamp): return int(timestamp.strftime('%s')) - int(datetime.datetime(1970, 1, 1).strftime('%s')) + 3600 else: return (timestamp - datetime.datetime(1970, 1, 1)).total_seconds() + + +def _hash_attr(obj, *attributes, **kwargs): + """ + Provide a hash value for the given set of attributes. + + :param Object obj: object to be hashed + :param list attributes: attribute names to take into account + :param class parent: parent object to include in the hash value + """ + + my_hash = 0 if kwargs.get('parent') == None else kwargs.get('parent').__hash__(obj) + + for attr in attributes: + my_hash *= 1024 + + attr_value = getattr(obj, attr) + + if attr_value is not None: + if isinstance(attr_value, dict): + for k, v in attr_value.items(): + my_hash = (my_hash + hash(k)) * 1024 + hash(v) + else: + my_hash += hash(attr_value) + + return my_hash diff --git a/stem/version.py b/stem/version.py index de4a784..e13c420 100644 --- a/stem/version.py +++ b/stem/version.py @@ -72,10 +72,11 @@ easily parsed and compared, for instance... import os import re
-import stem import stem.util.enum import stem.util.system
+from stem.util import _hash_attr + try: # added in python 3.2 from functools import lru_cache @@ -235,7 +236,7 @@ class Version(object):
def __hash__(self): if self._hash is None: - self._hash = stem._hash_attr(self, 'major', 'minor', 'micro', 'patch', 'status') + self._hash = _hash_attr(self, 'major', 'minor', 'micro', 'patch', 'status')
return self._hash