preheat_open.query.query_stuff
- preheat_open.query.query_stuff(obj, query, sub_obj_attrs, sub_obj_attrs_for_removal=None, query_type=None, include_obj=False, **kwargs)
Recursively query objects and their sub-objects based on specified criteria.
This function performs a recursive search through an object and its nested attributes, yielding all objects that match the provided query criteria. It supports filtering by multiple queries and can traverse complex object hierarchies.
- Parameters:
obj (object) – The root object to start querying from
query (Query | Sequence[Query | dict] | None) – The query or list of queries to apply for filtering
sub_obj_attrs (list[str]) – Attribute names containing sub-objects to query
sub_obj_attrs_for_removal (list[str], optional) – Attributes to skip in nested queries
query_type (type) – The Query class type to use for filtering
include_obj (bool) – Whether to include the root object if it matches
kwargs – Additional keyword arguments for query creation
- Returns:
Generator yielding objects that match the query
- Return type:
Generator[object, None, None]
- Example:
>>> # Create a simple object hierarchy for demonstration >>> from dataclasses import field >>> @dataclass ... class Person: ... name: str ... children: list = None ... def __post_init__(self): ... self.children = self.children or [] >>> >>> @dataclass(eq=False) ... class PersonQuery(Query): ... name: list[str] = field(default_factory=list) ... _class = Person >>> >>> # Create test data >>> child1 = Person("Alice") >>> child2 = Person("Bob") >>> parent = Person("Charlie", [child1, child2]) >>> >>> # Query for specific name >>> results = list(query_stuff( ... parent, ... PersonQuery(name="Alice"), ... ["children"], ... query_type=PersonQuery ... )) >>> len(results) 1 >>> results[0].name 'Alice'