English 中文(简体)
如果存在IPython,则使用IPython与代码模块进行交流
原标题:Using IPython interchangeably with code module if IPython exists

在现行法典中,我已经指出:

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?

最佳回答

这里我自己的尝试:

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__ :
    try:
        import IPython
        IPython.embed()
    except:
        sh = Shell()
        sh.interact()

看来是工作罚款,但我可能失去<条码>cache和<条码>stdout习俗方法/功能。

Any criticism, edits and improvement suggestions welcome!

问题回答

我不是一位大专家(如果我错的话,你不会 down倒),但我认为,你可以做像我这样的事情。

try: from IPython.core import interactiveshell as InteractiveConsole #facade code here, if needed except ImportError: from code import InteractiveConsole #fallback case

这样,如果出现<条码>,请携带<>。 IPython.core.interactiveshell 否则改写为InteractiveConsole。 你们必须改变方法名称和签名,或尝试建造某种校友或改编者。

一种可能的办法,是把所有必要的职能划入你的名称空间,并利用这些内容。 在校外法中,你只得界定具有相同签名(姓名和参数清单)的职能,并代之以<代码>互动编码<>功能:

try:
     from IPython.core import interactiveshell as InteractiveConsole
     def func1(a,b,c):
          InteractiveConsole.some_other_func(a,b,c)
except ImportError:
     from code import InteractiveConsole        #fallback case
     func1 = InteractiveConsole.func1

...
func1(a,b,c)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签