<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          有了PySnooper,再也不用靠一堆print來調(diào)試代碼了

          共 5093字,需瀏覽 11分鐘

           ·

          2021-05-11 07:29


          大家好,歡迎來到 Crossin的編程教室 !

          當(dāng)我們的Python代碼出現(xiàn)報(bào)錯(cuò)或者沒有得到預(yù)期的執(zhí)行效果時(shí),通常會使用具有斷點(diǎn)功能的Debug工具,或者借助“print 大法”輸出調(diào)試。但是,Debug工具有時(shí)不那么好使,print太多又很麻煩。

          今天,我們就給大家分享另一個(gè)不錯(cuò)的調(diào)試選擇:PySnooper

          PySnooper 是一個(gè)非常方便的調(diào)試器。有了它之后,就能輕松完成對整個(gè)代碼的調(diào)試分析。它能告訴你哪些代碼正在運(yùn)行,以及局部變量的值是什么。

          PySnooper 的實(shí)現(xiàn)原理其實(shí)挺簡單,就是替代了一行一行print的重復(fù)性工作,給你的代碼一個(gè)pysnooper裝飾器,它能自動(dòng)識別到語句和變量并將其值print出來:

          import pysnooper

          @pysnooper.snoop()
          def number_to_bits(number):
              if number:
                  bits = []
                  while number:
                      number, remainder = divmod(number, 2)
                      bits.insert(0, remainder)
                  return bits
              else:
                  return [0]

          number_to_bits(6)


          效果如下:


          Source path:... 1.py
          Starting var:.. number = 6
          23:03:35.990701 call         4 def number_to_bits(number):
          23:03:35.991699 line 5     if number:
          23:03:35.991699 line 6         bits = []
          New var:....... bits = []
          23:03:35.991699 line 7         while number:
          23:03:35.991699 line 8             number, remainder = divmod(number, 2)
          Modified var:.. number = 3
          New var:....... remainder = 0
          23:03:35.991699 line 9             bits.insert(0, remainder)
          Modified var:.. bits = [0]
          23:03:36.004664 line 7         while number:
          23:03:36.005661 line 8             number, remainder = divmod(number, 2)
          Modified var:.. number = 1
          Modified var:.. remainder = 1
          23:03:36.005661 line 9             bits.insert(0, remainder)
          Modified var:.. bits = [1, 0]
          23:03:36.007657 line 7         while number:
          23:03:36.007657 line 8             number, remainder = divmod(number, 2)
          Modified var:.. number = 0
          23:03:36.008655 line 9             bits.insert(0, remainder)
          Modified var:.. bits = [1, 1, 0]
          23:03:36.008655 line 7         while number:
          23:03:36.009651 line 10         return bits
          23:03:36.009651 return      10         return bits
          Return value:.. [1, 1, 0]
          Elapsed time: 00:00:00.020945


          可以看到,它將每一行變量的值都輸出到屏幕上,方便你調(diào)試代碼。

          僅僅需要寫一行代碼—使用裝飾器就可以實(shí)現(xiàn)這個(gè)方便的調(diào)試功能,比起一行行寫print,這可方便多了。

          0.安裝模塊

          使用這個(gè)模塊,你只需要使用Pip安裝PySnooper:

          pip install pysnooper


          接下來講講這個(gè)模塊其他好用的功能:

          1. 支持日志文件

          如果你覺得print到屏幕上不方便,還可以將其輸出到log文件中,你只需要將裝飾器那一行改為:

          @pysnooper.snoop('/my/log/file.log')


          2. 讀取局外變量或其他表達(dá)式

          如果你想讀取在裝飾器作用范圍以外的變量或者表達(dá)式的值,還可以使用watch參數(shù):

          @pysnooper.snoop(watch=('foo.bar', 'self.x["whatever"]'))


          3. 如果你不想用裝飾器,也可以用上下文的形式調(diào)試

          沒錯(cuò),裝飾器有限定的使用條件,使用起來比較局限,因此pysnooper還支持使用 with 的上下文形式:

          import pysnooper
          import random

          def foo():
              lst = []
              for i in range(10):
                  lst.append(random.randrange(1, 1000))

              with pysnooper.snoop():
                  lower = min(lst)
                  upper = max(lst)
                  mid = (lower + upper) / 2
                  print(lower, mid, upper)

          foo()


          效果如下,只有上下文里的代碼才會被調(diào)試出來:


          New var:....... i = 9
          New var:....... lst = [681, 267, 74, 832, 284, 678, ...]
          09:37:35.881721 line 10         lower = min(lst)
          New var:....... lower = 74
          09:37:35.882137 line 11         upper = max(lst)
          New var:....... upper = 832
          09:37:35.882304 line 12         mid = (lower + upper) / 2
          74 453.0 832
          New var:....... mid = 453.0
          09:37:35.882486 line 13         print(lower, mid, upper)
          Elapsed time: 00:00:00.000344


          當(dāng)我們只需要調(diào)試部分代碼的時(shí)候,這個(gè)上下文形式的調(diào)試方法非常方便。

          此外,PySnooper還有許多更強(qiáng)大的用法,大家可以看他們的高級使用文檔:

          https://github.com/cool-RR/PySnooper/blob/master/ADVANCED_USAGE.md

          關(guān)于 PySnooper 今天就介紹到這里

          如果文章對你有幫助,歡迎轉(zhuǎn)發(fā)/點(diǎn)贊/收藏~

          作者:Ckend

          來源:Python實(shí)用寶典


          _往期文章推薦_

          開發(fā)5分鐘,調(diào)試2小時(shí)-你的問題在哪里?




          如需了解付費(fèi)精品課程教學(xué)答疑服務(wù)
          請?jiān)?strong style="max-width: 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;">Crossin的編程教室內(nèi)回復(fù): 666

          瀏覽 37
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  精品国产久久 | 亚洲性情| 亚洲色图第十页 | 高清无码在线观看视频 | 99热这里只有免费精品 |