IPython基礎使用_Round2
目錄
目錄
前言
軟件環境
Ipython的字符串處理
Ipython的魔力函數Magic
lsmagic Output所有魔力函數
查看Magic的源碼
env 顯示系統環境變量
history 查看指令執行歷史記錄
pwd 顯示當前目錄路徑
pycat 語法高亮顯示一個Python程序文件
save 將歷史指令篩選并保存到文件中
run 在IPython執行一個腳本
timeit 測試一條指令執行的時間
bookmark 記憶路徑
file 創建并編輯一個文件
Python 和 shell 的結合
在Shell中使用Python變量
將Shell 的結果賦值給Python vairable
前言
上篇博文中記錄了Setup Python環境和Iptyhon,以及Ipython的特性和部分基礎功能。現在繼續更深入的學習Ipython的特殊功能,以及Ipython的魔力函數—Magic
軟件環境
系統
Ubuntukylin 14.04
軟件
Python 2.7.6
Ipython 4.0.0
Ipython的字符串處理
Ipython內嵌grep()函數,可以實現類似awk、grep指令的功能
a). 獲取要處理的文本內容
b). 對文本進行行過濾
contentsInfo.grep(“lineFilter”)
c). 行過濾后再進行的列過濾
contentsInfo.grep(“lineFilter”).fields(rangeArray)
也可以直接進行列過濾contentsInfo.fields(rangeArray)
e). 對進行過濾后的Out[ ]作選擇操作
Ipython的魔力函數—Magic
Magic是Ipython內置的豐富的函數庫,一般以%開頭,支持查看函數的源代碼,Magic是Ipython主要內容,為編程提供了很好的便捷性能。其中Magic有兩種類型,分別是%:line magic和%%:call magic。前者針對處理一行代碼,反之,后者處理多行代碼。
%lsmagic — Output所有魔力函數
In [39]: %lsmagic Out[39]: Available line magics: %alias %alias_magic %autocall %autoindent %automagic %bookmark %cat %cd %clear %colors %config %cp %cpaste %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %install_default_config %install_ext %install_profiles %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %paste %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile Automagic is ON, % prefix IS NOT needed for line magics.
1
2
3
4
5
6
7
8
9
可以看見Ipython擁有豐富的內置函數。下面著重介紹常用的幾個。
查看Magic的源碼
In [69]: %edit??
1
??:查看函數、指令、對象的詳細信息,包括用法、函數的源代碼等
?:查看函數、指令、對象、變量的更加詳細使用方法、文檔,但不包括函數的源代碼
%env 顯示系統環境變量
In [40]: %env
1
%history 查看指令執行歷史記錄
In [41]: %history -n
1
%pwd 顯示當前目錄路徑
IpythonShell中的pwd是%pwd的鏈接,而%pwd是系統Bash中pwd的鏈接。Ipython中大部分可以直接使用的系統指令都是經過了這樣的處理后才能使用。
In [42]: %pwd Out[42]: u'/usr/local/src/pyScript' In [43]: pwd Out[43]: u'/usr/local/src/pyScript'
1
2
3
4
5
%pycat 語法高亮顯示一個Python程序文件
In [45]: %pycat pyFile.py
1
%save 將歷史指令篩選并保存到文件中
常%save 結合 %history 、%edit 指令一起使用。
%save fileName commandsNnmber
先將歷史記錄保存到文件中
In [54]: %save testSave 1-3 14-15 24 27-30 File `testSave.py` exists. Overwrite (y/[N])? y The following commands were written to file `testSave.py`: portInfo = get_ipython().getoutput(u'netstat -lpntu') portInfo portInfo.grep("tcp") diskInfo = get_ipython().getoutput(u'df -TH') diskInfo diskInfo.grep("ext4",field=1).fields(0) diskInfo.grep("ext4",field=1).fields(0,3,6)[0] Out[1] Out[26] Out[26][4]
1
2
3
4
5
6
7
8
9
10
11
12
13
再編輯保存了歷史指令的testSave文件,可以非常方便的將調試的指令寫入到文件中,再作進一步的處理。
In [56]: %edit testSave.py # coding: utf-8 portInfo = get_ipython().getoutput(u'netstat -lpntu') portInfo portInfo.grep("tcp") diskInfo = get_ipython().getoutput(u'df -TH') diskInfo diskInfo.grep("ext4",field=1).fields(0) diskInfo.grep("ext4",field=1).fields(0,3,6)[0] Out[1] Out[26] Out[26][4]
1
2
3
4
5
6
7
8
9
10
11
12
%run 在IPython執行一個腳本
在IPython中因無法使用python指令來執行一個.py的腳本,所以IPython也內嵌了一個%run函數來代替python command。其中run是%run的鏈接,所以有下面兩種方式,而且還提供能多個選項來滿足開發需求。
In [8]: run test.py My Name is Jmilk! In [9]: %run test.py My Name is Jmilk!
1
2
3
4
5
a). -t 輸出程序執行的CPU timings, -Nx (x為數字)指定程序重復執行的次數。
In [10]: run -t test.py My Name is Jmilk! IPython CPU timings (estimated): User : 0.00 s. System : 0.00 s. Wall time: 0.00 s. In [12]: run -t -N2 test.py My Name is Jmilk! My Name is Jmilk! IPython CPU timings (estimated): Total runs performed: 2 Times : Total Per run User : 0.00 s, 0.00 s. System : 0.00 s, 0.00 s. Wall time: 0.00 s.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
b). -p 開啟Python profiler(Python 程序性能調優)
In [18]: run -t -N2 -p test.py My Name is Jmilk!
1
2
可以顯示詳細的程序運行時資源使用量的參數。
c). -d 進入ipdb控制臺中,-bx(x為數字)指定行數打斷點。是一個不錯的程序調試平臺。
In [19]: run -d test.py Breakpoint 1 at /usr/local/src/pyScript/test.py:1 NOTE: Enter 'c' at the ipdb> prompt to continue execution. > /usr/local/src/pyScript/test.py(1)
1
2
3
4
5
6
7
上述%run函數的幾個選項都非常值得深入學習,在以后會繼續為大家介紹。
%timeit 測試一條指令執行的時間
%timeit 函數默認會重復執行10000次此條指令,并取出3個最佳的結果來計算平均值,從而得出一條指令執行所需要的時間。
In [23]: %timeit [i*i for i in range(1000)] The slowest run took 5.67 times longer than the fastest. This could mean that an intermediate result is being cached 10000 loops, best of 3: 160 μs per loop
1
2
3
a). -n 指定重復執行的次數,默認為10000。-p 顯示出詳細的高精度時間。
In [31]: %timeit -n 100 [i*i for i in range(1000)] 100 loops, best of 3: 231 μs per loop In [34]: %timeit -p 1000 [i*i for i in range(1000)] 1000 loops, best of 3: 162.34493255615234375 μs per loop
1
2
3
4
5
b). -r 指定取多少個最佳結果來計算平均值,默認為3個。
In [26]: %timeit -p 1000 -r 4 [i*i for i in range(1000)] 10000 loops, best of 4: 162.2743129730224609375 μs per loop
1
2
%bookmark 記憶路徑
In [36]: %bookmark workspace /usr/local/src/pyScript/ In [39]: cd workspace (bookmark:workspace) -> /usr/local/src/pyScript/ /usr/local/src/pyScript
1
2
3
4
5
注意的是,使用%bookmark的定義的路徑無法使用 !cd 來進入。既系統Shell不是別%bookmark定義的變量。
%file 創建并編輯一個文件
In [58]: %%file testFile.py def myFuncation(): name = 'Jmilk' print "My name is %s" % name if __name__ == '__main__': myFuncation() ....: Overwriting testFile.py In [59]: run testFile.py My name is Jmilk
1
2
3
4
5
6
7
8
9
10
11
Python 和 shell 的結合
在Shell中使用Python變量
可以在定義Python變量后,使用$符來結合Shell command和Python variable。需要注意的是,mkdir其實是%mkdir的鏈接,既本質上是IPython 的內嵌函數%mkdir 可以識別variable “folder” 。
In [62]: folder = 'testVariable' In [63]: mkdir $folder In [65]: cd $folder /usr/local/src/pyScript/testVariable #反之,指令:!cd $folder 則無法執行
1
2
3
4
5
6
7
將Shell 的結果賦值給Python vairable。
vairable = !shellCommands
In [83]: import re In [84]: portInfo = !netstat -lpntu | grep "tcp" In [86]: portInfo Out[86]: ['tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 2259/memcached ', 'tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 1468/dnsmasq ', 'tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1761/sshd ', 'tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 5483/cupsd ', 'tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 2428/beam.smp ', 'tcp 0 0 0.0.0.0:55672 0.0.0.0:* LISTEN 2428/beam.smp ', 'tcp 0 0 0.0.0.0:3260 0.0.0.0:* LISTEN 1823/tgtd ', 'tcp 0 0 0.0.0.0:54531 0.0.0.0:* LISTEN 2428/beam.smp ', 'tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1975/mysqld ', 'tcp6 0 0 :::80 :::* LISTEN 2845/apache2 ', 'tcp6 0 0 :::4369 :::* LISTEN 2332/epmd ', 'tcp6 0 0 :::22 :::* LISTEN 1761/sshd ', 'tcp6 0 0 ::1:631 :::* LISTEN 5483/cupsd ', 'tcp6 0 0 :::3260 :::* LISTEN 1823/tgtd ', 'tcp6 0 0 :::5672 :::* LISTEN 2428/beam.smp '] In [88]: for i in portInfo: print re.sub(r"0","J",i) ....: tcp J J 127.J.J.1:11211 J.J.J.J:* LISTEN 2259/memcached tcp J J 127.J.1.1:53 J.J.J.J:* LISTEN 1468/dnsmasq tcp J J J.J.J.J:22 J.J.J.J:* LISTEN 1761/sshd tcp J J 127.J.J.1:631 J.J.J.J:* LISTEN 5483/cupsd tcp J J J.J.J.J:15672 J.J.J.J:* LISTEN 2428/beam.smp tcp J J J.J.J.J:55672 J.J.J.J:* LISTEN 2428/beam.smp tcp J J J.J.J.J:326J J.J.J.J:* LISTEN 1823/tgtd tcp J J J.J.J.J:54531 J.J.J.J:* LISTEN 2428/beam.smp tcp J J J.J.J.J:33J6 J.J.J.J:* LISTEN 1975/mysqld tcp6 J J :::8J :::* LISTEN 2845/apache2 tcp6 J J :::4369 :::* LISTEN 2332/epmd tcp6 J J :::22 :::* LISTEN 1761/sshd tcp6 J J ::1:631 :::* LISTEN 5483/cupsd tcp6 J J :::326J :::* LISTEN 1823/tgtd tcp6 J J :::5672 :::* LISTEN 2428/beam.smp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
將Shell commands的結果賦值給Pyhon variable后,可以將此List類型的變量再作后期的處理。其中re(regular expression) module在Python中實現正則表達式的功能。re.sub(substitute)則是實現正則表達式的替換功能。
到此IPython的基本介紹也完成了,IPython是一個非常有意思的東西,他所能實現的功能,總會給你驚喜。當然,,這也需要失時間去熟悉他。往后會結合Python的語法和代碼實現來繼續學習IPython。
Jmilk
IPython Python
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。