2018-03-28 00:56:39 中斷正在執行的代碼 無論是%run執行的腳本還是長時間運行的命令ctrl + cIn [1]: KeyboardInterrupt 執行剪切板中的代碼 ctrl-shift-V %paste,%cpaste魔術函數%paste可以承載剪切板中的一切文本,併在s ...
2018-03-28 00:56:39
中斷正在執行的代碼
無論是%run執行的腳本還是長時間運行的命令
ctrl + c
In [1]:
KeyboardInterrupt
執行剪切板中的代碼
ctrl-shift-V
In [1]: def f(x,y,z): ...: return (x + y) / z ...: a = 5 ...: b = 6 ...: c = 7.5 ...:
%paste,%cpaste魔術函數
%paste可以承載剪切板中的一切文本,併在shell中以整體形式執行
%cpaste跟%paste差不多,只不過多了用於粘貼代碼的特殊符號
In [2]: result = f(a, b, c) In [3]: %paste def f(x,y,z): return (x + y) / z a = 5 b = 6 c = 7.5 result = f(a, b, c) ## -- End pasted text -- In [4]: %cpaste Pasting code; enter '--' alone on the line to stop or use Ctrl-D. """ %cpaste在最終執行之前,你想粘貼多少就多少,若你發現粘貼的代碼有錯,只需按下ctrl+c可終止%cpaste提示符 """ :def f(x,y,z): : return (x + y) / z :a = 5 :b = 6 :c = 7.5 : :result = f(a, b, c) : :-- In [8]: %paste def f(x,y,z): return (x + y) / z a = 5 b = 6 c = 7.5
如果你粘貼的是一段縮進代碼,就會引發一個IndentationError
python語言還是很嚴格的
result = f(a, b, c) y = 8 ## -- End pasted text -- File "<ipython-input-8-74e10d8df765>", line 8 y = 8 ^ IndentationError: unexpected indent In [9]: %cpaste Pasting code; enter '--' alone on the line to stop or use Ctrl-D. :def f(x,y,z): : return (x + y) / z :a = 5 :b = 6 :c = 7.5 : :result = f(a, b, c) : y = 8 :<EOF> File "<ipython-input-9-74e10d8df765>", line 8 y = 8 ^ IndentationError: unexpected indent
鍵盤快捷鍵
Ctrl+P或向上 後向搜索命令歷史中以當前輸入的文本開頭的命令
Ctrl+N或向下 前向搜索命令歷史中以當前輸入的文本開頭的命令
Ctrl+R 按行讀取的反向歷史搜索(部分匹配)
(reverse-i-search)`a': def add_numbers(a, b): ...: """ ...: Add two numbers together ...: Returns ...: ------- ...: the_sum : type of arguments """
Ctrl+Shift+V 從剪切板粘貼文本
Ctrl+C 中止當前正在執行的代碼
Ctrl+A 將游標移到行首
Ctrl+E 將游標移到行尾
Ctrl+K 刪除從游標開始至行尾的文本
Ctrl+U 清除當前的所有文本(與Ctrl+K相反)
Ctrl+F 將游標向前移到一個字元
Ctrl+B 將游標向後移到一個字元
Ctrl+L 清屏
魔術命令
In [1]: import numpy as np In [2]: a = np.random.randn(100,100) asd In [3]: %timeit np.dot(a,a) 1000 loops, best of 3: 638 µs per loop
魔術命令預設可以不帶百分號,只要沒有定義與其同名的變數即可/
%automagic打開關閉
%quickref 顯示IPython的快速參考
%magic 顯示所有魔術命令的詳細文檔
%debug 從最新的異常跟蹤的底部進入互動式調試器
%hist 列印命令的輸入(可選輸出)歷史
%pdb 在異常發生後自動進入調試器
%paste 執行剪貼板中的Python代碼
%cpaste 打開一個特殊提示符以便手工粘貼待執行的Python代碼
%reset 刪除interactive命名空間中的全部變數/名稱
%page OBJECT 通過分頁器列印輸出OBJECT
%run script.py 在IPython中執行一個Python腳本文件
%prun statement 通過cProfile執行statement,並列印分析器的輸出結果
%time statement 報告statement的執行時間
%timeit statement 多次執行statement以計算系綜平均執行時間。對那些執行時 間非常小的代碼很有用
%who、%who_ls、%whos 顯示interactive命名空間中定義的變數,信息級別/冗餘度可變
%xdel variable 刪除variable,並嘗試清除其在IPython中的對象上的一切引用
matplotlib集成與pylab模式
輸入和輸出變數
最近的兩個輸出結果分別保存在_和__變數中
輸入的文本保存在_ix變數中,其中x是輸入行的行號
In [1]: 2 ** 3 Out[1]: 8 In [2]: _ Out[2]: 8 In [3]: __ Out[3]: 8 In [4]: foo = 'bar' In [5]: foo Out[5]: 'bar' In [6]: _i5 Out[6]: u'foo' In [7]: _5 Out[7]: 'bar' In [8]: exec _5
記錄輸入和輸出變數
%logstart 即可開始記錄日誌
如果在寫代碼的過程中,突然想要保存所有工作時,直接啟動日誌功能就好啦
In [11]: %logstart Activating auto-logging. Current session state plus future input saved. Filename : ipython_log.py Mode : rotate Output logging : False Raw input log : False Timestamping : False State : active