Utilities

Decorators

edsl.utilities.decorators.add_edsl_version(func)[source]

Decorator for EDSL objects’ to_dict method. - Adds the EDSL version and class name to the dictionary.

edsl.utilities.decorators.jupyter_nb_handler(func)[source]

Decorator to run an async function in the event loop if it’s running, or synchronously otherwise.

edsl.utilities.decorators.memory_profile(func)[source]

Decorator to profile memory usage of a function.

Only activates if the EDSL_MEMORY_PROFILE environment variable is set to ‘true’ or if the function name is included in the EDSL_MEMORY_PROFILE_FUNCTIONS environment variable (comma-separated list of function names).

Example:

EDSL_MEMORY_PROFILE=true python your_script.py EDSL_MEMORY_PROFILE_FUNCTIONS=filter,duplicate python your_script.py

edsl.utilities.decorators.remove_edsl_version(func)[source]

Decorator for the EDSL objects’ from_dict method. - Removes the EDSL version and class name from the dictionary. - Ensures backwards compatibility with older versions of EDSL.

edsl.utilities.decorators.sync_wrapper(async_func)[source]

Decorator to create a synchronous wrapper for an asynchronous function.

Interface

Pastebin

Utilities

Utility functions for working with strings, dictionaries, and files.

class edsl.utilities.utilities.CustomEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

default(obj)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)
class edsl.utilities.utilities.HTMLSnippet(value)[source]

Bases: str

Create an object with html content (value).

view method allows you to view the html content in a web browser.

view()[source]

View the HTML content in a web browser.

edsl.utilities.utilities.clean_json(bad_json_str)[source]

Clean JSON string by replacing single quotes with double quotes

edsl.utilities.utilities.create_valid_var_name(s, transform_func: ~typing.Callable = <function <lambda>>) str[source]

Create a valid variable name from a string.

edsl.utilities.utilities.dict_hash(data: dict)[source]
edsl.utilities.utilities.dict_to_html(d)[source]

Convert a dictionary to an HTML table.

edsl.utilities.utilities.extract_json_from_string(text)[source]
edsl.utilities.utilities.file_notice(file_name)[source]

Print a notice about the file being created.

edsl.utilities.utilities.fix_partial_correct_response(text: str) dict[source]
edsl.utilities.utilities.hash_value(value: str | int) str[source]

Hash a string or integer value using SHA-256.

edsl.utilities.utilities.is_gzipped(file_path)[source]

Check if a file is gzipped.

edsl.utilities.utilities.is_notebook() bool[source]

Check if the code is running in a Jupyter notebook or Google Colab.

edsl.utilities.utilities.is_valid_variable_name(name, allow_name=True)[source]

Check if a string is a valid variable name.

edsl.utilities.utilities.merge_dicts(dict_list)[source]

Merge a list of dictionaries into a single dictionary.

edsl.utilities.utilities.random_string() str[source]

Generate a random string of fixed length.

edsl.utilities.utilities.repair_json(json_string: str) str[source]

Attempt to repair a JSON string that is not valid JSON.

edsl.utilities.utilities.sanitize_jinja_syntax(data: dict, data_type: str) dict[source]

Sanitize values that contain problematic Jinja2 syntax by replacing with safe equivalents.

This function recursively processes a dictionary and escapes any Jinja2 template syntax found in string values to prevent unintended template rendering. This is particularly useful when user data might contain characters that could be interpreted as Jinja2 template directives.

Args:

data: Dictionary to sanitize data_type: Type of data being sanitized (for warning messages)

Returns:

dict: Sanitized copy of the input data with Jinja2 syntax escaped

Examples:

Basic sanitization of problematic syntax:

>>> data = {"message": "Hello {{name}}", "comment": "Use {# tags #} for comments"}
>>> result = sanitize_jinja_syntax(data, "test_data")
>>> # Jinja2 syntax will be escaped to HTML entities
>>> "&#123;&#123;" in result["message"]  # {{ becomes &#123;&#123;
True

Nested dictionaries and lists are handled recursively:

>>> nested = {
...     "user": {"template": "Hi {{user}}"},
...     "items": ["Item {%for i in range(3)%}", "Normal item"]
... }
>>> result = sanitize_jinja_syntax(nested, "nested_data")
>>> # All nested values are processed

Non-string values are preserved unchanged:

>>> data = {"number": 42, "boolean": True, "text": "{{unsafe}}"}
>>> result = sanitize_jinja_syntax(data, "mixed_data")
>>> result["number"] == 42
True
>>> result["boolean"] is True
True
edsl.utilities.utilities.shorten_string(s, max_length, placeholder='...')[source]

Shorten a string to a maximum length by removing characters from the middle.

edsl.utilities.utilities.shortname_proposal(question, max_length=None)[source]

Take a question text and generate a slug.

edsl.utilities.utilities.text_to_shortname(long_text, forbidden_names=[])[source]

Create a slug for the question.

edsl.utilities.utilities.time_all_functions(module_or_class)[source]
edsl.utilities.utilities.time_it(func)[source]
edsl.utilities.utilities.truncate_base64_in_place(obj)[source]

Recursively truncate any ‘base64_string’ value to 1000 chars (in place).

edsl.utilities.utilities.valid_json(json_string)[source]

Check if a string is valid JSON.

edsl.utilities.utilities.write_api_key_to_env(api_key: str) str[source]

Write the user’s Expected Parrot key to their .env file.

If a .env file doesn’t exist in the current directory, one will be created.

Returns a string representing the absolute path to the .env file.