unitxt.utils module¶
- class unitxt.utils.Singleton¶
Bases:
type
- unitxt.utils.artifacts_json_cache(artifact_path)¶
- unitxt.utils.flatten_dict(d: Dict[str, Any], parent_key: str = '', sep: str = '_') Dict[str, Any] ¶
- unitxt.utils.import_module_from_file(file_path)¶
- unitxt.utils.is_module_available(module_name)¶
Check if a module is available in the current Python environment.
Parameters: - module_name (str): The name of the module to check.
Returns: - bool: True if the module is available, False otherwise.
- unitxt.utils.is_package_installed(package_name)¶
Check if a package is installed.
Parameters: - package_name (str): The name of the package to check.
Returns: - bool: True if the package is installed, False otherwise.
- unitxt.utils.json_dump(data)¶
- unitxt.utils.load_json(path)¶
- unitxt.utils.safe_eval(expression: str, context: dict, allowed_tokens: list) any ¶
Evaluates a given expression in a restricted environment, allowing only specified tokens and context variables.
- Parameters:
expression (str) – The expression to evaluate.
context (dict) – A dictionary mapping variable names to their values, which can be used in the expression.
allowed_tokens (list) – A list of strings representing allowed tokens (such as operators, function names, etc.) that can be used in the expression.
- Returns:
The result of evaluating the expression.
- Return type:
any
- Raises:
ValueError – If the expression contains tokens not in the allowed list or context keys.
Note
This function should be used carefully, as it employs eval, which can execute arbitrary code. The function attempts to mitigate security risks by restricting the available tokens and not exposing built-in functions.
- unitxt.utils.save_to_file(path, data)¶