Python

Python trace 神器之一

遇到 Exception 但不知道哪邊或是 root cause

發現 stackprinter 真的是很棒

來看看範例

import stackprinter

a = [10, 4, 3, 9, 22, "0"]
try:
    b = [i for i in a if 100 % i == 0]

except Exception as e:
    print(e)
    stackprinter.show()

執行後顯示

unsupported operand type(s) for %: 'int' and 'str'
File "U:/PY/practice/exception.py", line 12, in <module>
    8    # with stackprinter.TracePrinter(style='darkbg2'):
    9    #     b = [i for i in a if 100 % i == 0]
    10   
    11   try:
--> 12       b = [i for i in a if 100 % i == 0]
    13   
    ..................................................
     a = [10, 4, 3, 9, 22, '0', ]
    ..................................................

File "U:/PY/practice/exception.py", line 12, in <listcomp>
    8    # with stackprinter.TracePrinter(style='darkbg2'):
    9    #     b = [i for i in a if 100 % i == 0]
    10   
    11   try:
--> 12       b = [i for i in a if 100 % i == 0]
    13   
    ..................................................
     i = '0'
     a = [10, 4, 3, 9, 22, '0', ]
    ..................................................

TypeError: unsupported operand type(s) for %: 'int' and 'str'

如果 還沒 try except 更推薦用

with stackprinter.TracePrinter(style='darkbg2'):
    something()

結果顯示如下 (雖然有點攏長,但是色彩很棒~)

File "U:/PY/practice\exception.py", line 9, in <module>
    5    
    6    a = [10, 4, 3, 9, 22, "0"]
    7    
    8    with stackprinter.TracePrinter(style='darkbg2'):
--> 9        b = [i for i in a if 100 % i == 0]
    10   
    ..................................................
     a = [10, 4, 3, 9, 22, '0', ]
     stackprinter.TracePrinter = <class 'stackprinter.tracing.TracePrinter'>
    ..................................................

 └──┐
    File "U:/PY/practice\exception.py", line 9, in <listcomp>
        5    
        6    a = [10, 4, 3, 9, 22, "0"]
        7    
        8    with stackprinter.TracePrinter(style='darkbg2'):
    --> 9        b = [i for i in a if 100 % i == 0]
        10   
        ..................................................
         a = [10, 4, 3, 9, 22, '0', ]
         stackprinter.TracePrinter = <class 'stackprinter.tracing.TracePrinter'>
        ..................................................
    
    File "U:/PY/practice\exception.py", line 9, in <listcomp>
        5    
        6    a = [10, 4, 3, 9, 22, "0"]
        7    
        8    with stackprinter.TracePrinter(style='darkbg2'):
    --> 9        b = [i for i in a if 100 % i == 0]
        10   
        ..................................................
         a = [10, 4, 3, 9, 22, '0', ]
         stackprinter.TracePrinter = <class 'stackprinter.tracing.TracePrinter'>
         i = '0'
        ..................................................
    
    TypeError: unsupported operand type(s) for %: 'int' and 'str'
File "U:/PY/practice\exception.py", line 9, in <module>
    5    
    6    a = [10, 4, 3, 9, 22, "0"]
    7    
    8    with stackprinter.TracePrinter(style='darkbg2'):
--> 9        b = [i for i in a if 100 % i == 0]
    10   
    ..................................................
     a = [10, 4, 3, 9, 22, '0', ]
     stackprinter.TracePrinter = <class 'stackprinter.tracing.TracePrinter'>
    ..................................................

File "U:/PY/practice\exception.py", line 9, in <listcomp>
    5    
    6    a = [10, 4, 3, 9, 22, "0"]
    7    
    8    with stackprinter.TracePrinter(style='darkbg2'):
--> 9        b = [i for i in a if 100 % i == 0]
    10   
    ..................................................
     a = [10, 4, 3, 9, 22, '0', ]
     stackprinter.TracePrinter = <class 'stackprinter.tracing.TracePrinter'>
     i = '0'
    ..................................................

TypeError: unsupported operand type(s) for %: 'int' and 'str'
comments powered by Disqus