CityHash and FarmHash Python Bindings

0.4.10 · active · verified Mon Apr 13

CityHash is a family of fast non-cryptographic hash functions for strings, originally developed by Google. FarmHash is a successor designed for improved performance and collision resistance on modern CPUs. This library, `python-cityhash`, provides Python bindings for both CityHash and FarmHash, enabling high-performance hashing in Python applications. It is currently at version 0.4.10 and receives updates for Python version compatibility and Cython build fixes.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use CityHash and FarmHash functions. It highlights the necessity of encoding strings to bytes and converting integers to a fixed-size byte representation for consistent hashing.

import cityhash
import farmhash

# Hashing a string (must be encoded to bytes)
data_string = "hello world"
hashed_bytes = cityhash.CityHash64(data_string.encode('utf-8'))
print(f"CityHash64 of '{data_string}': {hashed_bytes}")

# Hashing bytes directly
data_bytes = b"another example"
hashed_bytes_128 = cityhash.CityHash128(data_bytes)
print(f"CityHash128 of '{data_bytes.decode()}': {hashed_bytes_128}")

# Hashing with FarmHash
farm_hash_64 = farmhash.FarmHash64(b"farmhash test")
print(f"FarmHash64 of 'farmhash test': {farm_hash_64}")

# Hashing an integer (must be converted to fixed-size bytes)
int_data = 123456789
hashed_int = cityhash.CityHash64(int_data.to_bytes(8, 'big'))
print(f"CityHash64 of integer {int_data}: {hashed_int}")

view raw JSON →