Sustainable Hashing Frozen Python Sets

How would you convert the nesting of Python frozenset objects into a unique integer that was the same for Python sessions and platforms?

eg. I get different values ​​from hash () on different platforms.

32 bit

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=frozenset([frozenset([1,2,3]),frozenset(['a','b','c'])]);
>>> hash(a)
1555175235

64 bit

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=frozenset([frozenset([1,2,3]),frozenset(['a','b','c'])]);
>>> hash(a)
-6076998737938213053
+5
source share
2 answers

How would you convert the nesting of Python frozenset objects into a unique integer that was the same for Python sessions and platforms?

AFAIK hashes are not guaranteed to be unique. In fact, when they are used for lookup tables (for example, in dictionaries), conflict hashing is quite common.

. , "" , hashlib. , , (, MD5) .

, , , , .


: :

>>> import cPickle as pkl
>>> import hashlib as hl
>>> s = frozenset([1,2,3])
>>> p = pkl.dumps(sorted(s))  #make sure you use the same pickle protocol on all platform!
'(lp1\nI1\naI2\naI3\na.'
>>> h = hl.md5(p)
<md5 HASH object @ 0xb76fb110>
>>> h.digest()
"\x89\xaeG\x1d'\x83\xa5\xbd\xac\xa7\x1c\xd9\x1d/2t"  #this should be consistent
+8

-:

def hash(fs):
    res = 1
    for v in fs:
        res = (res*31 + v) % 2**30
    return res

, , -, .

0

All Articles