brainstate.util.yield_unique_pretty_repr_items#
- brainstate.util.yield_unique_pretty_repr_items(node, repr_object=None, repr_attr=None)[source]#
Generate a pretty representation of an object while avoiding duplicate representations.
This function yields a structured representation of an object, using custom or default methods for representing the object itself and its attributes. It ensures that each object is only represented once to prevent infinite recursion in cases of circular references.
- Parameters:
node (
Any) – The object to be represented.repr_object (
Callable|None) – A callable that yields the representation of the object itself. If not provided, a default representation function is used.repr_attr (
Callable|None) – A callable that yields the representation of the object’s attributes. If not provided, a default attribute representation function is used.
- Yields:
Union[PrettyType, PrettyAttr] – The pretty representation of the object and its attributes, avoiding duplicates by tracking seen objects.
Examples
>>> class Node: ... def __init__(self, value, next=None): ... self.value = value ... self.next = next ... >>> # Create circular reference >>> node1 = Node(1) >>> node2 = Node(2, node1) >>> node1.next = node2 ... >>> # This will handle circular reference gracefully >>> for item in yield_unique_pretty_repr_items(node1): ... print(item)