Skip to content

treeo.compact

A decorator that enable the definition of Tree subnodes at runtime.

Source code in treeo/api.py
def compact(f):
    """
    A decorator that enable the definition of Tree subnodes at runtime.
    """

    if hasattr(f, "_treeo_mutable"):
        raise ValueError(
            f"""Cannot make 'compact' a 'mutable' function, invert the order. If you are using it as a decorator, instead of e.g.

    @compact
    @mutable
    def {f.__name__}(self, ...):

use:

    @mutable
    @compact
    def {f.__name__}(self, ...):

"""
        )

    @functools.wraps(f)
    def wrapper(tree, *args, **kwargs):
        with tree_m._COMPACT_CONTEXT.compact(f, tree):
            return f(tree, *args, **kwargs)

    wrapper._treeo_compact = True

    return wrapper