For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).
Python class
TreeDef
TreeDef
class max.tree.TreeDef(kind, children=(), keys=(), meta=None)
Bases: object
The shape of a tree, with its leaves abstracted away.
Equal structures rebuild each other’s leaves. Hashing skips the payloads.
child_keys
keys, or positions if empty.
-
Type:
-
The key of each child
children
The structure of each child, in leaf order.
flatten_up_to()
flatten_up_to(tree, path='')
Flattens tree using this structure to locate the leaves.
The walk descends into tree wherever this structure has a
container and stops wherever it has a leaf, returning whatever
tree holds at that position. Use this to flatten a second tree the
same way as a first one, without a leaf predicate that might stop
at different positions.
The following example flattens a tree whose leaves are containers:
from max.experimental import tree_utils as tree
_, treedef = tree.flatten({"a": 1, "b": [2, 3]}, leaf=int)
other = {"a": [0], "b": [[], {}]}
assert treedef.flatten_up_to(other) == [[0], [], {}]-
Parameters:
-
Returns:
-
The value at each leaf position, in order.
-
Raises:
-
ValueError – If
treedoes not have this structure. -
Return type:
keys
One key per child, or empty when the children are positional.
kind
kind: str
"leaf" (a leaf slot), "static" (a
value carried as-is in meta), "ref" (a repeat of an earlier
value), or one of the container tags "list", "tuple",
"namedtuple", "dict" and "node".
-
Type:
-
What this position holds
leaf_paths
The dotted path to each leaf, in leaf order.
meta
meta: Any = None
the value itself for "static",
the referenced slot number for "ref", the concrete type for
"namedtuple", and a (type, metadata) pair for "node".
-
Type:
-
Whatever the kind needs to rebuild
num_leaves
property num_leaves: int
The number of leaf slots in this structure; a "ref" counts none.