国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > IPython Magic Functions

IPython Magic Functions

来源:程序员人生   发布时间:2014-04-04 14:55:48 阅读次数:3619次
将 IPython 用熟了,可以轻松在试验、编码、测试间 "无缝折腾"。

$ ipython

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
Type "copyright", "credits" or "license" for more information.

IPython 0.10 -- 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.
1. 帮助

?: 查看对象信息。
??: 获取更详细的信息,比如源代码。
%quickref: IPython 简易手册。
%magic: IPython Magic 详细手册。
%lsmagic: 所有可用的 Magic 函数。

"?" 和 "??" 是 IPython 中最常用的命令。

In [96]: test ?
Type: function
Base Class: <type 'function'>
String Form: <function test at 0x190e1b8>
Namespace: Interactive
File: /home/yuhen/test/test.py
Definition: test()
Docstring:
<no docstring>

In [97]: test ??
Type: function
Base Class: <type 'function'>
String Form: <function test at 0x190e1b8>
Namespace: Interactive
File: /home/yuhen/test/test.py
Definition: test()
Source:
def test():
pass
2. 系统

%alias: 显示或定义系统命令别名。
%unalias: 删除别名。
%sc: 执行系统命令,并将输出结果存储到变量。
%sx: 执行系统命令,并将输出结果分割存储成列表。

(1) 定义别名和 shell 下的意思差不多。

In [13]: %alias
Total number of aliases: 15
Out[13]:
[('cat', 'cat'),
('clear', 'clear'),
('less', 'less'),
('mkdir', 'mkdir'),
('rmdir', 'rmdir'),
('cp', 'cp -i'),
('lc', 'ls -F -o --color'),
('ldir', 'ls -F -o --color %l | grep /$'),
('lf', 'ls -F -o --color %l | grep ^-'),
('lk', 'ls -F -o --color %l | grep ^l'),
('ll', 'ls -lF'),
('ls', 'ls -F'),
('lx', 'ls -F -o --color %l | grep ^-..x'),
('mv', 'mv -i'),
('rm', 'rm -i')]

In [14]: %alias mydir ls -la %1

In [15]: mydir ~
ls: cannot access %1: No such file or directory
/home/yuhen:
total 88
drwxr-xr-x 12 yuhen yuhen 4096 2010-07-18 21:15 ./
drwxr-xr-x 3 root root 4096 2010-06-13 09:44 ../
drwxr-xr-x 4 yuhen yuhen 4096 2010-07-11 11:07 backup/
-rw------- 1 yuhen yuhen 9228 2010-07-18 23:44 .bash_history
-rw-r--r-- 1 yuhen yuhen 220 2010-06-13 09:44 .bash_logout
-rw-r--r-- 1 yuhen yuhen 3103 2010-06-13 09:44 .bashrc

In [16]: %unalias mydir
还可以用 "%s" 来代替 "%1" 接收参数。

(2) 在执行系统命令时,通常以 "!" 开头(可省略)。还可以直接使用 Python 变量($var_name),如果需要引用系统环境变量,需要对 "$" 转义($$)。

In [20]: !df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/yuhen--server64-root
19G 1.8G 16G 10% /
none 243M 216K 242M 1% /dev
none 247M 0 247M 0% /dev/shm
none 247M 48K 247M 1% /var/run
none 247M 0 247M 0% /var/lock
none 247M 0 247M 0% /lib/init/rw
/dev/sda1 228M 17M 199M 8% /boot

In [21]: path = "/var/www"

In [22]: !echo $path
/var/www

In [23]: !echo $$HOME
/home/yuhen
(3) %sc 捕捉输出结果到单个字符串变量, %sx 按 "" 切分成列表。

In [41]: %sc s = ls -l ~

In [42]: s
Out[42]: LSString (.p, .n, .l, .s available). Value:
total 20
drwxr-xr-x 4 yuhen yuhen 4096 2010-07-11 11:07 backup
drwxr-xr-x 3 yuhen yuhen 4096 2010-06-28 23:49 mongo
drwxr-xr-x 7 yuhen yuhen 4096 2010-06-27 09:30 projects
drwxr-xr-x 4 yuhen yuhen 4096 2010-06-16 19:43 redis
drwxr-xr-x 2 yuhen yuhen 4096 2010-07-18 23:03 test

In [43]: s = %sx ls -l ~

In [44]: s
Out[44]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: total 20
1: drwxr-xr-x 4 yuhen yuhen 4096 2010-07-11 11:07 backup

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生