text

Text utilities.

anemoi.utils.text.dotted_line(width: int = 84) str

Return a dotted line using ‘┈’.

>>> dotted_line(40)
┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
Parameters:

width (int, optional) – Number of characters, by default 84

Returns:

The dotted line

Return type:

str

anemoi.utils.text.visual_len(s: str | list[tuple[str, int]]) int

Compute the length of a string as it appears on the terminal.

Parameters:

s (str or list of tuple) – The string or list of visual characters with their lengths.

Returns:

The visual length of the string.

Return type:

int

anemoi.utils.text.boxed(text: str, min_width: int = 80, max_width: int = 80) str

Put a box around a text.

>>> boxed("Hello,\nWorld!", max_width=40)
┌──────────────────────────────────────────┐
│ Hello,                                   │
│ World!                                   │
└──────────────────────────────────────────┘
Parameters:
  • text (str) – The text to box

  • min_width (int, optional) – The minimum width of the box, by default 80

  • max_width (int, optional) – The maximum width of the box, by default 80

Returns:

A boxed version of the input text

Return type:

str

anemoi.utils.text.bold(text: str) str

Make the text bold.

Parameters:

text (str) – The text to make bold.

Returns:

The bold text.

Return type:

str

anemoi.utils.text.red(text: str) str

Make the text red.

Parameters:

text (str) – The text to make red.

Returns:

The red text.

Return type:

str

anemoi.utils.text.green(text: str) str

Make the text green.

Parameters:

text (str) – The text to make green.

Returns:

The green text.

Return type:

str

anemoi.utils.text.blue(text: str) str

Make the text blue.

Parameters:

text (str) – The text to make blue.

Returns:

The blue text.

Return type:

str

class anemoi.utils.text.Tree(actor: Any, parent: Tree | None = None)

Bases: object

Tree data structure.

Parameters:
  • actor (Any) – The actor associated with the tree node.

  • parent (Tree, optional) – The parent tree node, by default None.

adopt(kid: Tree) None

Adopt a child tree node.

Parameters:

kid (Tree) – The child tree node to adopt.

forget() None

Forget the current tree node.

property is_leaf: bool

True if the tree node is a leaf, False otherwise.

Type:

Bool

property key: tuple

The key of the tree node.

Type:

Tuple

property summary: str

The summary of the tree node.

Type:

Str

as_dict() dict

Convert the tree node to a dictionary.

Returns:

The dictionary representation of the tree node.

Return type:

dict

node(actor: Any, insert: bool = False) Tree

Create a new tree node.

Parameters:
  • actor (Any) – The actor associated with the new tree node.

  • insert (bool, optional) – Whether to insert the new tree node at the beginning, by default False.

Returns:

The new tree node.

Return type:

Tree

print() None

Print the tree.

to_json(depth: int = 0) dict

Convert the tree to a JSON serializable dictionary.

Parameters:

depth (int, optional) – The depth of the tree, by default 0.

Returns:

The JSON serializable dictionary representation of the tree.

Return type:

dict

anemoi.utils.text.table(rows: list[list[Any]], header: list[str], align: list[str], margin: int = 0) str

Format a table.

>>> table([['Aa', 12, 5],
        ['B', 120, 1],
        ['C', 9, 123]],
        ['C1', 'C2', 'C3'],
        ['<', '>', '>'])
    C1 │  C2 │  C3
    ───┼─────┼────
    Aa │  12 │   5
    B  │ 120 │   1
    C  │   9 │ 123
    ───┴─────┴────
Parameters:
  • rows (list of lists (or tuples)) – The rows of the table

  • header (A list or tuple of strings) – The header of the table

  • align (A list of '<', '>', or '^') – To align the columns to the left, right, or center

  • margin (int, optional) – Extra spaces on the left side of the table, by default 0

Returns:

A table as a string

Return type:

str

anemoi.utils.text.progress(done: int, todo: int, width: int = 80) str

Generates a progress bar string.

Parameters:
  • done (int) – The number of tasks completed.

  • todo (int) – The total number of tasks.

  • width (int, optional) – The width of the progress bar, by default 80.

Returns:

A string representing the progress bar.

Return type:

str

Example

>>> print(progress(10, 100,width=50))
█████▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒