Model navigation¶
Iterate depth-first over a tree of models.
A child model can sit in a plain field, a list, a dictionary, an
Override wrapper, or a
TaggedUnionModel union case, so walking the
tree by hand takes care.
iterate_models¶
Yields a model, then every model reachable from it, in depth-first pre-order.
The iteration descends model fields,
list[BaseModel], anddict[_, BaseModel], including a model held in anOverride. AnOverridethat is unset or cleared holds no model and is skipped.- Parameters:
The model to start from.
- Yields:
IteratedModel– Each model, a parent before its descendants. A model under two parents appears once for each.
IteratedModel¶
One model the iteration yields, with lazy access to its position in the tree.
Each read of
ancestors,index_path, orpathcosts O(depth). Caching the value can speed up a hot loop.An
IteratedModelcompares and hashes by identity, so it can go in a set. A model yielded twice gives twoIteratedModelinstances, and they compare unequal.You don’t create an
IteratedModeldirectly.iterate_models()yields them.The model at this position in the tree.
The enclosing models, outermost first. It’s
()for the root.
The list indices along the path from the root, without field names.
For example, iterating a slide gives
(2, 3)forcontents[2].object.group.children[3], and()for the root. Readancestorsorpathwhen field names matter.
The dotted path from the root. It’s
""for the root.For example, iterating a slide gives
contents[0].object.shape.text. A dictionary key goes throughrepr, sofield['a']andfield[3]stay distinct.
See also
Navigating a document efficiently: searching a document’s text, and filtering by what encloses a match.