Ipython & Ipython Notebook
介绍一下一个增强的交互式编译平台Ipython和Ipython
Notebook.
一个相关的介绍1203_ipython_pycon.pdf
Ipython Basics
Ipython的可读性更佳(pretty printed)
In
[542]: data = {i : randn() for i in range(7)}
In [543]: data
Out[543]:
{0: 0.6900018528091594,
1: 1.0015434424937888,
2: -0.5030873913603446,
3: -0.6222742250596455,
4: -0.9211686080130108,
5: -0.726213492660829,
6: 0.2228955458351768}
#和传统的shell里面相比
>>> from numpy.random import randn
>>> data = {i : randn() for i in
range(7)}
>>> print data
{0: -1.5948255432744511, 1: 0.10569006472787983,
2: 1.972367135977295,
3: 0.15455217573074576, 4: -0.24058577449429575,
5: -1.2904897053651216,
6: 0.3308507317325902} |
自动补全(Tab Completion)
Ipython比python自带shell多了自动补全功能 ,用Tab实现:
In [1]: an_apple = 27
In [2]: an_example = 42
In [3]: an<Tab>
an_apple and an_example any
In [3]: b = [1, 2, 3]
In [4]: b.<Tab>
b.append b.extend b.insert b.remove b.sort
b.count b.index b.pop b.reverse |
自动补全功能方便我们写重复的代码
%run命令
我们能在Ipython里跑一个python文件
比如有一个ipython_script_test.py文件
def
f(x, y, z):
return (x + y) / z
a = 5
b = 6
c = 7.5
result = f(a, b, c)
In [550]: %run ipython_script_test.py #运行
In [551]: c
Out[551]: 7.5
In [552]: result
Out[552]: 1.4666666666666666 |
魔术命令 (Magic Command)
Ipython里特有的一些命令,以%开头作为标志
保留格式的粘帖 %cpaste
In
[7]: %cpaste # 有时候要复制一大段代码到shell里而直接粘帖会破坏换行格式,这时%cpaste就派用场了。
Pasting code; enter '--' alone on the line
to stop or use Ctrl-D. #右键,粘帖
:x = 5
:y = 7
:if x > 5:
: x += 1
:
: y = 8
:-- # 结束的时候打‘--’ |
计时 %timeit
In
[554]: a = np.random.randn(100, 100)
In [555]: %timeit np.dot(a, a)
10000 loops, best of 3: 69.1 us per loop |
重置 %reset
In
[1]: %reset? # ?是内省(Introspection)功能,可以看到reset的细节
Resets the namespace by removing all names
defined by the user.
Parameters
----------
-f : force reset without asking for confirmation.
# 两种不同的reset
-s : 'Soft' reset: Only clears your namespace,
leaving history intact.
References to objects may be kept. By default
(without this option),
we do a 'hard' reset, giving you a new session
and removing all
references to objects from the current session.
Examples
--------
In [6]: a = 1
In [7]: a
Out[7]: 1
In [8]: 'a' in _ip.user_ns
Out[8]: True
In [9]: %reset -f
In [1]: 'a' in _ip.user_ns
Out[1]: False |
总结一些Magic Command,有待大家自己去尝试
%quickref
Display the IPython Quick Reference Card
%magic Display detailed documentation for
all of the available magic commands
%debug Enter the interactive debugger at the
bottom of the last exception traceback
%hist Print command input (and optionally
output) history
%pdb Automatically enter debugger after any
exception
%paste Execute pre-formatted Python code from
clipboard
%cpaste Open a special prompt for manually
pasting Python code to be executed
%reset Delete all variables / names defined
in interactive namespace
%page OBJECT Pretty print the object and display
it through a pager
%run script.py Run a Python script inside
IPython
%prun statement Execute statement with cProfile
and report the profiler output
%time statement Report the execution time
of single statement
%timeit statement Run a statement multiple
times to compute an emsemble average execution
time. Useful for timing code with very short
execution time
%who, %who_ls, %whos Display variables defined
in interactive namespace, with varying levels
of information / verbosity
%xdel variable Delete a variable and attempt
to clear any references to the object in the
IPython internals |
Ipython Notebook
Notebook 为将代码执行与实时计算文档的创建组合起来的交互式计算提供了工作环境。这些
Notebook 文件可以包含任意文本、数学公式、输入代码、结果、图形、视频以及新型 Web
浏览器能够显示的任何其他种类的媒体。
个人体会: 可以修改已经执行过得语句,重新的到结果(像Mathematica),这让修改变得很方便。
参考网站 http://ipython.org/notebook.html
启动本地notebook
cmd中输ipython notebook |