Skip to main content

Hashable dict/list - Subclasses of list/dict with a custom hash function based on alternating addition and subtraction of element hashes - pure Python

pip install hashlistdict

Tested against Windows 10 / Python 3.10 / Anaconda

The HashList and HashDict classes provide a custom hash function implementation for lists and dictionaries, respectively. The advantages and potential use cases for these classes are as follows:

Customized Hashing:

By overriding the __hash__() method, the classes allow you to define a custom hash function based on the elements of the list or the key-value pairs of the dictionary. This gives you more control over the hashing process and allows you to incorporate specific logic or considerations when computing the hash value.

Hash-Based Data Structures:

The custom hash function provided by these classes enables you to use instances of HashList and HashDict as keys in hash-based data structures such as dictionaries and sets. Since the hash value of an object is used to determine its position within a hash table, having a custom hash function can be useful when you want to ensure proper indexing and efficient retrieval of elements.

Data Integrity and Immutability:

Hashing is often used to verify data integrity or to create unique identifiers for objects. By providing a custom hash function, you can ensure that the hash value remains consistent and reliable for objects of HashList and HashDict. This can be valuable in scenarios where immutability and data integrity are important, such as caching, memoization, or cryptographic applications.

HashList

class HashList(builtins.list)

 |  HashList(iterable=(), /)
 |  
 |  A subclass of list that provides a custom hash function.
 |  
 |  This class overrides the __hash__() method to compute a hash value based on the elements of the list.
 |  The hash value is calculated by alternating between adding and subtracting the hash values of the elements.
 |  The method uses the `fla_tu()` function to flatten the list and its nested elements before computing the hash.
 |  
 |  Note:
 |      The order of the elements matters when computing the hash.
 |  
 |  Examples:
 |      l1 = HashList([1, 2, 3, 4, {1: 2, 3: 4}])
 |      print(hash(l1))
 |      # Output: -5103430572081493847
 |  
 |  Method resolution order:
 |      HashList
 |      builtins.list
 |      builtins.object
 

HashDict

class HashDict(builtins.dict)

|  A subclass of dict that provides a custom hash function.
|  
|  This class overrides the __hash__() method to compute a hash value based on the key-value pairs of the dictionary.
|  The hash value is calculated by alternating between adding and subtracting the hash values of the keys and values.
|  The method uses the `fla_tu()` function to flatten the dictionary and its nested elements before computing the hash.
|  
|  Note:
|      The order of the key-value pairs matters when computing the hash.
|  
|  Examples:
|      d1 = HashDict({1: 'a', 2: 'b', 3: 'c'})
|      print(hash(d1))
|      # Output: -5076628985615637757
|  
|  Method resolution order:
|      HashDict
|      builtins.dict
|      builtins.object

Examples

from hashlistdict import HashList, HashDict
l1 = HashList([1, 2, 3, 4, {1: 2, 3: 4}])

l2 = HashList([1, 2, 3, 4, {1: 2, 3: 4}])
l3 = HashList([1, 2, 4, 3])

print(hash(l1))
print(hash(l2))
print(l3)
di = {l1: "baba", l2: "bubu", l3: "bobo"}  # baba will be overwritten - same hash
print(di)

# {[1, 2, 3, 4, {1: 2, 3: 4}]: 'bubu', [1, 2, 4, 3]: 'bobo'}

import numpy as np

l11 = HashList(
    [
        {1: 2, 3: 4},
        1,
        2,
        3,
        4,
    ]
)

l22 = HashList([1, 2, 3, 4, {1: 2, 3: 4}])
l33 = HashList([1, 2, 4, 3, {3: 4, 1: 2}])
l44 = HashList([np.array([1, 2, 4, 3]), {3: 4, 1: 2}])
l55 = HashList([[1, 2, 4, 3], {3: 4, 1: 2}])

di2 = {l11: "baba", l22: "bubu", l33: "bobo", l44: "bibi", l55: "bebe"}

print(di2)

# [1, 2, 4, 3] {[1, 2, 3, 4, {1: 2, 3: 4}]: 'bubu', [1, 2, 4, 3]: 'bobo'} {[{1: 2, 3: 4}, 1, 2, 3, 4]: 'baba', [1, 2,
# 3, 4, {1: 2, 3: 4}]: 'bubu', [1, 2, 4, 3, {3: 4, 1: 2}]: 'bobo', [array([1, 2, 4, 3]), {3: 4, 1: 2}]: 'bibi', [[1,
# 2, 4, 3], {3: 4, 1: 2}]: 'bebe'}


# Test HashList with nested dictionaries
l1 = HashList([{1: 2, 3: 4}, 1, 2, 3, 4])
l2 = HashList([1, 2, 3, 4, {1: 2, 3: 4}])
l3 = HashList([1, 2, 4, 3, {3: 4, 1: 2}])

print(f'{hash(l1)=}')
print(f'{hash(l2)=}')
print(f'{hash(l3)=}')
# Output: <hash values>

# Test HashList with numpy arrays
l4 = HashList([np.array([1, 2, 4, 3]), {3: 4, 1: 2}])
l5 = HashList([[1, 2, 4, 3], {3: 4, 1: 2}])

print(f'{hash(l4)=}')
print(f'{hash(l5)=}')

# Test HashDict with nested dictionaries
d1 = HashDict({1: {2: 3}, 4: {5: 6}})
d2 = HashDict({1: {2: 3}, 4: {5: 6}})
d3 = HashDict({1: {4: 5}, 2: {3: 6}})

print(f'{hash(d1)=}')
print(f'{hash(d2)=}')
print(f'{hash(d3)=}')

# Output: <hash values>

# hash(l1)=-1436120659400041378
# hash(l2)=-5103430572081493847
# hash(l3)=3618454608618990865
# hash(l4)=3029580590484665753
# hash(l5)=1100456941032353086
# hash(d1)=-415071809182110355
# hash(d2)=-415071809182110355
# hash(d3)=4338543280270579718

Metadata

Release files for hashlistdict 0.11

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

Source distribution (sdist)

Source distribution for hashlistdict 0.11
File Size Uploaded Provenance Actions
hashlistdict-0.11.tar.gz 5.7 kB Without provenance Details for hashlistdict-0.11.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hashlistdict 0.11
File Interpreter ABI Platform Actions
hashlistdict-0.11-py3-none-any.whl Python 3 none any Details for hashlistdict-0.11-py3-none-any.whl

Total release size: 13.3 kB

Release files / hashlistdict-0.11.tar.gz

Download URL hashlistdict-0.11.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
646c294fdd50131e27444b4a2024e4943139fee9b78a2574c383feb5b34de269
BLAKE2b-256 checksum
How to use checksums
fb86ecb9820bcf808bc21f34e13e8ed7480e123ead21f55d69391eb725ae3da4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release files / hashlistdict-0.11-py3-none-any.whl

Download URL hashlistdict-0.11-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4fe4f862150f5472c0529612580c5fe175b4dc600567ebd7b2564228d4483d18
BLAKE2b-256 checksum
How to use checksums
b439afeae4013804a3d232252756342e2ecac9889d04eb6dd87ec5cb38f51c62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Security for hashlistdict 0.11

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.

Release history Release notifications | RSS feed

This release

0.11 This release

2 release files No provenance

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