Skip to content

recursive

batcharray.recursive

Contain features to easily work on nested objects.

batcharray.recursive.ApplyState dataclass

Store the current state.

batcharray.recursive.AutoApplier

Bases: BaseApplier[Any]

Implement an applier that can automatically call other appliers based on the data type.

batcharray.recursive.AutoApplier.add_applier classmethod

add_applier(
    data_type: type,
    applier: BaseApplier,
    exist_ok: bool = False,
) -> None

Add an applier for a given data type.

Parameters:

Name Type Description Default
data_type type

The data type for this test.

required
applier BaseApplier

The applier object.

required
exist_ok bool

If False, RuntimeError is raised if the data type already exists. This parameter should be set to True to overwrite the applier for a type.

False

Raises:

Type Description
RuntimeError

if a applier is already registered for the data type and exist_ok=False.

Example usage:

>>> from batcharray.recursive import AutoApplier, SequenceApplier
>>> AutoApplier.add_applier(list, SequenceApplier(), exist_ok=True)

batcharray.recursive.AutoApplier.find_applier classmethod

find_applier(data_type: Any) -> BaseApplier

Find the applier associated to an object.

Parameters:

Name Type Description Default
data_type Any

The data type to get.

required

Returns:

Type Description
BaseApplier

The applier associated to the data type.

Example usage:

>>> from batcharray.recursive import AutoApplier
>>> AutoApplier.find_applier(list)
SequenceApplier()

batcharray.recursive.AutoApplier.has_applier classmethod

has_applier(data_type: type) -> bool

Indicate if an applier is registered for the given data type.

Parameters:

Name Type Description Default
data_type type

The data type to check.

required

Returns:

Type Description
bool

True if an applier is registered, otherwise False.

Example usage:

>>> from batcharray.recursive import AutoApplier
>>> AutoApplier.has_applier(list)
True
>>> AutoApplier.has_applier(str)
False

batcharray.recursive.BaseApplier

Bases: ABC, Generic[T]

Define the base class to implement a recursive applier.

batcharray.recursive.BaseApplier.apply abstractmethod

apply(data: T, func: Callable, state: ApplyState) -> T

Recursively apply a function on all the items in a nested data.

Parameters:

Name Type Description Default
data T

The input data.

required
func Callable

The function to apply on each item.

required
state ApplyState

The current state.

required

Returns:

Type Description
T

The transformed data.

batcharray.recursive.DefaultApplier

Bases: BaseApplier[Any]

Define the default applier.

Example usage:

>>> from batcharray.recursive import DefaultApplier, AutoApplier, ApplyState
>>> state = ApplyState(applier=AutoApplier())
>>> applier = DefaultApplier()
>>> applier
DefaultApplier()
>>> out = applier.apply([1, "abc"], str, state)
>>> out
"[1, 'abc']"

batcharray.recursive.MappingApplier

Bases: BaseApplier[T]

Define an applier for mappings/dictionaries.

Example usage:

>>> from batcharray.recursive import MappingApplier, AutoApplier, ApplyState
>>> state = ApplyState(applier=AutoApplier())
>>> applier = MappingApplier()
>>> applier
MappingApplier()
>>> out = applier.apply({"a": 1, "b": "abc"}, str, state)
>>> out
{'a': '1', 'b': 'abc'}

batcharray.recursive.SequenceApplier

Bases: BaseApplier[T]

Define a applier for sequences/lists/tuples.

Example usage:

>>> from batcharray.recursive import SequenceApplier, AutoApplier, ApplyState
>>> state = ApplyState(applier=AutoApplier())
>>> applier = SequenceApplier()
>>> applier
SequenceApplier()
>>> out = applier.apply([1, "abc"], str, state)
>>> out
['1', 'abc']

batcharray.recursive.recursive_apply

recursive_apply(data: Any, func: Callable) -> Any

Recursively apply a function on all the items in a nested data.

Parameters:

Name Type Description Default
data Any

The input data.

required
func Callable

The function to apply on each item.

required

Returns:

Type Description
Any

The transformed data.

Example usage:

>>> from batcharray.recursive import recursive_apply
>>> out = recursive_apply({"a": 1, "b": "abc"}, str)
>>> out
{'a': '1', 'b': 'abc'}