英文:
Handling line continuation and escaped characters in Python
问题
以下是您要翻译的内容:
"I am working with Python code captured from Nuke, a compositing software, which adds line continuation and escaped characters. The code typed in by the user in Nuke is as follows:
if nuke.frame()>1:
ret=nuke.frame()/2
else:
ret=nuke.frame()
To retrieve this code (which is stored in a node's knob in Nuke), i use the expression() method from Nuke's Python API, here's an example:
animation_curve = n['multiply'].animation(0)
code_string = animation_curve.expression()
The resulting code string is as follows:
[python -execlocal if\ nuke.frame()>1:\n\ \ \ ret=nuke.frame()/2\nelse:\n\ \ \ ret=nuke.frame()]
Then via regex pattern matching i capture only the code:
if\ nuke.frame()>1:\n\ \ \ ret=nuke.frame()/2\nelse:\n\ \ \ ret=nuke.frame()
My objective is to parse and execute this code string using the ast module in Python, taking into account the line continuation and escaped characters present. However, I am encountering difficulties due to the syntax error caused by the unexpected characters following the line continuation characters.
When I try tree = ast.parse(code_string)
I get:
if\ nuke.frame()>1:\n\ \ \ ret=nuke.frame()/2\nelse:\n\ \ \ ret=nuke.frame()
^
SyntaxError: unexpected character after line continuation character
Note that I probably can't use .replace() to get rid of backslashes in the code because some code might actually have backslashes in it.. I also can't use exec() due to security risks.
I appreciate any insights, suggestions, or code examples that can guide me in correctly parsing and executing the Python code obtained from Nuke's animation expression. Thank you for your valuable assistance!"
英文:
I am working with Python code captured from Nuke, a compositing software, which adds line continuation and escaped characters. The code typed in by the user in Nuke is as follows:
if nuke.frame()>1:
ret=nuke.frame()/2
else:
ret=nuke.frame()
To retrieve this code (which is stored in a node's knob in Nuke), i use the expression() method from Nuke's Python API, here's an example:
animation_curve = n['multiply'].animation(0)
code_string = animation_curve.expression()
The resulting code string is as follows:
[python -execlocal if\ nuke.frame()>1:\n\ \ \ ret=nuke.frame()/2\nelse:\n\ \ \ ret=nuke.frame()]
Then via regex pattern matching i capture only the code:
if\ nuke.frame()>1:\n\ \ \ ret=nuke.frame()/2\nelse:\n\ \ \ ret=nuke.frame()
My objective is to parse and execute this code string using the ast module in Python, taking into account the line continuation and escaped characters present. However, I am encountering difficulties due to the syntax error caused by the unexpected characters following the line continuation characters.
When I try tree = ast.parse(code_string)
I get:
if\ nuke.frame()>1:\n\ \ \ ret=nuke.frame()/2\nelse:\n\ \ \ ret=nuke.frame()
^
SyntaxError: unexpected character after line continuation character
Note that I probably can't use .replace() to get rid of backslashes in the code because some code might actually have backslashes in it.. I also can't use exec() due to security risks.
I appreciate any insights, suggestions, or code examples that can guide me in correctly parsing and executing the Python code obtained from Nuke's animation expression. Thank you for your valuable assistance!
答案1
得分: 1
这里的诀窍是你有用反斜杠转义的空格 "\ "
,而 ast
包不喜欢它们。我相信我们可以手动剥离它们,事情应该能顺利进行。
尝试:
import ast
text = "if\ nuke.frame()>1:\n\ \ \ ret=nuke.frame()/2\nelse:\n\ \ \ ret=nuke.frame()"
parser = ast.parse(text.replace("\ ", " ")) # <---- Take care of the escaped spaces
walker = ast.walk(parser)
print(list(walker))
英文:
The trick here is that you have spaces that are escaped with backslashes "\ "
and the ast
package does not like them. I believe we can manually strip them out and things should hopefully work.
Try:
import ast
text = "if\ nuke.frame()>1:\n\ \ \ ret=nuke.frame()/2\nelse:\n\ \ \ ret=nuke.frame()"
parser = ast.parse(text.replace("\ ", " ")) # <---- Take care of the escaped spaces
walker = ast.walk(parser)
print(list(walker))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论