continuous_timeseries.formatting#
Support for pretty formatting of our classes
Inspired by:
-
the difference between
__repr__and__str__in Python (see e.g. https://realpython.com/python-repr-vs-str/) -
the advice from the IPython docs about prettifying output (https://ipython.readthedocs.io/en/8.26.0/config/integrating.html#rich-display)
-
the way that xarray handles formatting (see https://github.com/pydata/xarray/blob/main/xarray/core/formatting.py)
-
the way that pint handles formatting (see e.g. this line )
Functions:
| Name | Description |
|---|---|
add_html_attribute_row |
Add a row for displaying an attribute's HTML value to a list of existing rows |
apply_ct_html_styling |
Apply continuous timeseries' HTML styling for displaying an instance |
get_html_repr_safe |
Get the HTML representation of an instance |
make_html_attribute_table |
Make an HTML table of attributes |
to_html |
Convert an instance to its html representation |
to_pretty |
Pretty-print an instance using IPython's pretty printer |
to_str |
Convert an instance to its string representation |
add_html_attribute_row #
add_html_attribute_row(
attribute_name: str,
attribute_value_html: str,
attribute_rows: list[str],
) -> list[str]
Add a row for displaying an attribute's HTML value to a list of existing rows
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attribute_name
|
str
|
Attribute's name |
required |
attribute_value_html
|
str
|
Attribute's HTML value to display |
required |
attribute_rows
|
list[str]
|
Existing attribute rows |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Attribute rows, with the new row appended |
Source code in src/continuous_timeseries/formatting.py
apply_ct_html_styling #
Apply continuous timeseries' HTML styling for displaying an instance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
display_name
|
str
|
Name to display as the instance's name This appears as the title of the output table/div |
required |
attribute_table
|
str
|
Table of attributes of the instance. Generally created with |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted HTML with CSS styling included |
Source code in src/continuous_timeseries/formatting.py
get_html_repr_safe #
get_html_repr_safe(
instance: Any,
escapes: tuple[tuple[str, str], ...] = (
("&", "&"),
("<", "<"),
(">", ">"),
),
) -> str
Get the HTML representation of an instance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Any
|
Instance of which to get the HTML representation |
required |
escapes
|
tuple[tuple[str, str], ...]
|
Escape characters to apply when a raw string has to be used |
(('&', '&'), ('<', '<'), ('>', '>'))
|
Returns:
| Type | Description |
|---|---|
str
|
HTML representation. If |
Source code in src/continuous_timeseries/formatting.py
make_html_attribute_table #
Make an HTML table of attributes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attribute_rows
|
Iterable[str]
|
Attribute rows to put in the table |
required |
Returns:
| Type | Description |
|---|---|
str
|
HTML table of attribute rows |
Source code in src/continuous_timeseries/formatting.py
to_html #
to_html(
instance: Any,
attrs_to_show: Iterable[str],
prefix: str = "continuous_timeseries.",
include_header: bool = True,
) -> str
Convert an instance to its html representation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Any
|
Instance to convert |
required |
attrs_to_show
|
Iterable[str]
|
Attributes to include in the HTML representation. |
required |
prefix
|
str
|
Prefix to include in front of the instance name when displaying. |
'continuous_timeseries.'
|
include_header
|
bool
|
Should the header be included when formatting the object? |
True
|
Returns:
| Type | Description |
|---|---|
str
|
HTML representation of the instance |
Source code in src/continuous_timeseries/formatting.py
to_pretty #
to_pretty(
instance: Any,
attrs_to_show: Collection[str],
p: RepresentationPrinter,
cycle: bool,
indent: int = 4,
) -> None
Pretty-print an instance using IPython's pretty printer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Any
|
Instance to convert |
required |
attrs_to_show
|
Collection[str]
|
Attributes to include in the pretty representation. |
required |
p
|
RepresentationPrinter
|
Pretty printer |
required |
cycle
|
bool
|
Whether the pretty printer has detected a cycle or not. |
required |
indent
|
int
|
Indent to apply to the pretty printing group |
4
|
Source code in src/continuous_timeseries/formatting.py
to_str #
Convert an instance to its string representation
Only include specified attributes in the representation. Show the string representation of the attributes to show.
As a note, the point of this is to provide a helpful representation for users.
The repr representation is intended for developers.
For more details, see e.g. https://realpython.com/python-repr-vs-str/
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Any
|
Instance to convert to str |
required |
attrs_to_show
|
Iterable[str]
|
Attributes to include in the string representation. |
required |
Returns:
| Type | Description |
|---|---|
str
|
String representation of the instance |