checkpointing.hash

 1from typing import Any
 2from checkpointing.hash.generic import hash_generic
 3from checkpointing.config import defaults
 4from checkpointing.hash.stream import HashStream
 5
 6
 7def hash_anything(*objs: Any, algorithm: str = None, pickle_protocol: int = None) -> str:
 8    """
 9    Args:
10        objs: the objects to be hashed
11        algorithm: the hash algorithm. Must be supported by the hashlib.
12                   If it's not specified, use the global default `hash.algorithm`.
13        pickle_protocol: the pickle protocol to use for hashing objects that does not have an
14                         specific hasher, and thus using the pickle based fallback hasher.
15                         If it's not specified, user the global default `hash.pickle_protocol`
16
17    Returns: a hexdigest of the hash value
18
19    >>> hash_anything(0, "hello", [1, {"a": "b"}], pickle_protocol=3)
20    '656b476a9f8fb668107c756113b38d89'
21
22    Note that when hashing some objects, such as functions, lambdas, generators, etc, it only
23    hashes the reference to their definition.  This could result in unexpected behaviors leading
24    to incorrect equality comparison.
25
26    >>> def foo():
27    ...     for i in range(10):
28    ...         yield i
29    >>>
30    >>> f1 = foo()
31    >>> f2 = foo()
32    >>> next(f2) # Now the state of f1 and f2 are different
33    0
34    >>> # But the are hashed by reference only,
35    >>> # So their hash value is considered the same
36    >>> hash_anything(f1) == hash_anything(f2) 
37    True
38    """
39
40    if algorithm is None:
41        algorithm = defaults["hash.algorithm"]
42
43    if pickle_protocol is None:
44        pickle_protocol = defaults["hash.pickle_protocol"]
45
46    hash_stream = HashStream(algorithm)
47
48    for obj in objs:
49        hash_generic(hash_stream, obj, pickle_protocol)
50
51    return hash_stream.hexdigest()
def hash_anything(*objs: Any, algorithm: str = None, pickle_protocol: int = None) -> str:
 8def hash_anything(*objs: Any, algorithm: str = None, pickle_protocol: int = None) -> str:
 9    """
10    Args:
11        objs: the objects to be hashed
12        algorithm: the hash algorithm. Must be supported by the hashlib.
13                   If it's not specified, use the global default `hash.algorithm`.
14        pickle_protocol: the pickle protocol to use for hashing objects that does not have an
15                         specific hasher, and thus using the pickle based fallback hasher.
16                         If it's not specified, user the global default `hash.pickle_protocol`
17
18    Returns: a hexdigest of the hash value
19
20    >>> hash_anything(0, "hello", [1, {"a": "b"}], pickle_protocol=3)
21    '656b476a9f8fb668107c756113b38d89'
22
23    Note that when hashing some objects, such as functions, lambdas, generators, etc, it only
24    hashes the reference to their definition.  This could result in unexpected behaviors leading
25    to incorrect equality comparison.
26
27    >>> def foo():
28    ...     for i in range(10):
29    ...         yield i
30    >>>
31    >>> f1 = foo()
32    >>> f2 = foo()
33    >>> next(f2) # Now the state of f1 and f2 are different
34    0
35    >>> # But the are hashed by reference only,
36    >>> # So their hash value is considered the same
37    >>> hash_anything(f1) == hash_anything(f2) 
38    True
39    """
40
41    if algorithm is None:
42        algorithm = defaults["hash.algorithm"]
43
44    if pickle_protocol is None:
45        pickle_protocol = defaults["hash.pickle_protocol"]
46
47    hash_stream = HashStream(algorithm)
48
49    for obj in objs:
50        hash_generic(hash_stream, obj, pickle_protocol)
51
52    return hash_stream.hexdigest()
Args
  • objs: the objects to be hashed
  • algorithm: the hash algorithm. Must be supported by the hashlib. If it's not specified, use the global default hash.algorithm.
  • pickle_protocol: the pickle protocol to use for hashing objects that does not have an specific hasher, and thus using the pickle based fallback hasher. If it's not specified, user the global default hash.pickle_protocol

Returns: a hexdigest of the hash value

>>> hash_anything(0, "hello", [1, {"a": "b"}], pickle_protocol=3)
'656b476a9f8fb668107c756113b38d89'

Note that when hashing some objects, such as functions, lambdas, generators, etc, it only hashes the reference to their definition. This could result in unexpected behaviors leading to incorrect equality comparison.

>>> def foo():
...     for i in range(10):
...         yield i
>>>
>>> f1 = foo()
>>> f2 = foo()
>>> next(f2) # Now the state of f1 and f2 are different
0
>>> # But the are hashed by reference only,
>>> # So their hash value is considered the same
>>> hash_anything(f1) == hash_anything(f2) 
True