在现行法典中,我已经指出:
import sys
from code import InteractiveConsole
class FileCacher:
"Cache the stdout text so we can analyze it before returning it"
def __init__(self):
self.reset()
def reset(self):
self.out = []
def write(self, line):
self.out.append(line)
def flush(self):
output =
.join(self.out)
self.reset()
return output
class Shell(InteractiveConsole):
"Wrapper around Python that can filter input/output to the shell"
def __init__(self):
self.stdout = sys.stdout
self.cache = FileCacher()
InteractiveConsole.__init__(self)
return
def get_output(self):
sys.stdout = self.cache
def return_output(self):
sys.stdout = self.stdout
def push(self, line):
self.get_output()
# you can filter input here by doing something like
# line = filter(line)
InteractiveConsole.push(self, line)
self.return_output()
output = self.cache.flush()
# you can filter the output here by doing something like
# output = filter(output)
print output # or do something else with it
return
if __name__ == __main__ :
sh = Shell()
sh.interact()
如果IPython在不尽可能修改法典其余部分的情况下提供,我如何加以修改,以使用IPython的交互式手法。
我试图从代码进口交互式Console到,从IPython. 核心进口交互式shell作为交互式Console
,但显然不是直接互换的类别。
除并使用<代码>外,用什么最佳方法(对代码基数的其余部分作最小改动)做到这一点。 IPython in favor over 模块是:<代码>IPython
?