Teebone Ding Technical Blog

Django, Python, Javascript, Pig, and Hadoop.

IPython 指令筆記 Written in IPython Notebook

tab 可以幫你自動完成函數/變數

  • ex: os.[tab]

%pdoc [MODULE NAME] or [MODULE NAME]? 幫你找出模組的文件

  • ex: %pdoc os or os?

    %pdoc os

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Class docstring:
    OS routines for NT or Posix depending on what system we're on.
    
    This exports:
      - all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc.
      - os.path is one of the modules posixpath, or ntpath
      - os.name is 'posix', 'nt', 'os2', 'ce' or 'riscos'
      - os.curdir is a string representing the current directory ('.' or ':')
      - os.pardir is a string representing the parent directory ('..' or '::')
      - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
      - os.extsep is the extension separator ('.' or '/')
      - os.altsep is the alternate pathname separator (None or '/')
      - os.pathsep is the component separator used in $PATH etc
      - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
      - os.defpath is the default search path for executables
      - os.devnull is the file path of the null device ('/dev/null', etc.)
    
    Programs that import and use 'os' stand a better chance of being
    portable between different platforms.  Of course, they must then
    only use functions that are defined by all platforms (e.g., unlink
    and opendir), and leave all pathname manipulation to os.path
    (e.g., split and join).

%pfile [MODULE NAME] or [MODULE NAME]?? 告訴你函數如何實作

  • ex: os.path.join??

Magic Commands: 神奇指令們

    %magic 查看所有的神奇指令。
    %edit 編輯並執行檔案。
    %edit -x filename 編輯但不執行檔案。
    %edit 5:20 filename 編輯檔案第5行、第20個字元。
    %pwd 顯示目前目錄。
    %time statement 計算程式碼執行的時間。

呼叫系統指令:

  • 如果要在IPython呼叫系統指令,只需要在指令面前加入!就好,例如要執行ping指令。

    !ping ptt.cc

      Ping ptt.cc [140.112.172.4] (使用 32 位元組的資料):
      回覆自 140.112.172.4: 位元組=32 時間=4ms TTL=52
      回覆自 140.112.172.4: 位元組=32 時間=3ms TTL=52
      回覆自 140.112.172.4: 位元組=32 時間=3ms TTL=52
      回覆自 140.112.172.4: 位元組=32 時間=8ms TTL=52
    
      140.112.172.4 的 Ping 統計資料:
          封包: 已傳送 = 4,已收到 = 4, 已遺失 = 0 (0% 遺失),
      大約的來回時間 (毫秒):
          最小值 = 3ms,最大值 = 8ms,平均 = 4ms
    

reload([MODULE NAME]) 重新載入修改後的模組

在IPython notebook 裡面畫圖

1
2
3
4
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline  

記得加入最後一行 %matplotlib inline 才會讓圖片show在notebook裡面

1
2
3
4
5
6
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

plt.plot([1,5,2,4])
[<matplotlib.lines.Line2D at 0x55b0e90>]

插入YouTube影片

1
2
from IPython.lib.display import YouTubeVideo
YouTubeVideo('PjJhg9-EXr8')

嵌入外部網站

1
2
from IPython.core.display import HTML
HTML('<iframe src="http://www.teeboneding.com" width=800 height=400>')

執行python程式檔

  • ex: %run test.py

Comments