PrettyDict#

class brainstate.util.PrettyDict#

Base dictionary class with pretty representation and tree utilities.

This class extends the built-in dict with pretty printing capabilities and provides base methods for tree operations. It serves as the parent class for NestedDict and FlattedDict.

__module__#

Module identifier set to ‘brainstate.util’.

Type:

str

filter(*filters)[source]#

Filter the dictionary based on filters (abstract method).

This method must be implemented by subclasses.

Parameters:

*filters (type | str | Callable[[Tuple[Key, ...], Any], bool] | bool | EllipsisType | None | Tuple[type | str | Callable[[Tuple[Key, ...], Any], bool] | bool | EllipsisType | None | Tuple[Filter, ...] | List[Filter], ...] | List[type | str | Callable[[Tuple[Key, ...], Any], bool] | bool | EllipsisType | None | Tuple[Filter, ...] | List[Filter]]) – Filter specifications to apply.

Raises:

NotImplementedError – This is an abstract method.

Return type:

PrettyDict | Tuple[PrettyDict, ...]

merge(*states)[source]#

Merge multiple dictionaries (abstract method).

This method must be implemented by subclasses.

Parameters:

*states (PrettyDict) – Additional PrettyDict objects to merge.

Raises:

NotImplementedError – This is an abstract method.

Return type:

PrettyDict

split(*filters)[source]#

Split the dictionary based on filters (abstract method).

This method must be implemented by subclasses.

Parameters:

*filters (type | str | Callable[[Tuple[Key, ...], Any], bool] | bool | EllipsisType | None | Tuple[type | str | Callable[[Tuple[Key, ...], Any], bool] | bool | EllipsisType | None | Tuple[Filter, ...] | List[Filter], ...] | List[type | str | Callable[[Tuple[Key, ...], Any], bool] | bool | EllipsisType | None | Tuple[Filter, ...] | List[Filter]]) – Filter specifications to split the dictionary.

Raises:

NotImplementedError – This is an abstract method.

Return type:

PrettyDict | Tuple[PrettyDict, ...]

subset(*filters)[source]#

Subset a PrettyDict into one or more PrettyDict instances.

The user must pass at least one Filter (e.g., State), and the filters must be exhaustive (i.e., they must cover all State types in the PrettyDict).

Parameters:

*filters (type | str | Callable[[Tuple[Key, ...], Any], bool] | bool | EllipsisType | None | Tuple[type | str | Callable[[Tuple[Key, ...], Any], bool] | bool | EllipsisType | None | Tuple[Filter, ...] | List[Filter], ...] | List[type | str | Callable[[Tuple[Key, ...], Any], bool] | bool | EllipsisType | None | Tuple[Filter, ...] | List[Filter]]) – Filter specifications for subsetting.

Returns:

One or more subsetted dictionaries.

Return type:

PrettyDict | Tuple[PrettyDict, ...]

to_dict()[source]#

Convert the PrettyDict to a standard Python dictionary.

Returns:

A standard dictionary representation.

Return type:

Dict[TypeVar(K, bound= Hashable), Dict[TypeVar(K, bound= Hashable), Any] | TypeVar(V)]

treefy_state()[source]#

Convert State objects to a reference tree of the state.

This method traverses the tree structure and converts any State objects to their reference form using to_state_ref().

Returns:

A tree structure where State objects are replaced with their references.

Return type:

Any

Examples

>>> from brainstate._state import State
>>> d = PrettyDict({'a': State(1), 'b': 2})
>>> ref_tree = d.treefy_state()