[ Python ] pysnooper 을 활용한 debug 하기
** 유용하셨다면 공감버튼 한번씩 눌러주세요**
pip install pysnooper
import pysnooper
## 해당 함수 debug 할 때 앞에 error check : 가 붙어서 나온다.
@ pysnooper.snoop (prefix = 'error check : ' )
def plus_with_power (a, b , d) :
a = a * a
b = b * b
b = b + d
c = a + b
return c

command 창에 mkdir를 사용해서 my 폴더 안에 log를 만든다. (아무거나 원하는 이름 하면 된다.)
@ pysnooper.snoop( '/my/log/file.log' )
def plus_with_power (a, b , d) :
a = a * a
b = b * b
b = b + d
c = a + b
return c

## class로 해도 가능하다.
class Plus :
a= 0
b= 0
def __init__ (self , a) :
self.a = a
@ pysnooper.snoop( variables = ('self.a'))
def plus(self, b) :
c = self.a + b
return c
result = Plus(2).plus(3)
print (result)

Error 날 때는 어떻게 표현이 될까?
@pysnooper.snoop()
def hello():
for i in range(10):
d = i * 5
k = d + "error 생겨라"
print(k)

Print를 자동으로 해주기 때문에 군데군데 안넣어줘도 된다! 물론 길어지니 보기가 어려울 수도 있지만 아무튼~~
일단 새로 나온지 얼마 안되서 바로 try 해본다. 해당 패키지 url : https://github.com/cool-RR/PySnooper
cool-RR/PySnooper
Never use print for debugging again. Contribute to cool-RR/PySnooper development by creating an account on GitHub.
github.com
# 참고
https://blog.ikedaosushi.com/entry/2019/04/28/085904
【Python】もうprintデバッグはいらない? / PySnooperで楽々デバッキング - フリーランチ食べたい
Hacker NewsとRedditでバズっていたPythonのデバッグツールが便利だったので紹介です! PySnooperというライブラリです。 Hacker News - PySnooper: Never use print for debugging again Reddit - PySnooper: Never use print for debugging again ※追記 想像以上にたくさんの方に読んでいただき、printデバッグなど他のデバッグ方法との比較について追記として文末に補足しました
blog.ikedaosushi.com