spekk.trees.registry.DuckTypedTreeDef#

class spekk.trees.registry.DuckTypedTreeDef(obj: Any)[source]#

Bases: TreeDef

An object can be a TreeDef if it has the dunder-methods: __spekk_treedef_keys__, __spekk_treedef_get__, and __spekk_treedef_create__.

__init__(obj: Any)[source]#

Methods

__init__(obj)

create(keys, values)

Create a new instance of the tree with the given keys and values.

get(key)

Get the subtree at the given key.

items()

keys()

Get the keys that can be used to get each subtree.

new_class(keys_fn, get_fn, create_fn)

Helper function for creating a new TreeDef class.

values()

Get the subtrees for each key in the treedef.

create(keys: Sequence, values: Sequence) Any[source]#

Create a new instance of the tree with the given keys and values.

>>> td = treedef({"a": 1, "b": 2})
>>> td.create(["a", "b"], [3, 4])
{'a': 3, 'b': 4}
get(key: Any)[source]#

Get the subtree at the given key.

keys() Sequence[source]#

Get the keys that can be used to get each subtree.

static new_class(keys_fn: Callable[[Mapping[Any, Mapping[Any, Tree] | Sequence[Tree] | Any] | Sequence[Mapping[Any, Tree] | Sequence[Tree] | Any] | Any], Sequence], get_fn: Callable[[Mapping[Any, Mapping[Any, Tree] | Sequence[Tree] | Any] | Sequence[Mapping[Any, Tree] | Sequence[Tree] | Any] | Any, Any], Any], create_fn: Callable[[Sequence, Sequence], Mapping[Any, Mapping[Any, Tree] | Sequence[Tree] | Any] | Sequence[Mapping[Any, Tree] | Sequence[Tree] | Any] | Any]) TreeDef#

Helper function for creating a new TreeDef class.

values() Sequence#

Get the subtrees for each key in the treedef.