alter_ego.utils package

Module contents

alter_ego.utils.exclusive_response(s: str, tokens: List[str], *, case_insensitive: bool = True) str | None[source]

Validates that a string contains only one of a list of tokens.

Parameters:
  • s (str) – The string to search in.

  • tokens (List[str]) – List of allowable tokens.

  • case_insensitive (bool) – If True, the function will be case insensitive.

Returns:

The first matching token found, or None.

Return type:

Optional[str]

alter_ego.utils.extract_number(s: str) int | float | None[source]

Extract a single number, potentially a float, from a string.

This function uses a straightforward definition of “number” that excludes scientific notation like 3e8.

Parameters:

s (str) – The string to extract the number from.

Returns:

The extracted number as an integer or a float, if applicable. Returns None otherwise.

Return type:

Optional[Union[int, float]]

alter_ego.utils.from_file(file_name: str) str[source]

Read file content as a stripped string.

Parameters:

file_name (str) – The name of the file to read.

Returns:

The content of the file as a stripped string.

Return type:

str

alter_ego.utils.homogenize(s: str) str[source]
alter_ego.utils.to_html(s: str) str[source]

Escapes special characters and converts newlines to HTML format.

Parameters:

s (str) – The string to convert.

Returns:

The converted string in HTML format.

Return type:

str