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
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 aTypeError
).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.
- 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.file_notice(file_name)[source]
Print a notice about the file being created.
- edsl.utilities.utilities.hash_value(value: str | int) str [source]
Hash a string or integer value using SHA-256.
- 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.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 >>> "{{" in result["message"] # {{ becomes {{ 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.