笔记:IPython基础
IPython基础
Python的优点
简单易学;能完成负责的任务;代码可读性高,易于维护;支持OOP等
IPython
Python提供了类似shell的交互式解释器
IPython集成了交互式Python,具有卓越的Python Shell,性能远远由于PYthon Shell
在fedora中可以直接通过yum安装 IPython
yum install ipython
FC14中为 IPython 0.10.1 -- An enhanced Interactive Python.
[ty@tiany ~]$ ipython
Python 2.7 (r27:82500, Sep 16 2010, 18:03:06)
Type "copyright", "credits" or "license" for more information.
IPython 0.10.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]:
在IPython中的操作类似于Python
并且可以通过Tab提供自动完成功能
IPython的Magic函数
IPython提供了功能强大、内建的Magic函数,定义为: IPython中将任何第一个字母为%的行视为Magic函数的特殊调用,可以控制IPython,为其增加许多系统功能。
例如 %cd mydir , 进入mydir目录
In [1]: %lsmagic
Available magic functions:
%Exit %Pprint %Quit %alias %autocall %autoindent %automagic %bg %bookmark %cd %clear %color_info %colors %cpaste %debug %dhist %dirs %doctest_mode %ed %edit %env %exit %hist %history %logoff %logon %logstart %logstate %logstop %lsmagic %macro %magic %p %page %paste %pdb %pdef %pdoc %pfile %pinfo %popd %profile %prun %psearch %psource %pushd %pwd %pycat %quickref %quit %r %rehash %rehashx %rep %reset %run %runlog %save %sc %store %sx %system_verbose %time %timeit %unalias %upgrade %who %who_ls %whos %xmode
Automagic is ON, % prefix NOT needed for magic functions.
在命令后面加 ? ,便可以输出帮助
In [5]: %who ?
Type: Magic function
Base Class:
String Form: >
Namespace: IPython internal
File: /usr/lib/python2.7/site-packages/IPython/Magic.py
Definition: %who(self, parameter_s='')
Docstring:
Print all interactive variables, with some minimal formatting.
If any arguments are given, only variables whose type matches one of
these are printed. For example:
%who function str
will only list functions and strings, excluding all other types of
variables. To find the proper type names, simply use type(var) at a
command line to see how python prints type names. For example:
In [1]: type('hello')
Out[1]:
indicates that the type name for strings is 'str'.
%who always excludes executed names loaded through your configuration
file and things which are internal to IPython.
This is deliberate, as typically you may load many modules and the
purpose of %who is to show you only what you've manually defined.
Shell命令的执行
IPython中可以执行cd ls 等常见的命令,
执行shell的其他命令时在命令前加感叹号( ! )
In [1]:!ping 202.202.0.33
PING 202.202.0.33 (202.202.0.33) 56(84) bytes of data.
64 bytes from 202.202.0.33: icmp_req=1 ttl=252 time=0.256 ms
64 bytes from 202.202.0.33: icmp_req=2 ttl=252 time=0.355 ms
字符处理
IPython提供了强有力的字符处理能力
首先将 ls -lh的输出结果存在flist中,它返回的是种类列表的结构
In [11]: flist = !ls -hl
In [12]: flist
Out[12]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: total 48K
1: drwxr-xr-x. 2 ty ty 4.0K Mar 7 18:43 Desktop
2: drwxrwxr-x 6 ty ty 4.0K Mar 7 12:56 development
3: drwxr-xr-x. 2 ty ty 4.0K Mar 7 13:35 Documents
4: drwxr-xr-x. 4 ty ty 4.0K Mar 7 14:58 Downloads
5: drwxrwxr-x. 4 ty ty 4.0K Mar 3 22:52 linuxInstall_HelpFile
6: drwxr-xr-x. 5 ty ty 4.0K Mar 2 21:26 Music
7: drwxr-xr-x. 3 ty ty 4.0K Mar 3 23:24 Pictures
8: drwxr-xr-x. 2 ty ty 4.0K Mar 2 19:43 Public
9: drwxrwxr-x. 6 ty ty 4.0K Mar 3 23:42 Software
10: drwxr-xr-x. 2 ty ty 4.0K Mar 2 19:43 Templates
11: drwxrwxr-x 2 ty ty 4.0K Mar 4 11:31 tmp
12: drwxr-xr-x. 5 ty ty 4.0K Mar 4 13:54 Videos
可以通过grep 和 fields 对flist进行过滤
In [13]: flist.grep('Do')
Out[13]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: drwxr-xr-x. 2 ty ty 4.0K Mar 7 13:35 Documents
1: drwxr-xr-x. 4 ty ty 4.0K Mar 7 14:58 Downloads
grep和fields输出的是相同类型的对象,可以嵌套调用
例如只显示第一和第四项内容
In [15]: flist.grep('Do').fields(0,3)
Out[15]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: drwxr-xr-x. ty
1: drwxr-xr-x. ty
In [17]: flist.grep('Do').fields(1,8).grep('Down')
Out[17]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: 4 Downloads
在grep中也可以使用field进行过滤
In [18]: flist.grep('Do',field=8)
Out[18]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: drwxr-xr-x. 2 ty ty 4.0K Mar 7 13:35 Documents
1: drwxr-xr-x. 4 ty ty 4.0K Mar 7 14:58 Downloads