websauna.utils.traverseattribute module

Helpers to traverse trees with __parent__ or __wrapped__ like attributes.

websauna.utils.traverseattribute.traverse_attribute(obj, attribute_name)[source]

Turn a recursive parent traverse to a generator.

E.g. if you have:

>>> class Dummy:
...     __parent__ = None
...
>>> grandparent = Dummy()
>>> parent = Dummy()
>>> parent.__parent__ = grandparent
>>> obj = Dummy()
>>> obj.__parent__ = parent

You can turn this to a list:

>>> tree = traverse_attribute(obj, "__parent__")
>>> list(tree)
[obj, parent, grandparent]