hashlistdict


Namehashlistdict JSON
Version 0.11 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/hashlistdict
SummaryHashable dict/list - subclasses of list/dict with a custom hash function based on alternating addition and subtraction of element hashes - pure Python
upload_time2023-07-14 15:08:06
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords nested list dict hash hashable
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# 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

```python
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

 ```python
 
 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 
 
```python

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

```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/hashlistdict",
    "name": "hashlistdict",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "nested,list,dict,hash,hashable",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/fb/86/ecb9820bcf808bc21f34e13e8ed7480e123ead21f55d69391eb725ae3da4/hashlistdict-0.11.tar.gz",
    "platform": null,
    "description": "\r\n# Hashable dict/list - Subclasses of list/dict with a custom hash function based on alternating addition and subtraction of element hashes - pure Python\r\n\r\n## pip install hashlistdict \r\n\r\n#### Tested against Windows 10 / Python 3.10 / Anaconda \r\n\r\n\r\nThe HashList and HashDict classes provide a custom hash function implementation for lists and dictionaries, respectively. \r\nThe advantages and potential use cases for these classes are as follows:\r\n\r\n### Customized Hashing: \r\n\r\nBy overriding the **\\_\\_hash\\_\\_()** method, the classes allow you to define a custom hash function based on the elements of the list or \r\nthe key-value pairs of the dictionary. This gives you more control over the hashing process and \r\nallows you to incorporate specific logic or considerations when computing the hash value.\r\n\r\n### Hash-Based Data Structures: \r\n\r\nThe 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 \r\ndictionaries and sets. Since the hash value of an object is used to determine its position within a hash table, having a custom \r\nhash function can be useful when you want to ensure proper indexing and efficient retrieval of elements.\r\n\r\n### Data Integrity and Immutability: \r\n\r\nHashing is often used to verify data integrity or to create unique identifiers for objects. \r\nBy providing a custom hash function, you can ensure that the hash value remains consistent and \r\nreliable for objects of HashList and HashDict. This can be valuable in scenarios where immutability \r\nand data integrity are important, such as caching, memoization, or cryptographic applications.\r\n\r\n\r\n## HashList\r\n\r\n```python\r\nclass HashList(builtins.list)\r\n\r\n |  HashList(iterable=(), /)\r\n |  \r\n |  A subclass of list that provides a custom hash function.\r\n |  \r\n |  This class overrides the __hash__() method to compute a hash value based on the elements of the list.\r\n |  The hash value is calculated by alternating between adding and subtracting the hash values of the elements.\r\n |  The method uses the `fla_tu()` function to flatten the list and its nested elements before computing the hash.\r\n |  \r\n |  Note:\r\n |      The order of the elements matters when computing the hash.\r\n |  \r\n |  Examples:\r\n |      l1 = HashList([1, 2, 3, 4, {1: 2, 3: 4}])\r\n |      print(hash(l1))\r\n |      # Output: -5103430572081493847\r\n |  \r\n |  Method resolution order:\r\n |      HashList\r\n |      builtins.list\r\n |      builtins.object\r\n \r\n ```\r\n \r\n ## HashDict\r\n\r\n ```python\r\n \r\n class HashDict(builtins.dict)\r\n \r\n |  A subclass of dict that provides a custom hash function.\r\n |  \r\n |  This class overrides the __hash__() method to compute a hash value based on the key-value pairs of the dictionary.\r\n |  The hash value is calculated by alternating between adding and subtracting the hash values of the keys and values.\r\n |  The method uses the `fla_tu()` function to flatten the dictionary and its nested elements before computing the hash.\r\n |  \r\n |  Note:\r\n |      The order of the key-value pairs matters when computing the hash.\r\n |  \r\n |  Examples:\r\n |      d1 = HashDict({1: 'a', 2: 'b', 3: 'c'})\r\n |      print(hash(d1))\r\n |      # Output: -5076628985615637757\r\n |  \r\n |  Method resolution order:\r\n |      HashDict\r\n |      builtins.dict\r\n |      builtins.object\r\n \r\n ```\r\n \r\n \r\n ### Examples \r\n \r\n```python\r\n\r\nfrom hashlistdict import HashList, HashDict\r\nl1 = HashList([1, 2, 3, 4, {1: 2, 3: 4}])\r\n\r\nl2 = HashList([1, 2, 3, 4, {1: 2, 3: 4}])\r\nl3 = HashList([1, 2, 4, 3])\r\n\r\nprint(hash(l1))\r\nprint(hash(l2))\r\nprint(l3)\r\ndi = {l1: \"baba\", l2: \"bubu\", l3: \"bobo\"}  # baba will be overwritten - same hash\r\nprint(di)\r\n\r\n# {[1, 2, 3, 4, {1: 2, 3: 4}]: 'bubu', [1, 2, 4, 3]: 'bobo'}\r\n\r\nimport numpy as np\r\n\r\nl11 = HashList(\r\n    [\r\n        {1: 2, 3: 4},\r\n        1,\r\n        2,\r\n        3,\r\n        4,\r\n    ]\r\n)\r\n\r\nl22 = HashList([1, 2, 3, 4, {1: 2, 3: 4}])\r\nl33 = HashList([1, 2, 4, 3, {3: 4, 1: 2}])\r\nl44 = HashList([np.array([1, 2, 4, 3]), {3: 4, 1: 2}])\r\nl55 = HashList([[1, 2, 4, 3], {3: 4, 1: 2}])\r\n\r\ndi2 = {l11: \"baba\", l22: \"bubu\", l33: \"bobo\", l44: \"bibi\", l55: \"bebe\"}\r\n\r\nprint(di2)\r\n\r\n# [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,\r\n# 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,\r\n# 2, 4, 3], {3: 4, 1: 2}]: 'bebe'}\r\n\r\n\r\n# Test HashList with nested dictionaries\r\nl1 = HashList([{1: 2, 3: 4}, 1, 2, 3, 4])\r\nl2 = HashList([1, 2, 3, 4, {1: 2, 3: 4}])\r\nl3 = HashList([1, 2, 4, 3, {3: 4, 1: 2}])\r\n\r\nprint(f'{hash(l1)=}')\r\nprint(f'{hash(l2)=}')\r\nprint(f'{hash(l3)=}')\r\n# Output: <hash values>\r\n\r\n# Test HashList with numpy arrays\r\nl4 = HashList([np.array([1, 2, 4, 3]), {3: 4, 1: 2}])\r\nl5 = HashList([[1, 2, 4, 3], {3: 4, 1: 2}])\r\n\r\nprint(f'{hash(l4)=}')\r\nprint(f'{hash(l5)=}')\r\n\r\n# Test HashDict with nested dictionaries\r\nd1 = HashDict({1: {2: 3}, 4: {5: 6}})\r\nd2 = HashDict({1: {2: 3}, 4: {5: 6}})\r\nd3 = HashDict({1: {4: 5}, 2: {3: 6}})\r\n\r\nprint(f'{hash(d1)=}')\r\nprint(f'{hash(d2)=}')\r\nprint(f'{hash(d3)=}')\r\n\r\n# Output: <hash values>\r\n\r\n# hash(l1)=-1436120659400041378\r\n# hash(l2)=-5103430572081493847\r\n# hash(l3)=3618454608618990865\r\n# hash(l4)=3029580590484665753\r\n# hash(l5)=1100456941032353086\r\n# hash(d1)=-415071809182110355\r\n# hash(d2)=-415071809182110355\r\n# hash(d3)=4338543280270579718\r\n\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Hashable dict/list - subclasses of list/dict with a custom hash function based on alternating addition and subtraction of element hashes - pure Python",
    "version": "0.11",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/hashlistdict"
    },
    "split_keywords": [
        "nested",
        "list",
        "dict",
        "hash",
        "hashable"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b439afeae4013804a3d232252756342e2ecac9889d04eb6dd87ec5cb38f51c62",
                "md5": "bde31671c55219f2fc162bd14c2a8ce5",
                "sha256": "4fe4f862150f5472c0529612580c5fe175b4dc600567ebd7b2564228d4483d18"
            },
            "downloads": -1,
            "filename": "hashlistdict-0.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bde31671c55219f2fc162bd14c2a8ce5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 7649,
            "upload_time": "2023-07-14T15:08:05",
            "upload_time_iso_8601": "2023-07-14T15:08:05.292190Z",
            "url": "https://files.pythonhosted.org/packages/b4/39/afeae4013804a3d232252756342e2ecac9889d04eb6dd87ec5cb38f51c62/hashlistdict-0.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fb86ecb9820bcf808bc21f34e13e8ed7480e123ead21f55d69391eb725ae3da4",
                "md5": "5a6e3281ab13f76afb97eab8f4f81d68",
                "sha256": "646c294fdd50131e27444b4a2024e4943139fee9b78a2574c383feb5b34de269"
            },
            "downloads": -1,
            "filename": "hashlistdict-0.11.tar.gz",
            "has_sig": false,
            "md5_digest": "5a6e3281ab13f76afb97eab8f4f81d68",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5682,
            "upload_time": "2023-07-14T15:08:06",
            "upload_time_iso_8601": "2023-07-14T15:08:06.973463Z",
            "url": "https://files.pythonhosted.org/packages/fb/86/ecb9820bcf808bc21f34e13e8ed7480e123ead21f55d69391eb725ae3da4/hashlistdict-0.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-14 15:08:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "hashlistdict",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "hashlistdict"
}
        
Elapsed time: 0.09044s