zoukankan      html  css  js  c++  java
  • 使用IPython的好处

    相较于Linux上的python shell以及Windows上的IDLE,IPython可以使用python shell的一切,IPython还有许多magic函数可以使用,在IPython使用的同时还可以使用Unix shell命令(只需在命令前加!即可),推荐使用ipython,当你在学习python的一些方法时可以结合使用bash命令来达到更好的效果。

    1.Tab自动完成,当然python shell也可具有tab自动完成功能但需要以下处理。

    Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
    [GCC 7.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import rlcompleter,readline
    >>> readline.parse_and_bind('tab:complete')
    >>>
    >>> import os
    >>> os.li
    os.linesep os.link( os.listdir( os.listxattr(
    >>> os.list

    2.魔力函数

    IPython会把第一个字母为%的行视为魔力函数的调用,魔力函数可由%lsmagic或%<tab>列出。

    In [1]: %alias nss netstat -lptn
    
    In [2]: nss
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
    tcp        0      0 0.0.0.0:39505               0.0.0.0:*                   LISTEN      1225/rpc.statd      
    tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN

      In [3]: store nss (存储别名,下次启动IPython时可用)
      Alias stored: nss (netstat -lptn)

    3.特殊的shell执行

    之前提到过IPython中执行shell命令是在命令前加上!,在shell中可以使用重定向的方式来处理命令执行的结果,如ps aux | grep "root",在IPython中可以使用将命令执行结果存储为list再用处理list的方式进行下一步处理。

    In [48]: a = !netstat -plnt
    
    In [49]: a
    Out[49]: 
    ['Active Internet connections (only servers)',
     'Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   ',
     'tcp        0      0 0.0.0.0:39505               0.0.0.0:*                   LISTEN      1225/rpc.statd      ',
     'tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1375/sshd           ',
    ...]

      In [50]: a.grep("sshd",prune=True).fields(0,1,6) #bash shell:netstat -plnt | grep -v "sshd" | awk '{print $1,$2,$6}'
      Out[50]:
      ['Active Internet',
      'Proto Recv-Q Address',
      'tcp 0 1225/rpc.statd',
      'tcp 0 1508/master',

      ...]

    shell命令和python命令同时使用:

    In [84]: for i in range(10):
        ...:     !echo "$i"
        ...:     
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9

    4.获取详细帮助信息,魔力函数%pfile,%pdoc,%pdef,%psearch可查找python对象,%pinfo(?),%psource(??)可查看运行对象的文件(源码)。

    5.历史hist,Ctrl-s

  • 相关阅读:
    C++-POJ1020-Anniversary Cake[搜索][dfs]
    C++-POJ1988-Cube Stacking[数据结构][并查集]
    大佬的代码
    C++-POJ3349-Snowflake Snow Snowflakes[STL][set][hash未写]
    C++-POJ3274-Gold Balanced Lineup[hash]
    ListView 在设备切换横竖屏时保存状态
    Android Studio 常见命令
    android textView 总是有paddingtop怎么解决
    ionic build Android错误记录 error in opening zip file
    git grep 或者 ag 进行快速代码搜索
  • 原文地址:https://www.cnblogs.com/hana-alice/p/9330617.html
Copyright © 2011-2022 走看看