utils.text

Text formatting utilities.

Module Contents

Functions

str_to_list(s) Convert string to list.
dedent_initial(s,n=4) Remove identation from first line of text.
dedent(s,n=4,sep=” “) Remove identation.
fill_paragraphs(s,width,sep=” “) Fill paragraphs with newlines (or custom separator).
join(l,sep=” “) Concatenate list of strings.
ensure_sep(sep,s,n=2) Ensure text s ends in separator sep’.
abbr(S,max,ellipsis=”…”) Abbreviate word.
abbrtask(S,max) Abbreviate task name.
indent(t,indent=0,sep=” “) Indent text.
truncate(s,maxlen=128,suffix=”…”) Truncate text to a maximum number of characters.
pluralize(n,text,suffix=”s”) Pluralize term when n is greater than one.
pretty(value,width=80,nl_width=80,sep=” “,**kw) Format value for printing to console.
match_case(s,other)
simple_format(s,keys,pattern=RE_FORMAT,expand=”\1”) Format string, expanding abbreviations in keys’.
remove_repeating_from_task(task_name,s) Given task name, remove repeating module names.
remove_repeating(substr,s) Remove repeating module names from string.
str_to_list(s)

Convert string to list.

dedent_initial(s, n=4)

Remove identation from first line of text.

dedent(s,n=4,sep="

“)

Remove identation.
fill_paragraphs(s,width,sep="

“)

Fill paragraphs with newlines (or custom separator).
join(l,sep="

“)

Concatenate list of strings.
ensure_sep(sep, s, n=2)

Ensure text s ends in separator sep’.

abbr(S, max, ellipsis="...")

Abbreviate word.

abbrtask(S, max)

Abbreviate task name.

indent(t,indent=0,sep="

“)

Indent text.
truncate(s, maxlen=128, suffix="...")

Truncate text to a maximum number of characters.

pluralize(n, text, suffix="s")

Pluralize term when n is greater than one.

pretty(value,width=80,nl_width=80,sep="

“,**kw)

Format value for printing to console.
match_case(s, other)
simple_format(s, keys, pattern=RE_FORMAT, expand="1")

Format string, expanding abbreviations in keys’.

remove_repeating_from_task(task_name, s)

Given task name, remove repeating module names.

Example:
>>> remove_repeating_from_task(
...     'tasks.add',
...     'tasks.add(2, 2), tasks.mul(3), tasks.div(4)')
'tasks.add(2, 2), mul(3), div(4)'
remove_repeating(substr, s)

Remove repeating module names from string.

Arguments:
task_name (str): Task name (full path including module),
to use as the basis for removing module names.

s (str): The string we want to work on.

Example:

>>> _shorten_names(
...    'x.tasks.add',
...    'x.tasks.add(2, 2) | x.tasks.add(4) | x.tasks.mul(8)',
... )
'x.tasks.add(2, 2) | add(4) | mul(8)'