Skip to main content

Python bindings for MetroHash, a fast non-cryptographic hash algorithm

Project description

MetroHash

Python wrapper for MetroHash, a fast non-cryptographic hash function.

Build Status Latest Version Downloads License Supported Python versions

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 usage example below).

Usage Examples

Stateless 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 and an optional seed parameter:

>>> import metrohash
...
>>> metrohash.hash64_int("abc", seed=0)
17099979927131455419
>>> metrohash.hash128_int("abc")
182995299641628952910564950850867298725

Incremental hashing

Unlike its cousins CityHash and FarmHash, MetroHash allows incremental (stateful) 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

The resulting hash value above should be the same as in:

>>> mh = metrohash.MetroHash64()
>>> mh.update("Nobody inspects the spammish repetition")
>>> mh.intdigest()
7851180100622203313

Fast hashing of NumPy arrays

The Python Buffer Protocol allows Python objects to expose their data as raw byte arrays to other objects, for fast access without copying to a separate location in memory. Among others, NumPy is a major framework that supports this protocol.

All hashing functions in this packege will read byte arrays from objects that expose them via the buffer protocol. Here is an example showing hashing of a 4D NumPy array:

>>> import numpy as np
>>> arr = np.zeros((256, 256, 4))
>>> metrohash.hash64_int(arr)
12125832280816116063

The arrays need to be contiguous for this to work. To convert a non-contiguous array, use NumPy's ascontiguousarray() function.

Development

Local workflow

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           # create a virtual environment
make test          # run Python tests
make cpp-test      # run C++ tests
make shell         # enter IPython shell

To find out which Make targets are available, type:

make help

Distribution

The wheels are built using cibuildwheel and are distributed to PyPI using GitHub actions. The wheels contain compiled binaries and are available for the following platforms: windows-amd64, ubuntu-x86, linux-x86_64, linux-aarch64, and macosx-x86_64.

See Also

For other fast non-cryptographic hash functions available as Python extensions, see FarmHash and MurmurHash.

Authors

The MetroHash algorithm and C++ implementation is due to J. Andrew Rogers. The Python bindings for it were written by Eugene Scherba.

License

This software is licensed under the Apache License, Version 2.0. See the included LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metrohash-0.3.3.tar.gz (63.2 kB view hashes)

Uploaded Source

Built Distributions

metrohash-0.3.3-cp311-cp311-win_amd64.whl (32.5 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

metrohash-0.3.3-cp311-cp311-win32.whl (32.7 kB view hashes)

Uploaded CPython 3.11 Windows x86

metrohash-0.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (205.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

metrohash-0.3.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (205.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

metrohash-0.3.3-cp311-cp311-macosx_11_0_arm64.whl (35.7 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

metrohash-0.3.3-cp311-cp311-macosx_10_9_x86_64.whl (37.2 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

metrohash-0.3.3-cp310-cp310-win_amd64.whl (33.1 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

metrohash-0.3.3-cp310-cp310-win32.whl (32.8 kB view hashes)

Uploaded CPython 3.10 Windows x86

metrohash-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (190.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

metrohash-0.3.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (190.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

metrohash-0.3.3-cp310-cp310-macosx_11_0_arm64.whl (35.7 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

metrohash-0.3.3-cp310-cp310-macosx_10_9_x86_64.whl (37.1 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

metrohash-0.3.3-cp39-cp39-win_amd64.whl (33.5 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

metrohash-0.3.3-cp39-cp39-win32.whl (33.1 kB view hashes)

Uploaded CPython 3.9 Windows x86

metrohash-0.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (195.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

metrohash-0.3.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (197.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

metrohash-0.3.3-cp39-cp39-macosx_11_0_arm64.whl (36.2 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

metrohash-0.3.3-cp39-cp39-macosx_10_9_x86_64.whl (37.8 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

metrohash-0.3.3-cp38-cp38-win_amd64.whl (33.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

metrohash-0.3.3-cp38-cp38-win32.whl (33.2 kB view hashes)

Uploaded CPython 3.8 Windows x86

metrohash-0.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (194.4 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

metrohash-0.3.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (195.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

metrohash-0.3.3-cp38-cp38-macosx_10_9_x86_64.whl (37.3 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

metrohash-0.3.3-cp37-cp37m-win_amd64.whl (33.5 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

metrohash-0.3.3-cp37-cp37m-win32.whl (32.9 kB view hashes)

Uploaded CPython 3.7m Windows x86

metrohash-0.3.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (185.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

metrohash-0.3.3-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (184.9 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

metrohash-0.3.3-cp37-cp37m-macosx_10_9_x86_64.whl (36.9 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

metrohash-0.3.3-cp36-cp36m-win_amd64.whl (33.6 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

metrohash-0.3.3-cp36-cp36m-win32.whl (33.0 kB view hashes)

Uploaded CPython 3.6m Windows x86

metrohash-0.3.3-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (184.2 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

metrohash-0.3.3-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (183.3 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ ARM64

metrohash-0.3.3-cp36-cp36m-macosx_10_9_x86_64.whl (37.8 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page