zea.utils¶
General utility functions.
Functions
|
Decorator that ensures asynchronous (gpu) operations complete before returning. |
|
Canonicalize an axis in [-num_dims, num_dims) to [0, num_dims). |
|
Converts a date string to a more readable format. |
|
Recursively compare two objects for equality. |
|
Generate a date string for current time, according to format specified by string. |
|
Maps negative indices for array indexing to positive indices. |
Clears line. |
|
|
Convert a string representation of truth to True or False. |
|
Updates dict1 with values dict2 |
Classes
A decorator class for timing the execution of functions. |
- class zea.utils.FunctionTimer[source]¶
Bases:
objectA decorator class for timing the execution of functions.
Example
>>> from zea.utils import FunctionTimer >>> timer = FunctionTimer() >>> my_function = lambda: sum(range(10)) >>> my_function = timer(my_function, name="my_function") >>> _ = my_function() >>> print(timer.get_stats("my_function")) {'mean': ..., 'median': ..., 'std_dev': ..., 'min': ..., 'max': ..., 'count': ...}
- zea.utils.block_until_ready(func)[source]¶
Decorator that ensures asynchronous (gpu) operations complete before returning.
- zea.utils.canonicalize_axis(axis, num_dims)[source]¶
Canonicalize an axis in [-num_dims, num_dims) to [0, num_dims).
- Return type:
int
- zea.utils.date_string_to_readable(date_string, include_time=False)[source]¶
Converts a date string to a more readable format.
- Parameters:
date_string (
str) – The input date string.include_time (
bool) – Whether to include the time in the output. Defaults to False.
- Returns:
The date string in a more readable format.
- Return type:
str
- zea.utils.get_date_string(string=None)[source]¶
Generate a date string for current time, according to format specified by string. Refer to the documentation of the datetime module for more information on the formatting options.
If no string is specified, the default format is used: “%Y_%m_%d_%H%M%S”.
- zea.utils.map_negative_indices(indices, num_dims)[source]¶
Maps negative indices for array indexing to positive indices. .. rubric:: Example
>>> from zea.utils import map_negative_indices >>> map_negative_indices([-1, -2], 5) [4, 3]
- zea.utils.print_clear_line()[source]¶
Clears line. Helpful when printing in a loop on the same line.
- zea.utils.strtobool(val)[source]¶
Convert a string representation of truth to True or False.
True values are ‘y’, ‘yes’, ‘t’, ‘true’, ‘on’, and ‘1’; false values are ‘n’, ‘no’, ‘f’, ‘false’, ‘off’, and ‘0’. Raises ValueError if ‘val’ is anything else.
- zea.utils.update_dictionary(dict1, dict2, keep_none=False)[source]¶
Updates dict1 with values dict2
- Parameters:
dict1 (
dict) – base dictionarydict2 (
dict) – update dictionarykeep_none (
bool) – whether to keep keys with None values in dict2. Defaults to False.
- Returns:
updated dictionary
- Return type:
dict