在Python中是否有我运行的函数的历史记录?

时间:2014-03-18 14:31:23

标签: python python-2.7

写了很长时间之后我已经删除了某些功能...... CTRL + y无法拯救我。 基本上我有:

def foo(todo):
    print 'how why where'

在.py文件中,我删除了函数,并且无法从文件中跟踪。既然ipython运行了这个函数,并且解释器仍然存在,有没有办法观察运行该函数的解释器的历史? 我设法通过命令找到函数名称:

history

也来自Export Python interpreter history to a file?

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath

但它也与普通历史相同 真正的问题是,只要解释器​​的会话处于活动状态,即使文件被删除,python也可以从中重新启动函数的历史或跟踪在哪里。我可以再次运行该功能&再次,因为它符合某处。

谢谢!

2 个答案:

答案 0 :(得分:1)

此时所有可用的都是已编译的代码。您可以将其反汇编为可读的字节代码列表,这可以帮助您重建原始源。这将是一项艰苦的工作,因为字节代码的级别相当低,但它可能比没有更好。

import dis
dis.disassemble(foo.__code__)

结果如下:

  2           0 LOAD_CONST               1 ('how why where')
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE        

答案 1 :(得分:0)

使用PyCharm,备份文件并使用版本控制系统,例如hggitsvnPyCharm保存您的本地历史记录,以便您可以恢复已删除的行。使用hggitsvn,您可以查看文件的先前版本并还原更改。并且每天至少使用tar(或使用zip)备份您的文件。