Skip to main content

Pure Python reimplementation of CPython's `tuplehash` function with exact overflow behavior, designed for hashing user-defined immutable sequences.

Project description

tuplehash

Pure Python reimplementation of CPython's tuplehash function with exact overflow behavior, designed for hashing user-defined immutable sequences.

The Current Pain Point

Currently in Python, if you want your custom immutable sequence to hash like a tuple:

from typing import TypeVar, Sequence

T = TypeVar('T', covariant=True)


class MySequence(Sequence[T]):
    def __hash__(self):
        return hash(tuple(self))

This creates memory overhead for large collections.

Until such stdlib functionality exists, tuplehash provides:

# Explicitly provide a non-negative int to `len` if `len(iterable)` doesn't work
def tuplehash(iterable, length=None): ...

So you can do this:

from typing import TypeVar, Sequence

from tuplehash import tuplehash

T = TypeVar('T', covariant=True)


class MySequence(Sequence[T]):
    def __hash__(self):
        return tuplehash(self)

This gives you stdlib-quality hashing today.

Features

Installation

pip install tuplehash

Usage

from tuplehash import tuplehash

# Basic usage
assert tuplehash((1, 2, 3)) == hash((1, 2, 3))

# Works with any collection
from collections import namedtuple

Point = namedtuple('Point', ['x', 'y'])
assert tuplehash(Point(3, 4)) == hash(Point(3, 4))

Implementation Details

Version-Specific Algorithms

Python Version Algorithm Key Characteristics
<3.8 Multiplicative hash Initial value 0x345678, multiplier 1000003, length-dependent addend
≥3.8 Simplified xxHash Single accumulator, 31/13-bit rotations, prime multiplications

Overflow Handling

Uses custom Signed/Unsigned types to exactly replicate:

  • 32-bit overflow on 32-bit platforms
  • 64-bit overflow on 64-bit platforms
  • All intermediate casting behaviors

Limitations

  • Performance overhead vs native implementation

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

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

tuplehash-0.1.0a2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tuplehash-0.1.0a2-py2.py3-none-any.whl (4.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file tuplehash-0.1.0a2.tar.gz.

File metadata

  • Download URL: tuplehash-0.1.0a2.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for tuplehash-0.1.0a2.tar.gz
Algorithm Hash digest
SHA256 e9132e840b8f1441294a0de75b963c2fdda7112d1618e49acbffff7d1427de7f
MD5 012a164c9eec16fcd00615e85c65dfd8
BLAKE2b-256 63fa4095361002158a80aa9ff53ef88047b83dd0a35286886bca04a331aad2bc

See more details on using hashes here.

File details

Details for the file tuplehash-0.1.0a2-py2.py3-none-any.whl.

File metadata

  • Download URL: tuplehash-0.1.0a2-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for tuplehash-0.1.0a2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 964159b05c40ab052c7d187d0e1692ce71432cf1f8d6c282453abc02ab056a7f
MD5 9883060a035e6c8cc4b26583dc8593b7
BLAKE2b-256 276b9d1224377c4b5eec298d52e3e998286af71159473ece58b610aaacac1594

See more details on using hashes here.

Supported by

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