我想编写可以逐行执行其他Python代码的Python代码,但没有用户交互,就像pdb一样。我基本上想要df.dateFormat = "MM-dd-yyyy HH:mm:ss"
df.dateFromString("1-5-2016 1:3:2") // works too
,但一次只需要一行。
例如,我想编写如下代码:
exec()
这只是一个例子。我确信有一种更有效的方法可以产生与上述代码相同的效果 - 例如,import non_interactive_pdb as ni_pdb
def interleave(code1, code2):
'''"Interleave" the execution of code1 and code2, which are two strings
representing Python source code, by alternately running one line
from code1 and followed by one line from code2.'''
prog1 = ni_pdb.compile(code1)
prog2 = ni_pdb.compile(code2)
while not prog1.terminated() and not prog2.terminated():
prog1.exec_next_line()
prog2.exec_next_line()
if prog1.terminated():
while not prog2.terminated():
prog2.exec_next_line()
if prog2.terminated():
while not prog1.terminated():
prog1.exec_next_line()
---但这不是我之后的意思。