Display
coola.display ¶
Contain display shared helpers.
coola.display.BaseDisplayMixin ¶
Bases: ABC
Abstract base for display mixins.
Subclasses must implement :meth:_get_repr_kwargs to return a dict
of constructor arguments used by __repr__ and __str__.
coola.display.InlineDisplayMixin ¶
Bases: BaseDisplayMixin
Mixin that renders __repr__ and __str__ on a single line.
All constructor arguments are displayed inline. Best suited for simple objects with few, short arguments where a compact single-line format aids readability.
Example
>>> from coola.display import InlineDisplayMixin
>>> from typing import Any
>>> class MyClass(InlineDisplayMixin):
... def __init__(self, key1: str, key2: str) -> None:
... self.key1 = key1
... self.key2 = key2
... def _get_repr_kwargs(self) -> dict[str, Any]:
... return {"key1": self.key1, "key2": self.key2}
...
>>> obj = MyClass(key1="value1", key2="value2")
>>> print(repr(obj))
MyClass(key1='value1', key2='value2')
>>> print(str(obj))
MyClass(key1=value1, key2=value2)
coola.display.MultilineDisplayMixin ¶
Bases: BaseDisplayMixin
Mixin that renders __repr__ and __str__ in multiline dict
format.
Each constructor argument is displayed on its own indented line. Best suited for objects with several arguments or deeply nested values where a single-line format would be hard to read.
Example
>>> from coola.display import MultilineDisplayMixin
>>> from typing import Any
>>> class MyClass(MultilineDisplayMixin):
... def __init__(self, key1: str, key2: str) -> None:
... self.key1 = key1
... self.key2 = key2
... def _get_repr_kwargs(self) -> dict[str, Any]:
... return {"key1": self.key1, "key2": self.key2}
...
>>> obj = MyClass(key1="value1", key2="value2")
>>> print(repr(obj))
MyClass(
(key1): value1
(key2): value2
)
>>> print(str(obj))
MyClass(
(key1): value1
(key2): value2
)
coola.display.NoArgsDisplayMixin ¶
Bases: BaseDisplayMixin
Mixin that provides a default, no-op _get_repr_kwargs
implementation for classes with no constructor arguments to show.
Many stateless classes (e.g. handlers, hashers, transformers without
configuration) only need to satisfy the :class:BaseDisplayMixin
abstract method contract with an empty dict. Combine this mixin with
:class:InlineDisplayMixin or :class:MultilineDisplayMixin to avoid
writing a trivial _get_repr_kwargs override that just returns
{}. A subclass with actual constructor arguments should override
_get_repr_kwargs instead of using this mixin.
Example
>>> from coola.display import InlineDisplayMixin, NoArgsDisplayMixin
>>> class MyClass(NoArgsDisplayMixin, InlineDisplayMixin):
... pass
...
>>> obj = MyClass()
>>> print(repr(obj))
MyClass()
>>> print(str(obj))
MyClass()
coola.display.repr_pydantic_model ¶
repr_pydantic_model(
model: BaseModel,
*,
sort: bool = True,
exclude_none: bool = False,
exclude_secret: bool = True,
exclude_fields: list[str] | None = None,
) -> str
Return a formatted, single-line repr-style representation of
a pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BaseModel
|
The pydantic model to format. |
required |
sort
|
bool
|
If |
True
|
exclude_none
|
bool
|
If |
False
|
exclude_secret
|
bool
|
If |
True
|
exclude_fields
|
list[str] | None
|
Optional list of field names to omit. Names that do not exist on the model are silently ignored. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A string like |
Example
>>> from pydantic import BaseModel
>>> from coola.display.pydantic import repr_pydantic_model
>>> class Config(BaseModel):
... name: str
... count: int
...
>>> repr_pydantic_model(Config(name="my-app", count=3))
"Config(count=3, name='my-app')"
coola.display.str_pydantic_model ¶
str_pydantic_model(
model: BaseModel,
*,
sort: bool = True,
exclude_none: bool = False,
exclude_secret: bool = True,
exclude_fields: list[str] | None = None,
) -> str
Return a formatted, single-line string representation of a pydantic model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
BaseModel
|
The pydantic model to format. |
required |
sort
|
bool
|
If |
True
|
exclude_none
|
bool
|
If |
False
|
exclude_secret
|
bool
|
If |
True
|
exclude_fields
|
list[str] | None
|
Optional list of field names to omit. Names that do not exist on the model are silently ignored. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A string like |
Example
>>> from pydantic import BaseModel
>>> from coola.display.pydantic import str_pydantic_model
>>> class Config(BaseModel):
... name: str
... count: int
...
>>> str_pydantic_model(Config(name="my-app", count=3))
'Config(count=3, name=my-app)'
coola.display.colorlog ¶
Provide utilities for configuring Python's logging output.
coola.display.colorlog.configure_colorlog_logging ¶
configure_colorlog_logging(
level: int = INFO, force: bool = False
) -> None
Configure the root logger, using a coloured formatter when available.
If the colorlog package is installed and sys.stderr is
attached to a terminal, attaches a :class:colorlog.StreamHandler
with per-level colours for both the log metadata (level, logger
name, line number) and the message itself. If colorlog is not
installed, or output is not a terminal (e.g. redirected to a file
or running in CI), falls back to plain :func:logging.basicConfig
with no formatting, to avoid emitting raw ANSI escape codes into
non-interactive output.
Note
:func:logging.basicConfig is a no-op if the root logger
already has handlers configured. Pass force=True to remove
existing handlers and reconfigure unconditionally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int
|
Minimum log level for the root logger. Accepts any
constant from :mod: |
INFO
|
force
|
bool
|
When |
False
|
Example
>>> import logging
>>> from coola.display.colorlog import configure_colorlog_logging
>>> configure_colorlog_logging(level=logging.DEBUG)