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

Project description

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

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

hashlistdict-0.11.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

hashlistdict-0.11-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file hashlistdict-0.11.tar.gz.

File metadata

  • Download URL: hashlistdict-0.11.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for hashlistdict-0.11.tar.gz
Algorithm Hash digest
SHA256 646c294fdd50131e27444b4a2024e4943139fee9b78a2574c383feb5b34de269
MD5 5a6e3281ab13f76afb97eab8f4f81d68
BLAKE2b-256 fb86ecb9820bcf808bc21f34e13e8ed7480e123ead21f55d69391eb725ae3da4

See more details on using hashes here.

File details

Details for the file hashlistdict-0.11-py3-none-any.whl.

File metadata

  • Download URL: hashlistdict-0.11-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for hashlistdict-0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 4fe4f862150f5472c0529612580c5fe175b4dc600567ebd7b2564228d4483d18
MD5 bde31671c55219f2fc162bd14c2a8ce5
BLAKE2b-256 b439afeae4013804a3d232252756342e2ecac9889d04eb6dd87ec5cb38f51c62

See more details on using hashes here.

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