Sch*_*hof 11 python printing variables debugging emacs
我发现自己经常添加调试"打印"语句 - 这样的东西:
print("a_variable_name: %s" % a_variable_name)
Run Code Online (Sandbox Code Playgroud)
你们都是那样做的?我是否在试图寻找优化方法的神经质?我可能正在研究一个函数并放入这些行中的大约六个,弄清楚它为什么不起作用,然后再将它们删除.
你有没有开发出有效的方法呢?
我在Emacs中编写Python代码.
有时调试器很棒,但有时使用print语句更快,更容易重复设置和使用.
这可能仅适用于CPython的调试(因为不是所有的蟒蛇实施inspect.currentframe和inspect.getouterframes),但我觉得对于削减打字这个有用:
在utils_debug.py中:
import inspect
def pv(name):
record=inspect.getouterframes(inspect.currentframe())[1]
frame=record[0]
val=eval(name,frame.f_globals,frame.f_locals)
print('{0}: {1}'.format(name, val))
Run Code Online (Sandbox Code Playgroud)
然后在你的script.py中:
from utils_debug import pv
Run Code Online (Sandbox Code Playgroud)
使用此设置,您可以替换
print("a_variable_name: %s' % a_variable_name)
Run Code Online (Sandbox Code Playgroud)
同
pv('a_variable_name')
Run Code Online (Sandbox Code Playgroud)
请注意,参数pv应该是字符串(变量名称或表达式),而不是值本身.
要使用Emacs删除这些行,您可以
C-x ( # start keyboard macro
C-s pv('
C-a
C-k # change this to M-; if you just want to comment out the pv call
C-x ) # end keyboard macro
Run Code Online (Sandbox Code Playgroud)
然后你可以用一次C-x e
或一千次调用宏C-u 1000 C-x e
当然,你必须要小心,确实要删除所有包含的行pv('.
| 归档时间: |
|
| 查看次数: |
3114 次 |
| 最近记录: |