Skip to content

treeo.to_string

Converts a pytree to a string representation.

Parameters:

Name Type Description Default
obj Any

The pytree to convert.

required
private_fields bool

If True, private fields are included.

False
static_fields bool

If True, static fields are included.

True

Returns:

Type Description
str

A string representation of the pytree.

Source code in treeo/api.py
def to_string(
    obj: tp.Any,
    private_fields: bool = False,
    static_fields: bool = True,
    color: bool = False,
) -> str:
    """
    Converts a pytree to a string representation.

    Arguments:
        obj: The pytree to convert.
        private_fields: If `True`, private fields are included.
        static_fields: If `True`, static fields are included.

    Returns:
        A string representation of the pytree.
    """
    dict_ = to_dict(
        obj,
        private_fields=private_fields,
        static_fields=static_fields,
        type_info=True,
        field_info=True,
    )
    global RICH_WARNING_COUNT
    rep = _to_string(dict_, level=0, inline=False, color=color, space="    ")
    rep = _add_padding(rep)

    if color:
        if Console is None or Text is None:
            if RICH_WARNING_COUNT < 1:
                RICH_WARNING_COUNT += 1
                logging.warning(
                    f"'rich' library not available, install `rich` to get colors."
                )
        else:
            rep = _get_rich_repr(Text.from_markup(rep))

    return rep