Decorify

Functions

timeit

Tool for measuring execution time of small code snippets.

Submodules

Package Contents

timeit(accuracy: int = 2)

Decorator for measuring execution time of a function.

Parameters:

accuracy (int) – Rounding place of the value.

Returns:

Wrapped function that returns a tuple of original function’s result and measured execution time.

Return type:

function

Perfomes the grid search on passed arguments. The function can either return found arguments and it’s value, only value or dictionary of all search aruments and it’s values.

Parameters:
  • argument_parameters (int) – Rounding place of the value.

  • return_all_values (bool) – If True wrapped function returns dictionary {Parameters:value}

  • key (Callable, default=max) – Performes following function to find specific value

  • return_without_arguments (bool) – Returns only found value

Returns:

Wrapped function that returns a tuple of found arguments and their value for function.

Return type:

function

timeout(time: float, default_value: Any = mp_TimeoutError)

Decorated function is run in new process while still sharing memory, if the time limit is reached function returns default value or raises TimeoutError if default_value is not set.

Parameters:
  • time (Any) – The maximum allowed time for the function to execute, in seconds.

  • default_value – Default output value if timeout is reached. If value not changed, decorator raises TimeoutError.

Raises:

TimeoutError: – Raises TimeoutError if default_value argument is TimeoutError and specified time is reached.

Returns:

Functions output if function reached in time limit or default value if such value was set, and time limit was reached.

Return type:

function

default_value(default_value: Any = None, *, logger: logging.Logger | None = None)

Decorator for assigning default value if function fails

Parameters:
  • default_value (Any) – Default value which is set if the funciton fails

  • logger (logging.Logger) – Logger for logging warning if the function failed and the default value was returned

Returns:

Wrapped function that returns defealut value if exception is raised

Return type:

function

validate_typehints()

Checks if arguments passed to wrapped functions, are instances of typehint classes If not raises Value error. Decorator does not change the return value, it’s recommended if it’s important to check correctness of given types.

!! Be aware that this may increase the function runtime. Not recommended for very simple functions. !!

Raises:

Value Error: – Raises value error if passed arguments does not match the typehints.