ipython探查器

时间:2012-02-19 09:23:13

标签: ipython

我在python中有一些代码可以运行,但遗憾的是速度很慢。 #python的某个人建议我可以通过分析器运行代码,以查看代码花费最多时间的行和函数。

我想要分析的python源代码从STDIN读取。但由于输入很大,我将输入编译为文件,以便我可以简单地将它重定向到shell的python代码。所以在shell中,我发出命令..

cat input | python pythonsource.py 

问题是,当我尝试在ipython中运行探查器时,我似乎找不到将输入重定向到python代码的方法。在ipython shell中,我试过了,

run -p -l 1.0 pythonsource.py input (didn't work. simply waits at STDIN for input)
run -p -l 1.0 pythonsource.py << input (didn't work)
run -p -l 1.0 cat input | python pythonsource.py (didn't work.)

我不知道该怎么做我可以让ipython profiler命令将输入​​重定向到STDIN以供pythonsource读取。有人可以告诉我如何解决这个问题吗?或者我完全错了?也许有一些更简洁,更智能的方法来分析python代码?

也许我接下来要问的应该是另一个问题的一部分..但我想知道ipython在ipython探查器的某些输出中引用“原始调用”时的含义是什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

shell命令行上重定向输入的正确方法如下:

cat input | run -p -l 1.0 python pythonsource.py

run -p -l 1.0 python pythonsource.py < input

语法<< string创建“here here”,它不会重定向输入。

从python提示符(希望在ipython中),你可以像这样重定向标准输入:

import sys
save_stdin = sys.stdin
sys.stdin = open('input')
run -p <etc.>
sys.stdin = save_stdin # Restore the real stdin

或(推荐)您可以重写源代码以使用文件名:如果您按如下方式调用它,“输入”将在sys.argv[1]中,您可以打开它并从中读取:

python pythonsource.py input