2011-08-03 9 views
2

私はiPythonで定義した変数をPythonスクリプトに渡したいと思います。例えばスクリプト内のiPythonの変数を使用する

import IPython.ipapi 
ip = IPython.ipapi.get() 

def run_this_plot(*args): 
    """ Run 
    Examples 
    In [1]: import demo 
    In [2]: rtp x y <z> 
    Where x, y, and z are numbers of any type 
    """ 
    print "args: ", args 
    # Do something here with args, such as plot them 

# Activate the extension 
ip.expose_magic("rtp", run_this_plot) 

だから私は、彼らは(int型、範囲、などであるかもしれないものは何でも、iPythonでxとyの値が欲しい:スクリプトがある

In [1]: import demo 
In [2]: x = [1, 2, 3] 
In [3]: y = [1, 2, 3] 
In [4]: rtp x y 

)をスクリプトから見ることができます。このスクリプトは文字列 "xy"のみを表示します。

iPythonからスクリプトへの転送でこれらの値を取得するにはどうすればよいですか?それが不可能な場合、人々は通常回避策として何をしていますか?

ありがとうございます!私は

rtp x, y 

答えて

1

をしたとき

+0

ありがとう - これは私が探していたものです。 xとyの値を設定すると、rtp x yは正しい値を出力するようになりました。 –

+0

N.B.ソースはずっと移動しています。https://github.com/ipython/ipython –

0

--Erin は、それはあなたがそれが役に立つ手がかりをIPython.Magicモジュールのsourceを閲覧するために見つけるかもしれないうまく働きました。カスタムマジックメソッドには1つの文字列引数しか得られないように見えます。しかし、私は確認のための文書参照を見つけることができないようです。

私はこれを達成するためのより良い方法があります想像しますが、以下は、あなたの具体的な例のために働く必要があります。奇妙だ

import IPython.ipapi 
ip = IPython.ipapi.get() 

def run_this_plot(self, arg_s=''): 
    """ Run 
    Examples 
    In [1]: import demo 
    In [2]: rtp x y <z> 
    Where x, y, and z are numbers of any type 
    """ 
    args = [] 
    for arg in arg_s.split(): 
     try: 
      args.append(self.shell.user_ns[arg]) 
     except KeyError: 
      raise ValueError("Invalid argument: %r" % arg) 
    print "args: ", args 
    # Do something here with args, such as plot them 

# Activate the extension 
ip.expose_magic("rtp", run_this_plot) 
+0

を。私はまだその行を使用すると、文字列出力 - 'x、y'を取得します。 Pythonはx、y変数の正しい型を認識しますか? –

+0

IPythonのバージョン? –

+0

私はPython 2.7とiPython 0.10.1 Win 32を使用しています。 –

関連する問題