2016-10-09 26 views
0

在IPython的終端,我想深main() 功能回到IPython的,在那裏我可以打印,設置......像往常一樣,然後繼續運行main()如何協同程序IPython的<->回調()

IPython 
    run main.py 
    ... 
    def callback(*args): 
     ... 
     try: 
      back_to_ipython() # <-- how to do this ? 
       In[]: print, set *args ... 
       ... 
     except KeyboardInterrupt: # or IPython magic 
      pass 

     return # from callback(), keep running main() 

這必須在python2中運行。

(名稱callback可能是anything,但我的使用情況是scipy.optimize - >回調 也許一些聰明的人SciPy的已經這樣做了。?)


新增週二10月11日:感謝 embed, 但它似乎碰到一個bug,或我的誤解:

# http://stackoverflow.com/questions/39946052/how-to-coroutine-ipython-a-callback 

import sys 
from IPython import __version__ 
from IPython import embed # $site/IPython/terminal/embed.py 
from IPython.terminal.ipapp import load_default_config 

print "versions: IPython %s python %s" % (
     __version__, sys.version.split()[0]) 

def pdict(header, adict): 
    print header 
    for k, v in sorted(adict.items()): 
     print "%s\t: %s" % (k, v) 

config = load_default_config() 
pdict("load_default_config:", config) 

aglobal = [3] 

#............................................................................... 
def callback(adict): 
    # pdict("callback:", adict) 
    t = adict["t"] 
    x = 3 
    embed(header="callback: t %d" % t) 
     # interact: print t x ... 
     # ^D/EOF 
    return 

def aloop(*args): 
    for t in range(3): 
     callback(locals()) 

aloop(1, 2, 3) # works in "run this.py" 
# but typing "aloop()" in an IPython terminal -> 
# embed.py:218: UserWarning: Failed to get module unknown module 
# global_ns.get('__name__', 'unknown module') 
+0

'raise'在'back_to_ipython'中出現了一些異常,並在全局空間的'try'塊中運行'callback'。 –

回答

0

適應的答案https://stackoverflow.com/a/24827245/901925,我添加了一個IPython的embedhttps://ipython.org/ipython-doc/3/api/generated/IPython.terminal.embed.html

import numpy as np 
from scipy.optimize import minimize, rosen 
import time 
import warnings 
from IPython import embed 

class TookTooLong(Warning): 
    pass 

class MinimizeStopper(object): 
    def __init__(self, max_sec=60): 
     self.max_sec = max_sec 
     self.start = time.time() 
    def __call__(self, xk=None): 
     elapsed = time.time() - self.start 
     if elapsed > self.max_sec: 
      embed(header='FirstTime') 
      warnings.warn("Terminating optimization: time limit reached", 
          TookTooLong) 
     else: 
      # you might want to report other stuff here 
      print("Elapsed: %.3f sec" % elapsed) 

# example usage 
x0 = [1.3, 0.7, 0.8, 1.9, 1.2] 
res = minimize(rosen, x0, method='Nelder-Mead', callback=MinimizeStopper(1E-3)) 

與運行如:

1251:~/mypy$ python3 stack39946052.py 
Elapsed: 0.001 sec 
Python 3.5.2 (default, Jul 5 2016, 12:43:10) 
Type "copyright", "credits" or "license" for more information. 

IPython 5.1.0 -- An enhanced Interactive Python. 
?   -> Introduction and overview of IPython's features. 
%quickref -> Quick reference. 
help  -> Python's own help system. 
object? -> Details about 'object', use 'object??' for extra details. 


FirstTime 

In [1]: xk 
Out[1]: array([ 1.339, 0.721, 0.824, 1.71 , 1.236]) 

In [2]: elapsed 
Out[2]: 0.0010917186737060547 

In [3]: self.max_sec 
Out[3]: 0.001 

In [4]: self.max_sec=1000 

In [5]:                       
Do you really want to exit ([y]/n)? y 

stack39946052.py:20: TookTooLong: Terminating optimization: time limit reached 
    TookTooLong) 
.... 
+0

感謝'嵌入';你能看看添加的測試用例嗎? – denis

相關問題