finesse.utilities.text module
Text utilities.
- finesse.utilities.text.add_linenos(linenos, lines)[source]
Add line numbers to the start of lines.
Parameters
- linenossequence of int
The line numbers, in the same order as lines.
- linessequence of str
The lines.
Returns
- sequence of str
The lines with prepended line numbers.
- finesse.utilities.text.format_bullet_list(items, indent=4, bullet_char='-')[source]
Format items into a bullet list.
- finesse.utilities.text.format_section(header, body, ruler=True, ruler_char='=')[source]
Format text in sections.
- finesse.utilities.text.get_close_matches(word: str, options: Iterable[str], edit_distance: int = 2, case_sensitive: bool = True) Iterable[str] | None[source]
Wrapper around the py-spellchecker module. Filters words from options that are similar to word, using the ‘Levenshtein distance’.
Parameters
- wordstr
word to match
- optionsIterable[str]
Iterable to select matches from
- edit_distanceint, optional
See https://en.wikipedia.org/wiki/Levenshtein_distance, by default 2
- case_sensitivebool, optional
Whether to consider different case different characters, by default True
Returns
- Iterable[str] | None
Words that are within edit_distance of word
- finesse.utilities.text.ngettext(n, fsingle, fplural, sub=True)[source]
Get the singular or plural form of the specified messages based on n.
Simplified version of the Python standard library function
gettext.ngettext().Parameters
- nint
The number to use to decide which form to return.
- fsingle, fpluralstr
Single and plural templates.
- subbool, optional
Substitute n into the templates. Defaults to True.
Examples
>>> ngettext(1, "{n} item", "{n} items") '1 item'
>>> ngettext(5, "{n} item", "{n} items") '5 items'
The template doesn’t have to contain {n}: >>> ngettext(5, “item”, “items”) ‘items’
Setting sub=False turns off substitution: >>> ngettext(5, “{n} item”, “{n} items”, sub=False) ‘{n} items’
- finesse.utilities.text.option_list(sequence, final_sep='or', quotechar=None, sort=False, prefix=None)[source]
Build a list from sequence with commas and a final “or”.
As in Python’s error messages (e.g. “‘func’ missing 3 requied positional arguments: ‘a’, ‘b’, and ‘c’”), this function adds an Oxford comma for sequences of length > 2.
Parameters
- sequencesequence
The options to create a list with.
- final_sepstr, optional
The final separator when sequence has more than one item. Defaults to or.
- quotecharstr, optional
Quote the items in sequence with this character. Defaults to no quotes.
- sortbool, optional
Sort the items sequence alphabetically. Defaults to false.
- prefixstr, optional
Concatenates the prefix with all items in sequence. Defaults to false.
- finesse.utilities.text.rewrite_lines(lines: List[str], clear: bool = True)[source]
Rewrite lines in a Jupyter notebook or terminal.
This function takes a list of strings and writes them to the output, either in a Jupyter notebook or a terminal. In a Jupyter notebook, it uses clear_output to clear the output before writing new lines. In a terminal, it uses ANSI escape codes to move the cursor up and clear lines before writing new lines.
Parameters
- lineslist of str
A list of strings representing lines to be written.
- clearbool, optional
Clear the output before writing new lines. Defaults to True.
- finesse.utilities.text.scale_si(number, units=None)[source]
Convert number to an SI-scaled string representation, with optional unit.
Examples
>>> scale_si(123.45e-6) '123.45u' >>> scale_si(370e-6, units="m") '370 um'