选择何时在 Python 中执行
我是一名学生,正在编写一个 Python 脚本来实现以下目标:
给定一个 .py 文件(包括来自 pygame 的绘图逻辑),显示正在执行的代码以及显示绘图的框架。我写这篇文章是为了让教授能够同时演示代码及其结果,从而更轻松地教授算法。
我遇到的主要问题是能够在每一行代码处停止,并等待用户响应继续。此功能与任何调试器的“单步执行”非常相似。
我研究过的一种方法是在从提供的源文件执行代码时使用 exec() 和 eval() 函数。在这次调查中,我发现普遍认为 exec 和 eval 非常令人困惑并且通常不安全(即使我将控制输入)。使用这种方法,我将按顺序单步浏览源代码,同时寻找可能影响要执行的内容的分支或决策逻辑。
还有比这更好的方法吗?
I'm a student writing a Python script to achieve the following:
Given a .py file(that includes drawing logic from pygame), show the code as it is being executed, along with a frame that displays the drawings. I am writing this as a tool for a professor to more easily teach algorithms, by being able to demonstrate the code and its result simultaneously.
The major problem that I am running into is being able to stop at each line of code, and wait for a user response to continue. This feature is very similar to a "step into" from any debugger.
One method I've looked into is using the exec() and eval() functions when executing the code from the provided source file. In this investigation, I have found the general opinion that exec and eval are very confusing and often unsafe (even though I will be controlling the input). With this method, I would be sequentially stepping through the source, while looking for branches or decision-making logic that may influence what is to be executed.
Is there a better method to employ than this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不将 python 调试器本身包装到程序中,并让它在其中执行 - 并使用它的钩子以图形方式跟踪当前正在执行的行?
我还看到有人将 IronPython 解释器嵌入到他们的程序中 - 您可能可以跟踪其中当前执行的代码并将其数据输入 GUI 中。
http://writeonly。 wordpress.com/2008/09/08/embedding-a-python-shell-in-a-python-script/
Why don't you wrap the python debugger itself into your program, and have it execute within it - and use its hooks to graphically track the current line being executed?
I've also seen someone embed an ironPython interpreter into their program - and you can probably track the current code being executed within it and feed it data into a GUI.
http://writeonly.wordpress.com/2008/09/08/embedding-a-python-shell-in-a-python-script/