undr.utilities
#
Data loading and number formatting functions.
Overview#
Functions#
Generates a human-readable representation of a duration. |
|
Consumes an iterable and calculates a hash. |
|
Calculates a file’s hash. |
|
Calculates the maximum number of bytes in a chunk that can be divided into full words. |
|
Reads and parses a JSON schema bundled with UNDR. |
|
Creates a new byte hasher. |
|
Appends a suffix to a path and returns a new path. |
|
Appends a suffix to a POSIX path and returns a new path. |
|
Generates a human-readable representation of a size. |
|
Generates a human-readable representation of a speed. |
Module Contents#
- undr.utilities.duration_to_string(duration: float) str #
Generates a human-readable representation of a duration.
- undr.utilities.hash(chunks: Iterable[bytes]) hashlib._Hash #
Consumes an iterable and calculates a hash.
Since this function consumes the hash, users should use
new_hash()
and callhashlib._Hash.update()
manually if they plan to do something else with the bytes.
- undr.utilities.hash_file(path: pathlib.Path, chunk_size: int)#
Calculates a file’s hash.
- Parameters:
path (pathlib.Path) – Path of the file to hash.
chunk_size (int) – Chunk size in bytes, used to read the file.
- Returns:
_description_
- Return type:
_type_
- undr.utilities.least_multiple_over_chunk_size(word_size: int) int #
Calculates the maximum number of bytes in a chunk that can be divided into full words.
For instance, for a chunks size of 100 bytes and a word size of 32 bytes, this function would return 96.
- Parameters:
word_size (int) – The word size in bytes. The chunk size is not a parameter since UNDR always uses
undr.constants.CHUNK_SIZE
.- Returns:
Maximum number of bytes in a chunk that can be divided into full words. This number is guaranteed to be a multiple of word_size. It may be zero.
- Return type:
- undr.utilities.load_schema(name: str) jsonschema_rs.JSONSchema #
Reads and parses a JSON schema bundled with UNDR.
- Parameters:
name (str) – Name of the schema.
- Returns:
JSON schema validator.
- Return type:
jsonschema_rs.JSONSchema
- undr.utilities.new_hash() hashlib._Hash #
Creates a new byte hasher.
- Returns:
SHA3-224 (FIPS 202) hasher.
- Return type:
hashlib._Hash
- undr.utilities.path_with_suffix(path: pathlib.Path, suffix: str) pathlib.Path #
Appends a suffix to a path and returns a new path.
- Parameters:
path (pathlib.Path) – Input path, not modified by this function.
suffix (str) – The string to append to the path’s last component.
- Returns:
New path with the given suffix.
- Return type:
- undr.utilities.posix_path_with_suffix(path: pathlib.PurePosixPath, suffix: str) pathlib.PurePosixPath #
Appends a suffix to a POSIX path and returns a new path.
Similar to
path_with_suffix()
for POSIX paths.- Parameters:
path (pathlib.PurePosixPath) – Input path, not modified by this function.
suffix (str) – The string to append to the path’s last component.
- Returns:
New path with the given suffix.
- Return type: