2016-10-09 3 views
0

을 코 루틴 방법, 나는 평소와 같이, 다음 main()을 계속 실행 ... 설정 나는 인쇄 할 수 있습니다 IPython,로 돌아갈 깊은 main() 에서 함수를 원하는 :은 IPython <->에게 콜백()는 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에서 실행되어야합니다. 감사 embed를 들어, :


추가 화요일 10월 11일 -

는 (?> 콜백 은 아마도 일부 영리 scipy 사람이 이런 짓을했다 이름 callbackanything 될 수있다,하지만 내 사용 사례 scipy.optimize입니다.) 하지만 버그로 실행하는 것, 또는 내 오해 : 당신은 중단 점을 삽입 할 수

# 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

'back_to_ipython'에서 예외를'raise'하고 전역 공간에서'try' 블록에서'callback'을 실행합니다. –

답변

0

https://stackoverflow.com/a/24827245/901925에 답을 적응, 나는 실행을 가진 Ipython embed (https://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)) 

추가 like :

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