prefect.utilities.hashing
file_hash
Given a path to a file, produces a stable hash of the file contents.
Parameters:
Name | Description | Default |
---|---|---|
path |
the path to a file str |
required |
Returns:
Type | Description |
---|---|
str |
a hash of the file contents |
Source code in prefect/utilities/hashing.py
def file_hash(path: str) -> str:
"""Given a path to a file, produces a stable hash of the file contents.
Args:
path (str): the path to a file
Returns:
str: a hash of the file contents
"""
contents = Path(path).read_bytes()
return stable_hash(contents)
hash_objects
Attempt to hash objects by dumping to JSON or serializing with cloudpickle.
On failure of both, None
will be returned
Source code in prefect/utilities/hashing.py
def hash_objects(*args, **kwargs) -> Optional[str]:
"""
Attempt to hash objects by dumping to JSON or serializing with cloudpickle.
On failure of both, `None` will be returned
"""
try:
return stable_hash(json.dumps((args, kwargs), sort_keys=True))
except Exception:
pass
try:
return stable_hash(cloudpickle.dumps((args, kwargs)))
except Exception:
pass
return None
stable_hash
Given some arguments, produces a stable 64-bit hash of their contents.
Supports bytes and strings. Strings will be UTF-8 encoded.
Parameters:
Name | Description | Default |
---|---|---|
*args |
Items to include in the hash. Union[str, bytes] |
() |
Returns:
Type | Description |
---|---|
str |
A hex hash. |
Source code in prefect/utilities/hashing.py
def stable_hash(*args: Union[str, bytes]) -> str:
"""Given some arguments, produces a stable 64-bit hash of their contents.
Supports bytes and strings. Strings will be UTF-8 encoded.
Args:
*args: Items to include in the hash.
Returns:
A hex hash.
"""
h = hashlib.md5()
for a in args:
if isinstance(a, str):
a = a.encode()
h.update(a)
return h.hexdigest()
to_qualified_name
Given an object, returns its fully-qualified name, meaning a string that represents its Python import path
Parameters:
Name | Description | Default |
---|---|---|
obj |
an importable Python object Any |
required |
Returns:
Type | Description |
---|---|
str |
the qualified name |
Source code in prefect/utilities/hashing.py
def to_qualified_name(obj: Any) -> str:
"""
Given an object, returns its fully-qualified name, meaning a string that represents its
Python import path
Args:
obj (Any): an importable Python object
Returns:
str: the qualified name
"""
return obj.__module__ + "." + obj.__qualname__