Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1690065
  • 博文数量: 410
  • 博客积分: 9563
  • 博客等级: 中将
  • 技术积分: 4517
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-03 19:59
个人简介

文章分类

全部博文(410)

文章存档

2017年(6)

2016年(1)

2015年(3)

2014年(4)

2013年(32)

2012年(45)

2011年(179)

2010年(140)

分类: Python/Ruby

2010-07-08 13:25:12

ipython很好用,最近一直在用,但是偶尔中文会有乱码
 编辑vi ~/.ipython/ipythonrc ,注释掉红色的这行,重新进入ipython,就好了
  1. # These commands allow you to indent/unindent easily, with the 4-space
  2. # convention of the Python coding standards. Since IPython's internal
  3. # auto-indent system also uses 4 spaces, you should not change the number of
  4. # spaces in the code below.
  5. #readline_parse_and_bind "\M-i": " "

ipython 是一个 python 的交互式 shell,比默认的 python shell 好用得多,支持变量自动补全,自动缩近,支持 bash shell 命令,内置了许多很有用的功能和函数。在 ubuntu 下只要 sudo apt-get install ipython 就装好了,通过 ipython 启动。

下面是 ipython 中几个简单好用的 magic 函数:


%bg function把 function 放到后台执行,例如: %bg myfunc(x, y, z=1),之后可以用jobs将其结果取回。myvar = jobs.result(5) 或 myvar = jobs[5].result。另外,jobs.status() 可以查看现有任务的状态。
%ed 或 %edit编辑一个文件并执行,如果只编辑不执行,用 ed -x filename 即可。
%env显示环境变量
%hist 或 %history显示历史记录
%macro name n1-n2 n3-n4 ... n5 .. n6 ...创建一个名称为 name 的宏,执行 name 就是执行 n1-n2 n3-n4 ... n5 .. n6 ... 这些代码。
%pwd显示当前目录
%pycat filename用语法高亮显示一个 python 文件(不用加.py后缀名)
%save filename n1-n2 n3-n4 ... n5 .. n6 ...将执行过多代码保存为文件
%time statement计算一段代码的执行时间
%timeit statement自动选择重复和循环次数计算一段代码的执行时间,太方便了。

  另外,ipython 中用 ! 表示执行 shell 命令,用 $ 将 python 的变量转化成 shell 变量。通过这种两个符号,我们就可以做到和 shell 命令之间的交互,可以非常方便地做许多复杂的工作。比如你可以很方便地创建一组目录:


for i in range(10):
s = "dir%s" % i
!mkdir $s

不过写法上还是有一些限制,$ 后面只能跟变量名,不能直接写复杂表达式,$"dir%s"%i 就是错误的写法了,所以要先完全产生 python 的变量以后再用。像

for i in !ls: print i

这样的写法也是错的,可以这样:

a = !ls
for i in a: print i

  还有一点需要说明,就是执行普通的 shell 命令中如果有 $ 的话需要用两个 $。比如原来的echo $PATH现在得写成!echo $$PATH。

1、替代bash shell

用ipython -p pysh 启动ipython,能够执行所有shell命令,形成一个混合pythonunix shell的环境

2、书签
bookmark 可以定义一个书签目录,然后可以用cd 直接进入书签目录
bookmark -l 可以列出所有的书签目录
详细的帮助可以 bookmark? 列出
3、dhist 列出所有使用过的目录,然后可以用 cd -n 进入该目录(和bash的运行历史命令有点像)
4、可以用$ 将python的变量转化成shell 变量
a='*.py'
ls -la $a
5、保存系统命令的输出结果赋值给python变量
res = ! ls $a
print res
6、whos
列出所有当前的对象(结果的格式为)
Variable   Type      Data/Info
------------------------------
a          str       *.py
mm         module   
res        SList     ['mm.py', 'selectwork.py']
7、edit
生成一个临时文件,保存时自动执行
edit -p 编辑上次的临时文件
edit -x 修改但是不运行
8、历史 hist
现在要将第4,5,6句代码导出到编辑器,只要输入:

  edit 4:7

阅读(6940) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~