美文网首页
PySnooper Python 打印函数执行流程模块

PySnooper Python 打印函数执行流程模块

作者: 河码匠 | 来源:发表于2019-10-22 11:17 被阅读0次

    PySnooper 可以打印出 Python 执行代码的全流程和每行代码的执行时间,以及执行过程中变量的变化。

    安装 PySnooper

    $ pip install pysnooper
    

    简单示例

    import pysnooper
    
    @pysnooper.snoop()
    def text(number):
        new_list = []
        for i in range(number):
            new_list.append(i * 2)
        return new_list
    text(6)
    

    效果

    Source path:... text.py
    Starting var:.. number = 6
    10:45:31.237191 call         5 def text(number):
    10:45:31.237644 line         6     new_list = []
    New var:....... new_list = []
    10:45:31.237732 line         7     for i in range(number):
    New var:....... i = 0
    10:45:31.237849 line         8         new_list.append(i * 2)
    Modified var:.. new_list = [0]
    10:45:31.237941 line         7     for i in range(number):
    Modified var:.. i = 1
    10:45:31.238024 line         8         new_list.append(i * 2)
    Modified var:.. new_list = [0, 2]
    10:45:31.238088 line         7     for i in range(number):
    Modified var:.. i = 2
    10:45:31.238157 line         8         new_list.append(i * 2)
    Modified var:.. new_list = [0, 2, 4]
    10:45:31.238229 line         7     for i in range(number):
    Modified var:.. i = 3
    10:45:31.238303 line         8         new_list.append(i * 2)
    Modified var:.. new_list = [0, 2, 4, 6]
    10:45:31.238364 line         7     for i in range(number):
    Modified var:.. i = 4
    10:45:31.238432 line         8         new_list.append(i * 2)
    Modified var:.. new_list = [0, 2, 4, 6, 8]
    10:45:31.238500 line         7     for i in range(number):
    Modified var:.. i = 5
    10:45:31.238570 line         8         new_list.append(i * 2)
    Modified var:.. new_list = [0, 2, 4, 6, 8, 10]
    10:45:31.238640 line         7     for i in range(number):
    10:45:31.238713 line         9     return new_list
    10:45:31.238767 return       9     return new_list
    Return value:.. [0, 2, 4, 6, 8, 10]
    

    可以很明显看到每次循环的操作和变量的变化。

    其他操作

    • 把输出内容写入文件
    @pysnooper.snoop('/my/log/file.log')
    
    • 显示函数调用的其他函数的执行过程
    @pysnooper.snoop(depth=2)
    

    示例

    import pysnooper
    
    class Text():
        @pysnooper.snoop(depth=2)
        def a(self, param):
            return self.b(param)
    
        def b(self, param):
            new_list = []
            for i in range(param):
                new_list.append(i * 2)
    
    Text().a(2)
    
    • 给输出的行加前缀
    @pysnooper.snoop(prefix='aaa ')
    

    示例

    import pysnooper
    
    class Text():
        @pysnooper.snoop(prefix='aaa ')
        @pysnooper.snoop(depth=2)
        def a(self, param):
            return self.b(param)
    
        def b(self, param):
            new_list = []
            for i in range(param):
                new_list.append(i * 2)
    
    Text().a(2)
    

    结果
    \color{red}{注意}:这里的 pysnooper.snoop 顺序对结果有影响。

    aaa Source path:... /Users/machao/Documents/PythonVirtual/SpeedyCloud/lib/python2.7/site-packages/pysnooper/tracer.py
    aaa Starting var:.. args = (<__main__.Text instance at 0x101da2bd8>, 2)
    aaa Starting var:.. kwargs = {}
    aaa Starting var:.. function = <function a at 0x101da96e0>
    aaa Starting var:.. self = <pysnooper.tracer.Tracer instance at 0x101da2b48>
    aaa 11:15:53.951363 call       254         def simple_wrapper(*args, **kwargs):
    aaa 11:15:53.954333 line       255             with self:
    aaa 11:15:53.954468 line       256                 return function(*args, **kwargs)
        Source path:... /Users/machao/Desktop/licens.py
        Starting var:.. self = <__main__.Text instance at 0x101da2bd8>
        Starting var:.. param = 2
        11:15:53.954581 call         6     def a(self, param):
        11:15:53.954993 line         7         return self.b(param)
            Starting var:.. self = <__main__.Text instance at 0x101da2bd8>
            Starting var:.. param = 2
            11:15:53.955131 call         9     def b(self, param):
            11:15:53.955316 line        10         new_list = []
            New var:....... new_list = []
            11:15:53.955506 line        11         for i in range(param):
            New var:....... i = 0
            11:15:53.955675 line        12             new_list.append(i * 2)
            Modified var:.. new_list = [0]
            11:15:53.955818 line        11         for i in range(param):
            Modified var:.. i = 1
            11:15:53.956045 line        12             new_list.append(i * 2)
            Modified var:.. new_list = [0, 2]
            11:15:53.956168 line        11         for i in range(param):
            11:15:53.956281 return      11         for i in range(param):
            Return value:.. None
        11:15:53.956454 return       7         return self.b(param)
        Return value:.. None
    aaa Call ended by exception
    
    • 在执行线程的函数时打印内容
    @pysnooper.snoop(thread_info=True)
    
    • 控制最长输出
    @pysnooper.snoop(max_variable_length=200)
    

    相关文章

      网友评论

          本文标题:PySnooper Python 打印函数执行流程模块

          本文链接:https://www.haomeiwen.com/subject/jhzfvctx.html