Python code debugging artifact: pysnopper

Wang Yibai 2020-11-14 22:33:05
python code debugging artifact pysnopper


I'd like to recommend my own e-book 《PyCharm Chinese guide 》, Put all kinds of PyCharm The efficient use of skills with GIF It's shown in the form of a dynamic graph . If you are interested, you can see its online documentation :
http://pycharm.iswbm.com


For every programmer , Debugging is almost a must .

The code is stuck in the middle of writing , I don't know what the result of this function is ? Debug it and see

The code runs half way and reports an error , What circumstance ? How is it different from what I expected ? Debug it and see

There are many ways to debug , Different debugging methods are suitable for different scenarios and people .

  • If you're a little new to programming , I'm not very proficient in many tools , that print and log Dafa is good
  • If you're here (Win perhaps Mac) Development on computer , that IDE Graphical interface debugging is undoubtedly the most suitable ;
  • If you check on the server BUG, So use PDB Debugging without graphical interface should be the first choice ;
  • If you're going to develop locally , But the project needs to rely on a complex server environment , So we can understand PyCharm Remote debugging

In addition to the above , Today Mingo will introduce you a very useful debugging tool , It can be used in some scenarios , Greatly improve the debugging efficiency , That's it PySnooper, It's in Github It's been received on 13k Of star, Get everybody's unanimous praise .

With this tool , Even xiaomengxin can start without any threshold , From now on with print say goodbye ~

1. Fast installation

Execute the following commands to install PySnooper

$ python3 -m pip install pysnooper
# perhaps
$ conda install -c conda-forge pysnooper
# perhaps
$ yay -S python-pysnooper

2. A simple case

Here's the code , Defined a demo_func Function of , Generate a profile The dictionary variable of , And then update it , Finally back to .

The code itself has no practical significance , But to demonstrate PySnooper Enough already .

import pysnooper
@pysnooper.snoop()
def demo_func():
profile = {}
profile["name"] = " Mingo who wrote the code "
profile["age"] = 27
profile["gender"] = "male"
return profile
def main():
profile = demo_func()
main()

Now I use the terminal command line to run it

[root@iswbm ~]# python3 demo.py
Source path:... demo.py
17:52:49.624943 call 4 def demo_func():
17:52:49.625124 line 5 profile = {}
New var:....... profile = {}
17:52:49.625156 line 6 profile["name"] = " Mingo who wrote the code "
Modified var:.. profile = {'name': ' Mingo who wrote the code '}
17:52:49.625207 line 7 profile["age"] = 27
Modified var:.. profile = {'name': ' Mingo who wrote the code ', 'age': 27}
17:52:49.625254 line 8 profile["gender"] = "male"
Modified var:.. profile = {'name': ' Mingo who wrote the code ', 'age': 27, 'gender': 'male'}
17:52:49.625306 line 10 return profile
17:52:49.625344 return 10 return profile
Return value:.. {'name': ' Mingo who wrote the code ', 'age': 27, 'gender': 'male'}
Elapsed time: 00:00:00.000486

You can see PySnooper Record the whole process of function running , Include :

  • Snippets of code 、 Line number and other information , And when each line of code is called ?
  • How the value of a local variable in a function changes ? When a variable was added , When the variable was modified .
  • What is the return value of the function ?
  • How much time does it take to run the function ?

And as a developer , To get this detailed debugging information , What you need to do is very simple , Just put a hat on the function you want to debug ( Decorator ) -- @pysnooper.snoop() that will do .

3. Detailed use

2.1 Redirect to log file

@pysnooper.snoop() When no parameters are added , The debug information will be output to standard output by default .

For a single debugging can solve BUG , There's no problem with that , But there are some BUG It only appears in specific scenarios , It takes you to put the program in the back and run for a while to reproduce .

In this case , You can redirect the debug information to a log file , It is convenient to trace and check .

@pysnooper.snoop(output='/var/log/debug.log')
def demo_func():
...

2.2 Tracking nonlocal variable values

PySnooper Is a unit of function debugging , By default, it only tracks local variables within the function body , If you want to track global variables , You can give @pysnooper.snoop() add watch Parameters

out = {"foo": "bar"}
@pysnooper.snoop(watch=('out["foo"]'))
def demo_func():
...

In this way ,PySnooper Will be in out["foo"] When the value changes , And print it out

watch Parameters , Receive an iteratable object ( It can be list perhaps tuple), The elements inside are string expressions , What does that mean ? Take a look at the following example

@pysnooper.snoop(watch=('out["foo"]', 'foo.bar', 'self.foo["bar"]'))
def demo_func():
...

and watch Relative ,pysnooper.snoop() You can also receive a function watch_explode, Indicates that all global variables except these parameters are monitored .

@pysnooper.snoop(watch_explode=('foo', 'bar'))
def demo_func():
...

2.3 Set the depth of the trace function

When you use PySnooper When debugging a function , If other functions are called in the function ,PySnooper You won't be foolishly following in .

If you want to continue to track other functions that are called in this function , You can specify depth Parameter to set the trace depth ( If not specified, the default is 1).

@pysnooper.snoop(depth=2)
def demo_func():
...

2.4 Set the prefix of debug log

When you are using PySnooper When tracking multiple functions , The debug log will look messy , Inconvenient to view .

under these circumstances ,PySnooper A parameter is provided , It is convenient for you to set different flags for different functions , It is convenient for you to distinguish when you view the log .

@pysnooper.snoop(output="/var/log/debug.log", prefix="demo_func: ")
def demo_func():
...

The effect is as follows

2.5 Set the maximum output length

By default ,PySnooper Output variables and exception information , If exceeded 100 Characters , Will be truncated to 100 Characters .

Of course, you can also specify parameters by Make changes

@pysnooper.snoop(max_variable_length=200)
def demo_func():
...

You can also use the max_variable_length=None It never cuts them off .

@pysnooper.snoop(max_variable_length=None)
def demo_func():
...

2.6 Support multithreading debugging mode

PySnooper It also supports multithreading debugging , By setting parameters thread_info=True, It will print out in the log which thread the variable was modified .

@pysnooper.snoop(thread_info=True)
def demo_func():
...

The effect is as follows

2.7 Custom object format output

pysnooper.snoop() The function has an argument of custom_repr, It receives a tuple object .

In this tuple , You can specify specific types of objects to output in a specific format .

Here's an example .

If I want to follow person This Person Object of type , Because it's not conventional Python The base type ,PySnooper It can't output its information normally .

So I am pysnooper.snoop() Function is set to custom_repr Parameters , The first element of the parameter is Person, The second element is print_persion_obj function .

PySnooper When printing debugging information for an object , We will judge whether it is Person Object of type , if , Just pass in the object print_persion_obj Function , It's up to this function to decide how to display the information of this object .

class Person:pass
def print_person_obj(obj):
return f"<Person {obj.name} {obj.age} {obj.gender}>"
@pysnooper.snoop(custom_repr=(Person, print_person_obj))
def demo_func():
...

The complete code is as follows

import pysnooper
class Person:pass
def print_person_obj(obj):
return f"<Person {obj.name} {obj.age} {obj.gender}>"
@pysnooper.snoop(custom_repr=(Person, print_person_obj))
def demo_func():
person = Person()
person.name = " Mingo who wrote the code "
person.age = 27
person.gender = "male"
return person
def main():
profile = demo_func()
main()

Run it , Observe the effect .

If you want to customize the format, there are many types of output , that custom_repr The value of the parameter can be written as follows

@pysnooper.snoop(custom_repr=((Person, print_person_obj), (numpy.ndarray, print_ndarray)))
def demo_func():
...

There's one more thing I'd like to remind you of , The first element of a tuple can be a type ( Such as class name Person Or other basic types list etc. ), It can also be a function to determine the type of object .

in other words , The following three expressions are equivalent .

# 【 The first way to write it 】
@pysnooper.snoop(custom_repr=(Person, print_persion_obj))
def demo_func():
...
# 【 The second way 】
def is_persion_obj(obj):
return isinstance(obj, Person)
@pysnooper.snoop(custom_repr=(is_persion_obj, print_persion_obj))
def demo_func():
...
# 【 The third way 】
@pysnooper.snoop(custom_repr=(lambda obj: isinstance(obj, Person), print_persion_obj))
def demo_func():
...

The above is a debugging artifact introduced by Mingge today (PySnooper) A detailed user manual for , Don't you think it's good ?

版权声明
本文为[Wang Yibai]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database