Skip to content

General Utilities

drizm_commons.utils.decorators

memoize(fn)

A decorator that caches the last provided parameter for a function, until a new one is provided.

Only works for function with a single positional param.

Warning

This method is most likely not thread-safe and is only recommended to be used for scripts that do not rely on threading or multiprocessing.

Examples:

@memoize
def hello(name: str) -> None:
    print(f'Hello {name.capitalize().strip()}!')

hello("Ben")
hello()  # after the first run, we do not need to pass any params anymore

resolve_super_auto_resolution(fn)

A decorator that fixes the issues with the parameterless super call, on instance methods of classes created with type(), or types.new_class().

See: Issue 29944

Whether this is to be considered a bug or not is up to your use-case.

This decorator however can override the functions closure cell to effectively 'patch' this issue in the local scope of each method.

drizm_commons.utils.pathing

Path

A subclass of pathlib.Path.

With the exception of the below listed, overridden methods, the behaviour is identical to that of its superclass.

rmdir(self, recursive=True)

Remove this directory. By default, this method will recursively delete all contents of this directory.

Parameters:

Name Type Description Default
recursive Optional[bool]

If False, will only delete the directory if it is empty. Otherwise it will recursively delete its contents.

True

get_absolute_root_path()

Return the absolute path to the project root directory.

get_root_path_dirname()

Return the name of the project root directory.

drizm_commons.utils.various

Assortment of uncategorizable utility functions.

from drizm_commons.testing.various import *

camel_to_snake(name)

Converts camelCase names, to snake_case.

decorate_class_object_methods(cls, decorator, methods=None)

Programatically decorate all provided attributes of the class, with the passed decorator function.

Parameters:

Name Type Description Default
cls ~DecoratedClass

The class object to have its methods decorated.

required
decorator Callable

The decorator function to be applied to the classes methods.

required
methods Optional[Sequence[str]]

A list of attribute names to be decorated. Alternatively all non-dunder, instance methods will be decorated.

None

Returns:

Type Description
~DecoratedClass

The class with its methods decorated.

exclude_keys(dictionary, keys)

Removes all key-value pairs, matching the provided keys from the dictionary and returns a new one.

Does not crash when non-existant keys are provided.

Parameters:

Name Type Description Default
dictionary Mapping

A mapping-type from which keys should be removed.

required
keys Sequence[Hashable]

A sequence of hashable keys to be excluded from the new dictionary.

required

Returns:

Type Description
dict

A new dictionary minus the specified key-value pairs.