如何通过Python表达式评估过程? [英] How to step through Python expression evaluation process?

查看:103
本文介绍了如何通过Python表达式评估过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个可视化调试器,它帮助编程学生看到表达式的评估发生(如何通过Excel中的表达式评估可视化程序对子表达式进行评估和替换)。

I want to build a visual debugger, which helps programming students to see how expression evaluation takes place (how subexpressions get evaluated and "replaced" by their values, something like expression evaluation visualizer in Excel).

看起来你无法通过Python的pdb进行这个过程,因为最好的步骤粒度是代码行。是否有可能逐步通过Python字节码?任何其他想法如何实现这一目标?

Looks like you can't step through this process with Python's pdb, as its finest step granularity is line of code. Is it somehow possible to step through Python bytecode? Any other ideas how to achieve this goal?

编辑:我需要一个可以构建在CPython标准库之上的轻量级解决方案。

I need a lightweight solution that can be built on top of CPython standard library.

推荐答案

我也有一个解决方案的想法 - 我可以通过在虚拟方法调用中包装所有(子)表达式来调用代码(或AST)只不过是回归自己的​​论据。例如。

I have a solution idea also myself -- I could instrument the code (or AST) by wrapping all (sub)expressions in a dummy method call, which does nothing more than returning its argument. Eg.

x = f(sin(x + y))

成为

x = dummy(f(dummy(sin(dummy(dummy(x) + dummy(y))))))

在每个子表达式得到评估后通知,我也得到值。我还可以添加额外的位置/ AST信息,关于表达式当前处理的部分,例如:

This way I'm guaranteed to be notified after each subexpression gets evaluated and I also get the values. I can also add extra location/AST information about which part of the expression is currently dealt with, eg:

... dummy(x, line=23, col=13, length=1) ...

搞乱AST和编译...

Unfortunately this requires messing with AST and compilation ...

这篇关于如何通过Python表达式评估过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆