spekk.trees.registry.DuckTypedTreeDef#
- class spekk.trees.registry.DuckTypedTreeDef(obj: Any)[source]#
Bases:
TreeDefAn object can be a
TreeDefif it has the dunder-methods:__spekk_treedef_keys__,__spekk_treedef_get__, and__spekk_treedef_create__.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
TreeDefclass.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}
- 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
TreeDefclass.
- values() Sequence#
Get the subtrees for each key in the treedef.