fromgraphvizimportDigraphimportbuiltinsimportreimportjson# Get a list of all names in the builtins modulebuiltins_list=dir(builtins)# Filter for function names; this includes exceptions, so you might want to refine thisglobal_functions_list=[namefornameinbuiltins_listifcallable(getattr(builtins,name))]
[docs]defsum_feedback(nodes):"""Aggregate the feedback of a list of nodes."""returnsum([sum(gg)forpinnodesforgginp.feedback.values()])
[docs]defcontain(container_of_nodes,node):# check for identity instead of valuereturnany([nodeisnfornincontainer_of_nodes])
[docs]defparse_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'} """lines=text.split("\n")result_dict={}last_key=Noneforlineinlines:ifline=="":continueif"="inline:key,value=line.split("=",1)last_key=key.strip()result_dict[last_key]=value.replace("`","")eliflast_key:result_dict[last_key]+="\n"+line.replace("`","")returnresult_dict
[docs]deffor_all_methods(decorator):"""Applying a decorator to all methods of a class."""defdecorate(cls):forname,attrincls.__dict__.items():ifcallable(attr)andnotname.startswith("__"):setattr(cls,name,decorator(attr))returnclsreturndecorate
[docs]defescape_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. """result=[]i=0in_value=Falsewhilei<len(json_str):char=json_str[i]ifchar=='"':# Check if this quote is around "name" or "value"next_four=json_str[i+1:i+5]next_five=json_str[i+1:i+6]is_key=next_four=="name"ornext_five=="value"# Check if this is a structural quote (after : or before })prev_char=json_str[i-1]ifi>0else""next_char=json_str[i+1]ifi<len(json_str)-1else""is_value_boundary=(prev_char==":"or(prev_char==" "andjson_str[i-2]==":")ornext_char=="}"ornext_char==",")ifis_keyoris_value_boundary:result.append(char)ifprev_char==":"or(prev_char==" "andjson_str[i-2]==":"):in_value=Trueifnext_char=="}"ornext_char==",":in_value=Falseelse:# if we double-escpaed like \\", we remove oneifin_valueandprev_char=="\\"andjson_str[i-2]=="\\":result.pop(-1)result.append(char)# If we're in a value and this is not a boundary quote, escape itelifin_valueandprev_char!="\\":result.append(r"\"")else:result.append(char)else:# we need to remove markdown latex syntax# it's a simple procedure that removes all "\\alpha" or "\\(" type strings# JSON can't accept any \ with invalid characters, in here we took a short cut and only keep \ for# we didn't add \u to this listifjson_str[i-1]=="\\"andcharnotin["\\","\/","n","b","f","r","t",]:result.pop(-1)result.append(char)# print(in_value, ''.join(result))i+=1return"".join(result)
[docs]defremove_non_ascii(json_txt):""" Example usage can be found in optimizers/textgrad.py """cleaned=""forcinescape_json_nested_quotes(json_txt):ifcnotin["\n","\t","\b","\r","\f"]andnotc.isprintable():continuecleaned+=creturncleaned
[docs]deftest_json_quote_escaper():test_cases=[('{"name": "Multiple "quotes" in "one" string", "value": "Multiple "quotes" in "the second" string"}',r'{"name": "Multiple \"quotes\" in \"one\" string", "value": "Multiple \"quotes\" in \"the second\" string"}',),('{"name": "Simple "quote"", "value": "Another "quote""}',r'{"name": "Simple \"quote\"", "value": "Another \"quote\""}',),('{"name": "No quotes here", "value": "But "quotes" here"}',r'{"name": "No quotes here", "value": "But \"quotes\" here"}',),('{"name": "Quote at "end"", "value": "Another at "end""}',r'{"name": "Quote at \"end\"", "value": "Another at \"end\""}',),(r'{"name": "Quote at "end"", "value": "Partial at \"end""}',r'{"name": "Quote at \"end\"", "value": "Partial at \"end\""}',),(r'{"name": "Quote at \\"end\\"", "value": "Partial at \"end""}',r'{"name": "Quote at \"end\"", "value": "Partial at \"end\""}',),(r'{"name": "Quote at \\"end\\"", "value": "\( \alpha_t \) \\n"}',r'{"name": "Quote at \"end\"", "value": "( alpha_t ) \\n"}',),]fori,(input_str,expected)inenumerate(test_cases,1):result=escape_json_nested_quotes(input_str)assert(result==expected),f"\nTest case {i} failed:\nInput: {input_str}\nExpected: {expected}\nGot: {result}"print("All tests passed!")