python - Python AST:类型错误重写节点
问题描述
我正在尝试使用 PLY 和 Python 的 AST 从头开始创建方程求解器。
2 + x
从我首先翻译成的输入开始N.Number(2) + x
。为什么?因为我正在使用我自己的实数类。
现在,我正在从第一步的输出创建一个 AST。在这里,我想重写“x”,通过函数调用节点更改其名称节点以获得如下内容:N.Number(2) + N.Number(42)
.
但我得到这个 TypeError :
Traceback (most recent call last):
File "test_ast.py", line 52, in <module>
print(eval(compile(yop, filename="", mode="eval")))
TypeError: required field "value" missing from Attribute
到目前为止,这是我的代码:
class RewriteName(ast.NodeTransformer):
def visit_Name(self, node):
if node.id == 'x':
return ast.copy_location(ast.Call(func=ast.Attribute(value=ast.Name(id='N', ctx=ast.Load()), attr='Number', ctx=ast.Load()),
args=[ast.Num(n=42)],
keywords=[]
), node)
question = "N.Number(2) + x"
yop = ast.parse(question, mode="eval")
print(ast.dump(yop))
RewriteName().visit(yop)
print(ast.dump(yop))
print(eval(compile(yop, filename="", mode="eval")))
问题显然来自 Rewrite_Name 类。注意它周围的两个打印:这些是它们的输出:
Expression(body=BinOp(left=Call(func=Attribute(value=Name(id='N', ctx=Load()), attr='Number', ctx=Load()), args=[Num(n=2)], keywords=[]), op=Add(), right=Name(id='x', ctx=Load())))
Expression(body=BinOp(left=Call(func=Attribute(attr='Number', ctx=Load()), args=[Num(n=2)], keywords=[]), op=Add(), right=Call(func=Attribute(value=Name(id='N', ctx=Load()), attr='Number', ctx=Load()), args=[Num(n=42)], keywords=[])))
我对 Rewrite_Name 类的 cal 错误地更改了表达式 BinOp 左侧的某些内容,但我不知道为什么。
有人能帮我吗 ?
解决方案
我认为您收到此错误是因为当您访问第一个 Name 时,它的 id 不是 x。在此代码中,您的函数visit_Name
返回None
.
添加一个else: return node
class RewriteName(ast.NodeTransformer):
def visit_Name(self, node):
if node.id == 'x':
return ast.copy_location(ast.Call(func=ast.Attribute(value=ast.Name(id='N', ctx=ast.Load()), attr='Number', ctx=ast.Load()),
args=[ast.Num(n=42)],
keywords=[]
), node)
else:
return node
将帮助您避免required field "value" missing from Attribute
...
只是为了得到另一个TypeError: required field "lineno" missing from expr
;-)
推荐阅读
- python - 如何将程序的 STDOUT 重定向到 GDB 调试程序的 STDIN?
- c# - this[] 索引器:类型“SheetsDecoratorImpl”已经保留了一个名为“get_Item”的成员,具有相同的参数类型
- r - 如何创建一个新变量,然后用多个条件更改它?
- flutter - 我需要绝对定位一个元素,但无法在 Flutter 中获得 SafeArea 的高度
- c++ - 使用 C++ 的 Visual Studio 2017 社区的代码覆盖率
- html - 为什么我的程序总能找到一个类,但如果我在 HTML 中搜索这个类,它却不存在?
- javascript - 我只想在选择单选按钮后在表单上突出显示我的
- arrays - 创建一个没有重复的结构数组
- ruby-on-rails - 如何在用户完成帐户设置之前锁定整个 Rails 应用程序
- regex - Perl 替换简化