In a python based project, I am using this as a string: [code] The input variables are…
- User
In a python based project, I am using this as a string:
f"[{a}] \ \ <Model Merge Chain> \ [ \ {b} \ ] \ \ UNET: \ <Model Merger (Advanced/DARE) Settings> \ \{ \ gradient: \ <Shell Gradient> \ \{ \ exterior: {c} \ interior: {c} \ core: {c} \ \} \ \ model_mask: None \ \ drop_rate: 0.96 \ ties: sum \ rescale: off \ seed: 42 \ method: cosine \ iterations: 8 \ \} \ \ CLIP: \ <CLIP Merger (DARE) Settings> \ \{ \ ties: sum \ rescale: off \ ratio: {c} \ drop_rate: 0.96 \ seed: 42 \ method: gradient \ iterations: 8 \ \} \ \ VAE: sdxl_vae-fp16-fix"The input variables are a, b and c, but I keep getting this error:
!!! Exception during processing!!! f-string expression part cannot include a backslash (<unknown>, line 41) Traceback (most recent call last): File "D:\AI Generation\Software\ComfyUI_windows_portable\ComfyUI\execution.py", line 151, in recursive_execute output_data, output_ui = get_output_data(obj, input_data_all) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AI Generation\Software\ComfyUI_windows_portable\ComfyUI\execution.py", line 81, in get_output_data return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AI Generation\Software\ComfyUI_windows_portable\ComfyUI\execution.py", line 74, in map_node_over_list results.append(getattr(obj, func)(**slice_dict(input_data_all, i))) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AI Generation\Software\ComfyUI_windows_portable\ComfyUI\custom_nodes\efficiency-nodes-comfyui\efficiency_nodes.py", line 4406, in evaluate result = simpleeval.simple_eval(python_expression, names=variables, functions=functions) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AI Generation\Software\ComfyUI_windows_portable\python_embeded\Lib\site-packages\simpleeval.py", line 748, in simple_eval return s.eval(expr) ^^^^^^^^^^^^ File "D:\AI Generation\Software\ComfyUI_windows_portable\python_embeded\Lib\site-packages\simpleeval.py", line 428, in eval return self._eval(previously_parsed or self.parse(expr)) ^^^^^^^^^^^^^^^^ File "D:\AI Generation\Software\ComfyUI_windows_portable\python_embeded\Lib\site-packages\simpleeval.py", line 410, in parse parsed = ast.parse(expr.strip()) ^^^^^^^^^^^^^^^^^^^^^^^ File "ast.py", line 50, in parse File "<unknown>", line 41 f"[{a}] \ \ <Model Merge Chain> \ [ \ {b} \ ] \ \ UNET: \ <Model Merger (Advanced/DARE) Settings> \ \{ \ gradient: \ <Shell Gradient> \ \{ \ exterior: {c} \ interior: {c} \ core: {c} \ \} \ \ model_mask: None \ \ drop_rate: 0.96 \ ties: sum \ rescale: off \ seed: 42 \ method: cosine \ iterations: 8 \ \} \ \ CLIP: \ <CLIP Merger (DARE) Settings> \ \{ \ ties: sum \ rescale: off \ ratio: {c} \ drop_rate: 0.96 \ seed: 42 \ method: gradient \ iterations: 8 \ \} \ \ VAE: sdxl_vae-fp16-fix" ^ SyntaxError: f-string expression part cannot include a backslash - Assistant
- User
I need to clarify a few things, I do not want backslashes as the final output, the final output should be something like this:
[zithmix_v017B] <Model Merge Chain> [ zithmix_v017A.fp16, confusionxl_v30, copaxPonyXL_semiReal, cyberrealisticPony_v10a, combiningChineseAnd_v20VAE ] UNET: <Model Merger (Advanced/DARE) Settings> { gradient: <Shell Gradient> { model: zithmix_v017A.fp16 exterior: 0.90 interior: 0.90 core: 0.90 } model_mask: None drop_rate: 0.96 ties: sum rescale: off seed: 42 method: cosine iterations: 8 } CLIP: <CLIP Merger (DARE) Settings> { ties: sum rescale: off ratio: 0.90 drop_rate: 0.96 seed: 42 method: gradient iterations: 8 } VAE: sdxl_vae-fp16-fixNote the following about the input variables:
a = the main output model name b = a pre-built string, built from a list of model names, a already contains the formatting required for multiple lines, commas and indenting. c = a float turned into a string, denoting the merge ratio used.
In the end I only included the backslashes to to try and avoid issues, since the variables need {} for the f-string to pull in the variable, but I am using {} as part of my main .txt output, so I need to escape them so the f-string doesn't try to parse them are variables, I then added the \ at the end of each line to escape the newline character for having multiple lines within a single f-string.
- Assistant
To achieve the desired formatting without unnecessary backslashes and proper handling of curly braces without interfering with f-string variable interpolation, you can take the following revised approach:
- Explicitly use double curly braces
{{and}}wherever you want single curly braces{and}to appear in the output. This escapes the braces in f-strings so they are not interpreted as Python expression delimiters.
- Explicitly use double curly braces
- User
Ahh! The issue is I wasn't escaping the curly braces correctly, I though backslash was used to escape everything, but I guess in python that isn't the case, I also forgot to utilize the triple quote string, I usually use that for multi-line comments, I forgot it is still considered a string within python. Making those changes it is now behaving as expected.
- Assistant
That's great to hear! Yes, Python has specific rules for escaping characters within strings, and understanding how to correctly use curly braces within f-strings can indeed be a bit tricky at first. Using double curly braces (
{{and}}) to output literal curly braces in the final string is the right approach when working with f-strings.Utilizing triple-quoted strings for multiline text can simplify matters significantly by reducing the need for explicit line continuation with backslashes and naturally supporting the inclusion of new line characters, which makes it ideal for formatting more complex textual outputs or creating readable templates.
If you have any more questions or need further assistance with your project, feel free to ask. Happy coding!
- User
I will, thank you again! I have a background in coding for decades, learned a lot of languages, C, C++, C#, Basic, Visual Basic, Pascal, Turbo Pascal, HTML, PHP, Python, as well as many different specialized scripting languages like QuakeC, mIRC scripts, etc, and visual coding with node graphs like with Unreal Engine Blueprints and right now I am using ComfyUI nodes for Stable Diffusion AI tasks. But, I spent most of my time on C/C++, only since just before the AI boom of Stable Diffusion 1.5 did I start delving more into python, so I am mostly winging it and there is a LOT I do not know about.
- Assistant
It sounds like you have a rich background in programming across a variety of languages and platforms! Transitioning to Python with such a deep reservoir of experience definitely gives you a solid foundation, though each language has its quirks and nuances, as you’ve noticed with Python’s string formatting and handling.