Python bindings for MetroHash, a fast non-cryptographic hash algorithm
Project description
A Python wrapper around MetroHash
Getting Started
To use this package in your program, simply type
pip install metrohash
After that, you should be able to import the module and do things with it (see Example Usage below).
Usage Examples
Simple Hashing
This package provides Python interfaces to 64- and 128-bit implementations of MetroHash algorithm. For stateless hashing, it exports metrohash64 and metrohash128 functions. Both take a value to be hashed (either string or unicode) and an optional seed parameter:
>>> import metrohash
...
>>> metrohash.metrohash64("abc", seed=0)
17099979927131455419
>>> metrohash.metrohash128("abc")
182995299641628952910564950850867298725
Incremental Hashing
For incremental hashing, use MetroHash64 and MetroHash128 classes. Incremental hashing is associative and guarantees that any combination of input slices will result in the same final hash value. This is useful for processing large inputs and stream data. Example with two slices:
>>> mh = metrohash.MetroHash64()
>>> mh.update("Nobody inspects")
>>> mh.update(" the spammish repetition")
>>> mh.intdigest()
7851180100622203313
Note that the resulting hash value above is the same as in:
>>> mh = metrohash.MetroHash64()
>>> mh.update("Nobody inspects the spammish repetition")
>>> mh.intdigest()
7851180100622203313
Buffer Protocol Support
The methods in this module support Python Buffer Protocol, which allows them to be used on any object that exports a buffer interface. Here is an example showing hashing of a 4D NumPy array:
>>> import numpy as np
>>> arr = np.zeros((256, 256, 4))
>>> metrohash.metrohash64(arr)
12125832280816116063
Note that arrays need to be contiguous for this to work. To convert a non-contiguous array, use np.ascontiguousarray() method.
Development
For those who want to contribute, here is a quick start using some makefile commands:
git clone https://github.com/escherba/python-metrohash.git
cd python-metrohash
make env # creates a Python virtualenv
make test # run Python tests
make cpp-test # run C++ tests
The Makefiles provided have self-documenting targets. To find out which targets are available, type:
make help
See Also
For other fast non-cryptographic hashing implementations available as Python extensions, see CityHash and xxh.
License
This software is licensed under the Apache License, Version 2.0. See the included LICENSE file for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.