Skip to content

util

util

Module for utility functions and classes used throughout the package.

T module-attribute

T = TypeVar('T')

C module-attribute

C = TypeVar('C', bound=Collection[T])

Patch

Patch(parent: Any, replacement: Any = None, key: str = None, as_dict: bool = False)

Class representing a replacement of an attribute on a module.

ATTRIBUTE DESCRIPTION
obj

Object to replace.

TYPE: Any

replacement

Object that replaces.

TYPE: Any

parent

Module or class to replace attribute.

TYPE: Any

parent instance-attribute

parent = parent

replacement instance-attribute

replacement = replacement

key instance-attribute

key = key

as_dict instance-attribute

as_dict = as_dict

orig instance-attribute

orig = parent[key]

patch

patch() -> None

Carries out the replacement of an object in a module/class.

restore

restore() -> None

Carries out the restoration of the original object on the objects module/class.

Patcher

Patcher(patches: Optional[List[Patch]] = None)

Bases: AbstractContextManager

Context manager that patches from a list of Patches on enter and restores the patch on exit.

ATTRIBUTE DESCRIPTION
patches

TYPE: List[Patch]

patches instance-attribute

patches = patches or []

entered instance-attribute

entered = False

add

add(patch: Patch) -> None

Adds a Patch to the patches. Also calls .patch() on the Patch.

PARAMETER DESCRIPTION
patch

Patch to add.

TYPE: Patch

__enter__

__enter__() -> Self

Enters the patching context. Calls .patch() on all patches.

RETURNS DESCRIPTION
Patcher

Patcher

TYPE: Self

__exit__

__exit__(exc_type, exc_val, exc_tb) -> None

Calls .restore() on all patches.

WrapperModule

Bases: Module

forward

forward(*args, **kwargs)

apply

apply(data: C, fn: Callable[[T], Any], cls: Type[T], inplace: bool = False) -> C

Applies some function to all members of a collection of a give type (or types)

PARAMETER DESCRIPTION
data

Collection of data to apply function to.

TYPE: Any

fn

Function to apply.

TYPE: Callable

cls

Type or Types to apply function to.

TYPE: type

inplace

If to apply the fn inplace. (For lists and dicts)

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Any

Same kind of collection as data, after then fn has been applied to members of given type.

TYPE: C

applyn

applyn(data: C, fn: Callable[[T], Any], cls: Type[T], inplace: bool = False) -> C

Applies some function to all members of a collection of a give type (or types)

PARAMETER DESCRIPTION
data

Collection of data to apply function to.

TYPE: Any

fn

Function to apply.

TYPE: Callable

cls

Type or Types to apply function to.

TYPE: type

inplace

If to apply the fn inplace. (For lists and dicts)

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Any

Same kind of collection as data, after then fn has been applied to members of given type.

TYPE: C

fetch_attr

fetch_attr(object: object, target: str) -> Any

Retrieves an attribute from an object hierarchy given an attribute path. Levels are separated by '.' e.x (transformer.h.1)

PARAMETER DESCRIPTION
object

Root object to get attribute from.

TYPE: object

target

Attribute path as '.' separated string.

TYPE: str

RETURNS DESCRIPTION
Any

Fetched attribute.

TYPE: Any

to_import_path

to_import_path(type: type) -> str

from_import_path

from_import_path(import_path: str) -> type