ipython是一个交互式计算机系统_1.13 IPython:一个交互式shell工具

科学家和工程师都喜欢做实验,而IPython正是诞生于爱做实验的科学家之手。IPython提供的交互式实验环境被很多人认为是Matlab、Mathematica和Maple的开源替代品。你可以在线获取包括安装指南在内的更多信息,地址为http://ipython.org/。

IPython是开源免费的软件,可以在Linux、Unix、Mac OS X以及Windows上使用。IPython的作者们希望那些用到IPython的科研工作成果在发表时能够提到IPython,这是他们对IPython使用者唯一的要求。下面是IPython的基本功能:

Tab键自动补全;

历史记录存档;

行内编辑;

使用%run可以调用外部Python脚本;

支持系统命令;

支持pylab模式;

Python代码调试和性能分析。

在pylab模式下,IPython将自动导入SciPy、NumPy和Matplotlib模块。如果没有这个功能,我们只能手动导入每一个所需模块。

而现在,我们只需在命令行中输入如下命令:

$ ipython --pylab

Python 2.7.2 (default, Jun 20 2012, 16:23:33)

Type "copyright", "credits" or "license" for more information.

IPython 0.14.dev -- 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.

Welcome to pylab, a matplotlib-based Python environment [backend: MacOSX].

For more information, type 'help(pylab)'.

In [1]: quit()

使用quit()函数或快捷键Ctrl+D均可以退出IPython shell。有时我们想要回到之前做过的实验,IPython可以便捷地保存会话以便稍后使用。

In [1]: %logstart

Activating auto-logging. Current session state plus future input saved.

Filename : ipython_log.py

Mode : rotate

Output logging : False

Raw input log : False

Timestamping : False

State : active

举例来说,我们将之前的向量加法程序放在当前目录下,可以按照如下方式运行脚本:

In [1]: ls

README vectorsum.py

In [2]: %run -i vectorsum.py 1000

你可能还记得,这里的1000是指向量中元素的数量。%run的–d参数将开启ipdb调试器,键入c后,脚本就开始逐行执行了(如果脚本有n行,就一共执行n步直到代码结束)。在ipdb提示符后面键入quit可以关闭调试器。

In [2]: %run -d vectorsum.py 1000

*** Blank or comment

*** Blank or comment

Breakpoint 1 at: /Users/.../vectorsum.py:3

在ipdb>提示符后面键入c,从而开始运行代码。

>(1)()

ipdb> c

> /Users/.../vectorsum.py(3)()

2

1---> 3 import sys

4 from datetime import datetime

ipdb> n

>

/Users/.../vectorsum.py(4)()

1 3 import sys

----> 4 from datetime import datetime

5 import numpy

ipdb> n

> /Users/.../vectorsum.py(5)()

4 from datetime import datetime

----> 5 import numpy

6

ipdb> quit

我们还可以使用%run的–p参数对脚本进行性能分析。

In [4]: %run -p vectorsum.py 1000

1058 function calls (1054 primitive calls) in 0.002 CPU seconds

Ordered by: internal time

ncallstottimepercallcumtimepercallfilename:lineno(function)

1 0.001 0.001 0.001 0.001 vectorsum.py:28(pythonsum)

1 0.001 0.001 0.002 0.002 {execfile}

1000 0.000 0.0000.0000.000 {method "append" of 'list' objects}

1 0.000 0.000 0.002 0.002 vectorsum.py:3()

1 0.000 0.0000.0000.000 vectorsum.py:21(numpysum)

3 0.000 0.0000.0000.000 {range}

1 0.000 0.0000.0000.000 arrayprint.py:175(_array2string)

3/1 0.000 0.0000.0000.000 arrayprint.py:246(array2string)

2 0.000 0.0000.0000.000 {method 'reduce' of 'numpy.ufunc' objects}

4 0.000 0.0000.0000.000 {built-in method now}

2 0.000 0.0000.0000.000 arrayprint.py:486(_formatlnteger)

2 0.000 0.0000.0000.000 {numpy.core.multiarray.arange}

1 0.000 0.0000.0000.000 arrayprint.py:320(_formatArray)

3/1 0.000 0.0000.0000.000 numeric.py:1390(array_str)

1 0.000 0.0000.0000.000 numeric.py:216(asarray)

2 0.000 0.0000.0000.000 arrayprint.py:312(_extendLine)

1 0.000 0.0000.0000.000 fromnumeric.py:1043(ravel)

2 0.000 0.0000.0000.000 arrayprint.py:208()

1 0.000 0.000 0.002 0.002 :1()

11 0.000 0.0000.0000.000 {len}

2 0.000 0.0000.0000.000 {isinstance}

1 0.000 0.0000.0000.000 {reduce}

1 0.000 0.0000.0000.000 {method 'ravel' of 'numpy.ndarray' objects}

4 0.000 0.0000.0000.000 {method 'rstrip' of 'str' objects}

3 0.000 0.0000.0000.000 {issubclass}

2 0.000 0.0000.0000.000 {method 'item' of 'numpy.ndarray' objects}

1 0.000 0.0000.0000.000 {max}

1 0.000 0.0000.0000.000 {method 'disable' of ' lsprof.Profiler'

objects}

根据性能分析的结果,可以更多地了解程序的工作机制,并能够据此找到程序的性能瓶颈。使用%hist命令可以查看命令行历史记录。

In [2]: a=2+2

In [3]: a

Out[3]: 4

In [4]: %hist

1: _ip.magic("hist ")

2: a=2+2

3: a

通过前面的介绍,希望你也认为IPython是非常有用的工具了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值