網站首頁 編程語言 正文
前言:
IPython
是 Python 的原生交互式 shell 的增強版,可以完成許多不同尋常的任務,比如幫助實現并行化計算;主要使用它提供的交互性幫助,比如代碼著色、改進了的命令行回調、制表符完成、宏功能以及改進了的交互式幫助。
IPython 8.0 醞釀了許久,主要對現有代碼庫和幾個新功能進行了改進。新功能包括在 CLI 中使用 Black 重新格式化代碼、ghost 建議以及突出錯誤節點的更好的回溯,從而使復雜的表達式更易于調試。
1.追溯改進
之前的錯誤回溯顯示一個散列表(hash),用于編譯 Python AST:
In [1]: def foo(): ...: ? ? return 3 / 0 ...: In [2]: foo() --------------------------------------------------------------------------- ZeroDivisionError ? ? ? ? ? ? ? ? ? ? ? ? Traceback (most recent call last) <ipython-input-2-c19b6d9633cf> in <module> ----> 1 foo() <ipython-input-1-1595a74c32d5> in foo() ? ? 1 def foo(): ----> 2 ? ? return 3 / 0 ? ? 3 ZeroDivisionError: division by zero
現在錯誤回溯的格式正確,會顯示發生錯誤的單元格編號:
In [1]: def foo(): ...: ? ? return 3 / 0 ...: Input In [2]: foo() --------------------------------------------------------------------------- ZeroDivisionError ? ? ? ? ? ? ? ? ? ? ? ? Traceback (most recent call last) input In [2], in <module> ----> 1 foo() Input In [1], in foo() ? ? 1 def foo(): ----> 2 ? ? return 3 / 0 ZeroDivisionError: division by zero
第二個回溯改進是stack_data
包的集成;在回溯中提供更智能的信息;它會突出顯示發生錯誤的 AST 節點,這有助于快速縮小錯誤范圍,比如:
def foo(i): ? ? x = [[[0]]] ? ? return x[0][i][0] def bar(): ? ? return foo(0) + foo( ? ? ? ? 1 ? ? ) + foo(2)
調用 bar()
會在 IndexError 的返回行上引發一個 foo,IPython 8.0 可以告訴你索引錯誤發生在哪里:
IndexError Input In [2], in <module> ----> 1 bar() ? ? ? ? ^^^^^ Input In [1], in bar() ? ? ? 6 def bar(): ----> 7 ? ? return foo(0) + foo( ? ? ? ? ? ? ? ? ? ? ? ? ? ? ^^^^ ? ? ? 8 ? ? ? ? 1 ? ? ? ? ?^^^^^^^^ ? ? ? 9 ? ? ) + foo(2) ? ? ? ? ?^^^^ Input In [1], in foo(i) ? ? ? 1 def foo(i): ? ? ? 2 ? ? x = [[[0]]] ----> 3 ? ? return x[0][i][0] ? ? ? ? ? ? ? ? ? ?^^^^^^^
用 ^ 標記的位置在終端會高亮顯示。
第三個回溯改進是最謹慎的,但對生產力有很大影響,在回溯中的文件名后面附加一個冒號 :: 和行號:
ZeroDivisionError ? ? ? ? ? ? ? Traceback (most recent call last) File ~/error.py:4, in <module> ? ? ? 1 def f(): ? ? ? 2 ? ? 1/0 ----> 4 f() File ~/error.py:2, in f() ? ? ? 1 def f(): ----> 2 ? ? 1/0
許多終端和編輯器具有的集成功能,允許在使用此語法時 直接跳轉到錯誤相關的文件/行 。
2.自動建議
Ptpython
允許用戶在ptpython/config.py
中啟用自動建議功能,此功能包含豐富的代碼補全建議,
如圖:
目前,自動建議僅在 emacs 或 vi 插入編輯模式中顯示:
ctrl e
、ctrl f
和 alt f
快捷鍵默認在 emacs 模式下工作。
要在 vi 插入模式下使用這些快捷鍵,必須在 config.py
中創建自定義鍵綁定。
3.使用“?”和"??"查看對象信息
在 IPDB 中,現在可以使用“?”和”? ?“來顯示對象的信息,在使用 IPython 提示符時也可如此操作:
ipdb> partial? Init signature: partial(self, /, *args, **kwargs) Docstring: partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords. File: ? ? ? ? ? ~/.pyenv/versions/3.8.6/lib/python3.8/functools.py Type: ? ? ? ? ? type Subclasses:
4.歷史范圍全局功能
之前使用 %history
?功能時 , 用戶可以指定會話和行的范圍,例如:
~8/1-~6/5 ? # see history from the first line of 8 sessions ago, ? ? ? ? ? ? # to the fifth line of 6 sessions ago.``
或者可以指定全局模式(global):
-g <pattern> ?# glob ALL history for the specified pattern.
但無法同時指定兩者,如果用戶確實指定了范圍和全局模式,則將使用 glob 模式(通配所有歷史記錄),并且將 忽略范圍 。
現在此功能獲得了增強,如果用戶同時指定范圍和 glob 模式,則 glob 模式將應用于指定的歷史范圍。
原文鏈接:https://www.tuicool.com/articles/rYJvqye
相關推薦
- 2023-03-05 Kotlin協程低級api?startCoroutine與ContinuationIntercept
- 2023-05-10 Pandas數據分析多文件批次聚合處理實例解析_python
- 2021-12-02 Oracle數據庫產重啟服務和監聽程序命令介紹_oracle
- 2022-10-26 Golang?Mutex?原理詳細解析_Golang
- 2022-11-22 Kotlin?by關鍵字作用及使用介紹_Android
- 2022-06-06 canvas保存圖片時,谷歌瀏覽器Chrome報錯解決方案Not allowed to naviga
- 2022-11-24 Linux學習之expect操作詳解_linux shell
- 2022-11-08 python安裝包出現Retrying?(Retry(total=4,?connect=None,?
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支