Skip to content

exprint.exprint

exprint.exprint.exprint

exprint(
    obj: Any,
    stream: TextIOBase | None = None,
    indentation: int = 2,
    depth: int = 4,
    width: int = 88,
    max_elements: int = 100,
    end: str = "\n",
    with_color: bool = True,
)

Pretty prints the obj by formatting it recursively and writes the content into a text stream.

Parameters:

Name Type Description Default

obj

Any

Object to format.

required

stream

TextIOBase | None

Text stream.

None

indentation

int

Number of spaces used for each indentation level

2

depth

int

For nested objects, this value reflects the depth to which nested objects are pretty-printed. For example, when depth is less than 0, list objects are formatted as [list] values and dict objects are formatted as {dict} values.

4

width

int

The maximum amount of allowed characters on one line.

88

max_elements

int

The maximum amount of elements formatted for each object.

100

end

str

A string added at the end after the formatted object.

'\n'

with_color

bool

If True, enables colored output for better readability

True

Raises:

Type Description
NotImplementedError

When no associated format function has been found for the object to be formatted.

Source code in exprint/exprint.py
def exprint(
    obj: Any,
    stream: io.TextIOBase | None = None,
    indentation: int = 2,
    depth: int = 4,
    width: int = 88,
    max_elements: int = 100,
    end: str = "\n",
    with_color: bool = True,
):
    """
    Pretty prints the obj by formatting it recursively and writes the content
    into a text stream.

    Parameters
    ----------
    obj : Any
        Object to format.
    stream : io.TextIOBase | None
        Text stream.
    indentation : int
        Number of spaces used for each indentation level
    depth : int
        For nested objects, this value reflects the depth to which nested
        objects are pretty-printed. For example, when `depth` is less than 0,
        list objects are formatted as `[list]` values and dict objects are
        formatted as `{dict}` values.
    width : int
        The maximum amount of allowed characters on one line.
    max_elements : int
        The maximum amount of elements formatted for each object.
    end : str
        A string added at the end after the formatted object.
    with_color : bool
        If `True`, enables colored output for better readability

    Raises
    ------
    NotImplementedError
        When no associated format function has been found for the object to be
        formatted.
    """
    stream: io.TextIOBase = sys.stdout if stream is None else stream  # type: ignore
    f = Formatter(indentation, depth, width, max_elements, with_color)
    f.format_any(obj).finish(stream)
    stream.write(end)

exprint.exprint.dispatch_obj

dispatch_obj(
    obj_type: type[T] | str,
    format_func: Callable[[T, Formatter], Format],
)

Registers format function associated to the __name__ attribute of an object type.

Parameters:

Name Type Description Default

obj_type

type[T] | str

Object type or string which refers to the object type.

required

format_func

Callable[[T, Formatter], Format]

Format function.

required

Examples:

>>> class Example:
...     __slots__ = "foo", "bar"
...     def __init__(self, foo: str, bar: int):
...         self.foo = foo
...         self.bar = bar
>>> def format_example(obj: Example, f: Formatter) -> Format:
...     return f.format_class("Example").field("foo", obj.foo).field("bar", obj.bar)
>>> dispatch_obj(Example, format_example)
See also
Source code in exprint/exprint.py
def dispatch_obj(
    obj_type: type[T] | str, format_func: Callable[[T, Formatter], Format]
):
    """
    Registers format function associated to the `__name__` attribute of an
    object type.

    Parameters
    ----------
    obj_type : type[T] | str
        Object type or string which refers to the object type.
    format_func : Callable[[T, Formatter], Format]
        Format function.

    Examples
    --------

    >>> class Example:
    ...     __slots__ = "foo", "bar"
    ...     def __init__(self, foo: str, bar: int):
    ...         self.foo = foo
    ...         self.bar = bar
    >>> def format_example(obj: Example, f: Formatter) -> Format:
    ...     return f.format_class("Example").field("foo", obj.foo).field("bar", obj.bar)
    >>> dispatch_obj(Example, format_example)

    See also
    --------

    - [`dispatch_repr`][exprint.exprint.dispatch_repr] : Format functions for
      types with `__repr__` method.
    - [`dispatch_generic`][exprint.exprint.dispatch_generic] : Format functions
      for generic objects.
    """
    if isinstance(obj_type, str):
        Formatter._dispatch_objs[obj_type] = format_func
    else:
        Formatter._dispatch_objs[obj_type.__name__] = format_func

exprint.exprint.dispatch_repr

dispatch_repr(
    obj_type: type[T],
    format_func: Callable[[T, Formatter], Format],
)

Registers format function associated to the __repr__ method of a type.

Default registered types:

  • type(None)
  • float
  • int
  • str
  • bytes
  • list
  • tuple
  • set
  • dict

Parameters:

Name Type Description Default

obj_type

type[T]

Type of the object

required

format_func

Callable[[T, Formatter], Format]

Format function

required

Examples:

>>> def format_float(obj: float, f: Formatter) -> Format:
...     return f.format_color(ANSIColors.MAGENTA).value(f.format_value().value(obj))
>>> dispatch_repr(float, format_float)
See also
Source code in exprint/exprint.py
def dispatch_repr(obj_type: type[T], format_func: Callable[[T, Formatter], Format]):
    """
    Registers format function associated to the `__repr__` method of a type.

    Default registered types:

    - `type(None)`
    - `float`
    - `int`
    - `str`
    - `bytes`
    - `list`
    - `tuple`
    - `set`
    - `dict`

    Parameters
    ----------
    obj_type : type[T]
        Type of the object
    format_func : Callable[[T, Formatter], Format]
        Format function

    Examples
    --------

    >>> def format_float(obj: float, f: Formatter) -> Format:
    ...     return f.format_color(ANSIColors.MAGENTA).value(f.format_value().value(obj))
    >>> dispatch_repr(float, format_float)

    See also
    --------

    - [`dispatch_obj`][exprint.exprint.dispatch_obj] : Format functions for
      types with `__name__` attribute.
    - [`dispatch_generic`][exprint.exprint.dispatch_generic] : Format functions
      for generic objects.
    """
    Formatter._dispatch_repr[obj_type.__repr__] = format_func

exprint.exprint.dispatch_generic

dispatch_generic(
    gen_key: str,
    format_func: Callable[[Any, Formatter], Format],
)

Registers format function associated to a generic object.

Supported generic keys:

  • "recursion"
  • "dataclass"
  • "class"

Parameters:

Name Type Description Default

gen_key

str

Generic key

required

format_func

Callable[[Any, Formatter], Format]

Format function

required

Examples:

>>> def format_class(obj: Any, f: Formatter) -> Format:
...     fclass = f.format_class(obj.__class__.__name__)
...     for name, value in obj.__dict__.items():
...         fclass.field(name, value)
...     return fclass
>>> dispatch_generic("class", format_class)
See also
  • dispatch_obj : Format functions for types with __name__ attribute.
  • dispatch_repr : Format functions for types with __repr__ method.
Source code in exprint/exprint.py
def dispatch_generic(gen_key: str, format_func: Callable[[Any, Formatter], Format]):
    """
    Registers format function associated to a generic object.

    Supported generic keys:

    - `"recursion"`
    - `"dataclass"`
    - `"class"`

    Parameters
    ----------
    gen_key : str
        Generic key
    format_func : Callable[[Any, Formatter], Format]
        Format function

    Examples
    --------

    >>> def format_class(obj: Any, f: Formatter) -> Format:
    ...     fclass = f.format_class(obj.__class__.__name__)
    ...     for name, value in obj.__dict__.items():
    ...         fclass.field(name, value)
    ...     return fclass
    >>> dispatch_generic("class", format_class)

    See also
    --------

    - [`dispatch_obj`][exprint.exprint.dispatch_obj] : Format functions for
      types with `__name__` attribute.
    - [`dispatch_repr`][exprint.exprint.dispatch_repr] : Format functions for
      types with `__repr__` method.
    """
    Formatter._dispatch_generic[gen_key] = format_func