Skip to content

Other

Miscellaneous utilities.

from prairielearn import ...

escape_unicode_string

escape_unicode_string(string: str) -> str

Replace invisible/unprintable characters with a text representation of their hex id: <U+xxxx>

A character is considered invisible if its category is "control" or "format", as reported by the 'unicodedata' library.

More info on unicode categories: https://en.wikipedia.org/wiki/Unicode_character_property#General_Category

Returns:

Type Description
str

A string with invisible characters replaced

full_unidecode

full_unidecode(input_str: str) -> str

Do unidecode of input and replace the unicode minus with the normal one.

Returns:

Type Description
str

A fully decoded string without unicode characters.

get_unit_registry

get_unit_registry() -> UnitRegistry

Get a unit registry using cache folder valid on production machines.

https://pint.readthedocs.io/en/stable/index.html

Returns:

Type Description
UnitRegistry

A process-specific unit registry.

get_uuid

get_uuid() -> str

Return the string representation of a new random UUID. First character of this uuid is guaranteed to be an alpha (at the expense of a slight loss in randomness).

This is done because certain web components need identifiers to start with letters and not numbers.

Returns:

Type Description
str

A UUID starting with an alpha char

index2key

index2key(i: int) -> str

Use when generating ordered lists of the form ['a', 'b', ..., 'z', 'aa', 'ab', ..., 'zz', 'aaa', 'aab', ...]

Returns:

Type Description
str

Alphabetic key in the form [a-z]* from a given integer (i = 0, 1, 2, ...).

iter_keys

iter_keys() -> Generator[str, None, None]

A continuous alphabetic list of the form ['a', 'b', ..., 'z', 'aa', 'ab', ..., 'zz', 'aaa', 'aab', ...].

https://stackoverflow.com/questions/29351492/how-to-make-a-continuous-alphabetic-list-python-from-a-z-then-from-aa-ab-ac-e/29351603#29351603

Returns:

Type Description
None

A generator that yields strings in the specified format.