Memoria
Memoria is a Python library for hashing and caching.
Installation
pip install memoria
Benefits of Memoria
Consistency
Unfortunately the built-in hash method is not consistent.
For example if you hash a string in two different Python sessions,
e.g., hash('hello world!')
, you may get different results, e.g., -69600567246316219
or
-8701498716516122875
. However, Memoria is consistent, e.g.,
memoria.hash('hello world!')
produces PwDVM4wattDXKR1HUtszcPP5BHTUVTYQ5X0cO51yAn4=
.
We should credit the built-in library hashlib of course.
Hashing Unhashable Types
Memoria can hash virtually anything. If you use the built-in
hash method hash(dict())
or hash(list())
you will get an error:
TypeError: unhashable type: 'dict'
but Memoria can even hash
unhashable types by converting them into a hashable type and
hashing the result. To make sure that the hash is still different between
the original type and the hashable representation, Memoria takes some
additional measures.
Usage
import memoria
memoria.hash(123)
memoria.hash(123, base=32)
memoria.hash({'name': 'John', 'age': 24})