utils
Module for commonly reused classes and functions.
file_exists
Try to expand ~/ and check if a file or dir exists.
Optionally check if it's a dir.
Examples:
Source code in cltk/utils/utils.py
reverse_dict
reverse_dict(
input_dict: dict[str, Any],
ignore_keys: Optional[list[str]] = None,
) -> dict[str, str]
Take a dict and reverse its keys and values.
Optional parameter to ignore certain keys.
Examples:
ids_lang = dict(anci1242='Ancient Greek', lati1261='Latin', unlabeled=['Ottoman'])
reverse_dict(ids_lang, ignore_keys=['unlabeled'])
# {'Ancient Greek': 'anci1242', 'Latin': 'lati1261'}
reverse_dict(dict(anci1242='Ancient Greek', lati1261='Latin'))
# {'Ancient Greek': 'anci1242', 'Latin': 'lati1261'}
try:
reverse_dict(ids_lang)
except TypeError:
pass
try:
reverse_dict(ids_lang, ignore_keys='unlabeled')
except TypeError:
pass
try:
reverse_dict(ids_lang, ignore_keys=['UNUSED-KEY'])
except TypeError:
pass
Source code in cltk/utils/utils.py
suppress_stdout
Wrap a function with this to suppress its printing to screen.
Source: <https://thesmithfam.org/blog/2012/10/25/temporarily-suppress-console-output-in-python/>_.
Examples:
Source code in cltk/utils/utils.py
get_cltk_data_dir
Return where to look for the cltk_data dir.
By default, this is located in a user's home directory
and the directory is created there (~/cltk_data).
However a user may customize where this goes with
the OS environment variable $CLTK_DATA. If the
variable is found, then its value is used.
Examples:
from cltk.utils import CLTK_DATA_DIR
import os
os.environ["CLTK_DATA"] = os.path.expanduser("~/cltk_data")
cltk_data_dir = get_cltk_data_dir()
assert os.path.split(cltk_data_dir)[1] == "cltk_data"
del os.environ["CLTK_DATA"]
os.environ["CLTK_DATA"] = os.path.expanduser("~/custom_dir")
cltk_data_dir = os.environ.get("CLTK_DATA")
assert os.path.split(cltk_data_dir)[1] == "custom_dir"
del os.environ["CLTK_DATA"]
Source code in cltk/utils/utils.py
str_to_bool
Convert a string into a boolean (case insensitively).
Parameters:
-
string(str) –String to convert.
-
truths(Optional[list[str]], default:None) –List of strings that count as Truthy; defaults to "yes" and "y".
Returns:
-
bool–Trueif string is in truths; otherwise, returnsFalse. All strings -
bool–are compared in lowercase, so the method is case insensitive.
Source code in cltk/utils/utils.py
query_yes_no
Ask a yes/no question via input() and return True/False.
Source: <https://stackoverflow.com/a/3041990>_.
Parameters:
-
question(str) –Question string presented to the user.
-
default(Union[str, None], default:'yes') –Presumed answer if the user just hits
. It must be "yes" (the default), "no", or None (meaning an answer is required of the user).
Returns:
-
bool–Truefor "yes" and "y" orFalsefor "no" and "n".
Source code in cltk/utils/utils.py
mk_dirs_for_file
Make all dirs specified for final file.
If dir already exists, then silently continue.
Parameters:
-
file_path(str) –Paths of dirs to be created (i.e.,
mkdir -p)
Returns:
-
None–None
Source code in cltk/utils/utils.py
pascal_case
camel_case
Convert a string to camelCase, removing separators.
Source code in cltk/utils/utils.py
capital_case
load_env_file
Load environment variables from a .env file.
Parameters:
-
env_file(str, default:'.env') –Path to the .env file. Defaults to ".env".
Returns:
-
None–None