Python wrapper for MurmurHash (MurmurHash3), a set of fast and robust hash functions.
mmh3 2.4 supports Python 2.7, Python 3.3 and higher.
>>> import mmh3 >>> mmh3.hash('foo') # 32 bit signed int -156908512 >>> mmh3.hash64('foo') # two 64 bit signed ints (by using the 128-bit algorithm as its backend) (-2129773440516405919, 9128664383759220103) >>> mmh3.hash128('foo') # 128 bit signed int 168394135621993849475852668931176482145 >>> mmh3.hash_bytes('foo') # 128 bit value as bytes 'aE\xf5\x01W\x86q\xe2\x87}\xba+\xe4\x87\xaf~' >>> mmh3.hash('foo', 42) # uses 42 for its seed -1322301282
hash_bytes have the third argument for architecture optimization. Use True for x64 and False for x86 (default: True).:
>>> mmh3.hash64('foo', 42, True) (-840311307571801102, -6739155424061121879)
hash64 returns two values, because it uses the 128-bit version of MurmurHash3 as its backend.
Version 2.4 added support for 64-bit data.
>>> import numpy as np >>> a = np.zeros(2**32, dtype=np.int8) >>> mmh3.hash_bytes(a) b'V\x8f}\xad\x8eNM\xa84\x07FU\x9c\xc4\xcc\x8e'
Version 2.4 also changed the type of seeds from signed 32-bit int to unsigned 32-bit int. (The resulting values with signed seeds still remain the same as before, as long as they are 32-bit)
>>> mmh3.hash('aaaa', -1756908916) # signed rep. for 0x9747b28c 1519878282 >>> mmh3.hash('aaaa', 2538058380) # unsigned rep. for 0x9747b28c 1519878282
Be careful so that these seeds do not exceed 32-bit. Unexpected results may happen with invalid values.
>>> mmh3.hash('foo', 2 ** 33) -156908512 >>> mmh3.hash('foo', 2 ** 34) -156908512
- Support seeds with 32-bit unsigned integers; thanks Alexander Maznev!
- Support 64-bit data (under 64-bit environments)
- Fix compile errors for Python 3.6 under Windows systems.
- Add unit testing and continuous integration with Travis CI and AppVeyor.
- Relicensed from public domain to CC0-1.0.
- Fix compile errors for gcc >=5.
hash128, which returns a 128-bit signed integer.
- Fix a misplaced operator which could cause memory leak in a rare condition.
- Fix a malformed value to a Python/C API function which may cause runtime errors in recent Python 3.x versions.
The first two commits are from Derek Wilson. Thanks!
- Improve portability to support systems with old gcc (version < 4.4) such as CentOS/RHEL 5.x. (Commit from Micha Gorelick. Thanks!)
- Add __version__ constant. Check if it exists when the following revision matters for your application.
- Incorporate the revision r147, which includes robustness improvement and minor tweaks.
Beware that due to this revision, the result of 32-bit version of 2.1 is NOT the same as that of 2.0. E.g.,:
>>> mmh3.hash('foo') # in mmh3 2.0 -292180858 >>> mmh3.hash('foo') # in mmh3 2.1 -156908512
The results of hash64 and hash_bytes remain unchanged. Austin Appleby, the author of Murmurhash, ensured this revision was the final modification to MurmurHash3's results and any future changes would be to improve performance only.
How can I use this module? Any tutorials?
The following textbooks and tutorials are great sources to learn how to use mmh3 (and other hash algorithms in general) for high-performance computing.
- Chapter 11: Using Less Ram in Micha Gorelick and Ian Ozsvald. 2014. High Performance Python: Practical Performant Programming for Humans. O'Reilly Media. ISBN: 978-1-4493-6159-4.
- Duke University. Efficient storage of data in memeory.
- Max Burstein. Creating a Simple Bloom Filter.
- Bugra Akyildiz. A Gentle Introduction to Bloom Filter.
Some results are different from other MurmurHash3-based libraries.
mmh3 returns signed values. To convert them into unsigned values, see https://stackoverflow.com/questions/20766813/how-to-convert-signed-to-unsigned-integer-in-python
For compatibility with Google Guava (Java), see https://stackoverflow.com/questions/29932956/murmur3-hash-different-result-between-python-and-java-implementation
I want to report errors/ask questions/send requests.
Thank you for helping me to improve the library. Please make sure to post them through the issue tracking system of GitHub. Issues sent directly to my email account may go unnoticed.
MurmurHash3 was originally developed by Austin Appleby and distributed under public domain.
Ported and modified for Python by Hajime Senuma.