Skip to content

opto.trace.utils

builtins_list module-attribute

builtins_list = dir(builtins)

global_functions_list module-attribute

global_functions_list = [
    name
    for name in builtins_list
    if callable(getattr(builtins, name))
]

sum_feedback

sum_feedback(nodes)

Aggregate the feedback of a list of nodes.

contain

contain(container_of_nodes, node)

parse_eqs_to_dict

parse_eqs_to_dict(text)

Parse the text of equations into a dictionary

Example: x0 = 1 x1=2 x2=2 x3= def fun():\n print('hello')\n abc_test1=test

would be parsed into

{'x0': '1', 'x1': '2', 'x2': '2', 'x3': "def fun():\nprint('hello')", 'abc_test1': 'test'}

for_all_methods

for_all_methods(decorator)

Applying a decorator to all methods of a class.

render_opt_step

render_opt_step(
    step_idx,
    optimizer,
    no_trace_graph=False,
    no_improvement=False,
)

escape_json_nested_quotes

escape_json_nested_quotes(json_str)

Escapes double quotation marks inside JSON string values for a specific format: {"name": "string value", "value": "string value"} Does not escape quotes around keys or structural quotes.

Warning: Here are what this function does not do: 1. Cannot handle "\n" or "\t" type of strings 2. Does not check if "\n", "\t", or other control characters are properly escaped. Please use json_str.replace("\n", "\n") to escape control characters outside of this function.

Example usage can be found in optimizers/textgrad.py.

Args: json_str (str): A string representation of JSON with exactly two keys: name and value.

Returns: str: JSON string with properly escaped quotes in values.

remove_non_ascii

remove_non_ascii(json_txt)

Example usage can be found in optimizers/textgrad.py

dedent

dedent(text: str)

A better dedent than dedent from textwrap module. Remove leading and trailing whitespace for each line For example:

Line 1 has no leading space
    Line 2 has two leading spaces
The output will be :
Line 1 has no leading space
Line 2 has two leading spaces
This allows writing cleaner multiline prompts in the code.

test_json_quote_escaper

test_json_quote_escaper()