Other¶
Miscellaneous utilities.
from prairielearn import ...
escape_unicode_string ¶
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 ¶
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 ¶
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 |
iter_keys ¶
partition ¶
partition(
data: Iterable[ListItem],
pred: Callable[[ListItem], bool],
) -> tuple[list[ListItem], list[ListItem]]
Implement a partition function, splitting the data into two lists based on the predicate.
Returns:
| Type | Description |
|---|---|
list[ListItem]
|
A tuple with two lists, the first containing items that satisfy the predicate, |
list[ListItem]
|
and the second containing items that do not. |
Example
numbers = [1, 2, 3, 4, 5] evens, odds = partition(numbers, lambda x: x % 2 == 0) evens [2, 4] odds [1, 3, 5]