epy.Lines#

class etils.epy.Lines(*, indent: int = 4)[source]#

Bases: object

Util to build multi-line text.

Useful for pretty-print tools and human readable __repr__.

Example:

d = {'a': 1, 'b': 2}

lines = epy.Lines()
lines += 'dict('
with lines.indent():
  for k, v in d.items():
    lines += f'{k}={v},'
lines += ')'
text = lines.join()

Output:

dict(
    a=1,
    b=2,
)
append(line: str) None[source]#

Append a new line str.

extend(iterable: Iterable[str]) None[source]#

Append all the new line str from the iterable.

indent() Iterator[None][source]#
join(*, collapse: bool = False) str[source]#

Returns the lines.

Parameters:

collapse – If True, all lines are merged together in a single line.

Returns:

All lines merged together

Return type:

text

classmethod make_block(header: str = '', content: str | dict[str, Any] | list[Any] | tuple[Any, ...] = (), *, braces: str | tuple[str, str] = '(', equal: str = '=', limit: int = 20) str[source]#

Util function to create a code block.

Example:

epy.Lines.make_block('A', {}) == 'A()'
epy.Lines.make_block('A', {'x': '1'}) == 'A(x=1)'
epy.Lines.make_block('A', {'x': '1', 'y': '2'}) == '''A(
    x=1,
    y=2,
)'''

Pattern is as:

{header}{braces[0]}
    {k}={v},
    ...
{braces[1]}
Parameters:
  • header – Prefix before the brace

  • content – Dict of key to values. One line will be displayed per item if len(content) > 1. Otherwise the code is collapsed

  • braces – Brace type ((, [, {), can be tuple for custom open/close.

  • equal – The separator (=, `: `)

  • limit – Strings smaller than this will be collapsed

Returns:

The block string