Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

perfection — Perfect hashing utilities

BETA VERSION:

Can import a minimal perfect (ordered!) hash function with the same API using:

import perfection.czech

API is not yet finalized!

<hr>

A module that creates perfect hash functions for a known set of integer inputs.

>>> import perfection
>>> l = (0, 3, 4, 7 ,10, 13, 15, 18, 19, 21, 22, 24, 26, 29, 30, 34)
>>> hf = perfection.make_hash(l)
>>> hf(19)
1

Main features

  • make_hash() that generates an honest-to-goodness perfect hash function for the given keys.

  • make_dict() creates a dictionary subclass that implements the MutableMapping interface (thus, acts exactly like a dict), and uses the hash function created in the equivalent call to make_hash().

Additionally, hash_parameters() may be used to output the parameters of making a perfect hash for the given set of input keys. These parameters can then be used to implement a perfect hash function in a language of your choice.

For example, generate t and r parameters using hash_parameters():

>>> l = (0, 3, 4, 7 ,10, 13, 15, 18, 19, 21, 22, 24, 26, 29, 30, 34)
>>> params = hash_parameters(l)
>>> params.t
6
>>> params.r
(2, 7, 12, 0, 7, 10)

Then, the hash function, in pseudocode is as follows:

function hash(i):
    static r = { 2, 7, 12, 0, 7, 10 }
    static t = 6

    x = i mod t
    y = i div t
    return x + r[y]

Note that div stands for floor (integer) division.

Credit

Algorithm described by Thomas Gettys.

Python code © 2014 Eddie Antonio Santos. MIT licensed.

Metadata

Release files for perfection 2.0.0-beta

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for perfection 2.0.0-beta
File Size Uploaded Provenance Actions
perfection-2.0.0-beta.tar.gz 10.5 kB Without provenance Details for perfection-2.0.0-beta.tar.gz

Release files / perfection-2.0.0-beta.tar.gz

Download URL perfection-2.0.0-beta.tar.gz
Size 10.5 kB
Tags Source
SHA-256 checksum
How to use checksums
43a1ae6fd377ebae4aa32301034fbff93f1b8ca9249ea3261e8a4fac63103e94
BLAKE2b-256 checksum
How to use checksums
ee8810fa64b6b90e09fe996a6d4d08db2913d96e3b00ad38fd6a7d3340248db5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for perfection 2.0.0-beta

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page