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 |
|---|---|---|---|
|
Any
|
Object to format. |
required |
|
TextIOBase | None
|
Text stream. |
None
|
|
int
|
Number of spaces used for each indentation level |
2
|
|
int
|
For nested objects, this value reflects the depth to which nested
objects are pretty-printed. For example, when |
4
|
|
int
|
The maximum amount of allowed characters on one line. |
88
|
|
int
|
The maximum amount of elements formatted for each object. |
100
|
|
str
|
A string added at the end after the formatted object. |
'\n'
|
|
bool
|
If |
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
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 |
|---|---|---|---|
|
type[T] | str
|
Object type or string which refers to the object type. |
required |
|
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
dispatch_repr: Format functions for types with__repr__method.dispatch_generic: Format functions for generic objects.
Source code in exprint/exprint.py
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)floatintstrbyteslisttuplesetdict
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
type[T]
|
Type of the object |
required |
|
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
dispatch_obj: Format functions for types with__name__attribute.dispatch_generic: Format functions for generic objects.
Source code in exprint/exprint.py
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 |
|---|---|---|---|
|
str
|
Generic key |
required |
|
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.