sign_language_translator.utils.utils module

class sign_language_translator.utils.utils.PrintableEnumMeta(cls, bases, classdict, **kwds)[source]

Bases: EnumMeta

Metaclass for customizing the string representation of Enum classes.

This metaclass overrides the __str__ & __repr__ method to provide a human-readable representation of Enum classes when they are printed. The generated string includes the class name and a formatted list of Enum members and their values.

Example:

class MyEnumClass(enum.Enum, metaclass=PrintableEnumMeta):
    MEMBER1 = "value_1"
    MEMBER2 = "value_2"

print(MyEnumClass)

# "MyEnumClass" enum class. Available values:
# 1. MEMBER1 = value_1
# 2. MEMBER2 = value_2
class sign_language_translator.utils.utils.ProgressStatusCallback(tqdm_bar: tqdm_asyncio)[source]

Bases: object

A callback class to update a tqdm progress bar with custom status information.

Parameters:

tqdm_bar (tqdm) – The tqdm progress bar to be updated.

tqdm_bar

The tqdm progress bar associated with the callback.

Type:

tqdm

__call__(self, status

Dict[str, Any]): Update the tqdm progress bar with the provided status information.

Example:

# Instantiate a tqdm progress bar & callback
progress_bar = tqdm(total=100, desc='Processing')
callback = ProgressStatusCallback(tqdm_bar=progress_bar)

# Update the progress bar inside some other function
status_info = {'Epoch': 1, 'Loss': 0.123, 'Accuracy': 0.95}
callback(status_info)
sign_language_translator.utils.utils.extract_recursive(data: Dict[str, Any], key: str) List[Any][source]

Recursively extracts values associated with a specified key from a nested dictionary.

Parameters:
  • data (Dict[str, Any]) – The input dictionary containing nested structures.

  • key (str) – The key for which values need to be extracted.

Returns:

A list containing all values associated with the specified key, extracted

recursively from the input dictionary.

Return type:

List[Any]

Examples:

data = {'a': 1, 'b': {'c': 2, 'd': {'e': 3, 'f': 4}}, 'g': [5, {'h': 6, 'e': 7}]}
extract_recursive(data, 'e')
# [3, 7]
extract_recursive(data, 'h')
# [6]
extract_recursive(data, 'x')
# []  # Key not found, returns an empty list.
sign_language_translator.utils.utils.in_jupyter_notebook()[source]

Checks if the code is running in a Jupyter notebook.

Returns:

True if running in a Jupyter notebook, False otherwise.

Return type:

bool

sign_language_translator.utils.utils.is_internet_available() bool[source]

Hit a well-known server (Google DNS) to check for internet availability.

Returns:

True if internet is available, False otherwise.

Return type:

bool

sign_language_translator.utils.utils.is_regex(string: str | Pattern) bool[source]

Tests whether the argument is a regex or a regular string.

Parameters:

string (str | Pattern) – The string to be tested.

Returns:

whether the argument is a regex (True) or a regular string (False).

Return type:

bool

sign_language_translator.utils.utils.sample_one_index(weights: List[float], temperature: float = 1.0) int[source]

Select an item based on the given probability distribution. Returns the index of the selected item sampled from weighted random distribution.

Parameters:
  • weights (List[float]) – the relative weights corresponding to each index.

  • temperature (float) – The temperature value for controlling the sampling behavior. High temperature means sampling probabilities are more uniform (says random things). Low temperature means that sampling probabilities are higher for bigger weights. Defaults to 1.0.

Returns:

The index of the chosen item.

Return type:

int

sign_language_translator.utils.utils.search_in_values_to_retrieve_key(code_name: str, class_to_codes: Dict[Any, Set[str]])[source]
sign_language_translator.utils.utils.validate_path_exists(path: str, overwrite: bool = False) str[source]

Validates the existence of a given file path and optionally creates necessary directories.

This function checks if a file already exists at the specified path. If the file exists and overwrite is set to False, a FileExistsError is raised. If overwrite is set to True, or if the file does not exist, the function returns the absolute path after ensuring that all necessary directories are created.

Parameters:
  • path (str) – The file path to be validated.

  • overwrite (bool, optional) – Whether to overwrite the file if it already exists. Defaults to False.

Raises:

FileExistsError – If the file already exists at the specified path and overwrite is set to False.

Returns:

The absolute path of the validated file.

Return type:

str

Examples

>>> validate_path_exists('/path/to/file.txt', overwrite=False)
'/absolute/path/to/file.txt'