精华内容
下载资源
问答
  • PySnooper

    2019-04-24 09:23:00
    pip install pysnooper 使用时,只需要在每个函数前面添加装饰器即可,比如下面的代码是 Sklearn 和 Tensorflow 机器学习实用指南(hands-on-ml-with-sklearn-and-tensorflow)里面第一章的一个数据处理函数 ...

    GitHub上刚开源的一个项目,用来寻找 python 代码中的错误。由于平时写的代码行数量少,个人不太喜欢用那种庞大(不轻量)的调试工具,像 pycharm 之类的。以前习惯于用 print 函数打印变量查看状态来找错误。如今这个工具完美适用于我这样的人。个人认为,这个工具非常适合持续服务或者大数据集任务。

    这个工具使用起来很简单,首先直接可以使用 pip 安装

    pip install pysnooper

    使用时,只需要在每个函数前面添加装饰器即可,比如下面的代码是 Sklearn 和 Tensorflow 机器学习实用指南(hands-on-ml-with-sklearn-and-tensorflow)里面第一章的一个数据处理函数

    @pysnooper.snoop('file.log', prefix='prepare_country_stats_debug: ')
    def prepare_country_stats(oecd_bli, gdp_per_capita):
        oecd_bli = oecd_bli[oecd_bli["INEQUALITY"]=="TOT"]
        oecd_bli = oecd_bli.pivot(index="Country", columns="Indicator", values="Value")
        gdp_per_capita.rename(columns={"2015": "GDP per capita"}, inplace=True)
        gdp_per_capita.set_index("Country", inplace=True)
        full_country_stats = pd.merge(left=oecd_bli, right=gdp_per_capita,
                                      left_index=True, right_index=True)
        full_country_stats.sort_values(by="GDP per capita", inplace=True)
        remove_indices = [0, 1, 6, 8, 33, 34, 35]
        keep_indices = list(set(range(36)) - set(remove_indices))
        return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices]

    函数返回日志输出到 file.log 文件中了,其内容如下,prepare_country_stats_debug 是为了便于辨识一个名字。可以看出,函数变量的值以及变化都会展示出来。

    prepare_country_stats_debug: Starting var:.. oecd_bli =      LOCATION          Country INDICATOR  ...  Value Flag Codes            Flags
    0         AUS   ...
    prepare_country_stats_debug: Starting var:.. gdp_per_capita =                                                Country  ... Estimates Start After
    0              ...
    prepare_country_stats_debug: 08:58:25.811795 call         9 @pysnooper.snoop('file.log', prefix='prepare_country_stats_debug: ')
    prepare_country_stats_debug: 08:58:25.877757 line        11     oecd_bli = oecd_bli[oecd_bli["INEQUALITY"]=="TOT"]
    prepare_country_stats_debug: 08:58:25.951715 line        12     oecd_bli = oecd_bli.pivot(index="Country", columns="Indicator", values="Value")
    prepare_country_stats_debug: Modified var:.. oecd_bli = Indicator        Air pollution  Assault rate  ...  Water quality  Years in education
    Country     ...
    prepare_country_stats_debug: 08:58:26.045661 line        13     gdp_per_capita.rename(columns={"2015": "GDP per capita"}, inplace=True)
    prepare_country_stats_debug: 08:58:26.123617 line        14     gdp_per_capita.set_index("Country", inplace=True)
    prepare_country_stats_debug: Modified var:.. gdp_per_capita =                                                                                    Subject Descri...
    prepare_country_stats_debug: 08:58:26.204192 line        15     full_country_stats = pd.merge(left=oecd_bli, right=gdp_per_capita,
    prepare_country_stats_debug: 08:58:26.281148 line        16                                   left_index=True, right_index=True)
    prepare_country_stats_debug: New var:....... full_country_stats =                  Air pollution  Assault rate  ...  GDP per capita  Estimates Start After
    Country ...
    prepare_country_stats_debug: 08:58:26.428064 line        17     full_country_stats.sort_values(by="GDP per capita", inplace=True)
    prepare_country_stats_debug: 08:58:26.574980 line        18     remove_indices = [0, 1, 6, 8, 33, 34, 35]
    prepare_country_stats_debug: New var:....... remove_indices = [0, 1, 6, 8, 33, 34, 35]
    prepare_country_stats_debug: 08:58:26.716899 line        19     keep_indices = list(set(range(36)) - set(remove_indices))
    prepare_country_stats_debug: New var:....... keep_indices = [2, 3, 4, 5, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29...
    prepare_country_stats_debug: 08:58:26.863815 line        20     return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices]
    prepare_country_stats_debug: 08:58:27.011381 return      20     return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices]

    PySnooper 参数

    如上面的示例,完全可以将日志输出到一个文件中,便于查看

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

    也可以查看非本地变量

    @pysnooper.snoop(variables=('foo.bar', 'self.whatever'))

    显示函数中调用函数的snoop行

    @pysnooper.snoop(depth=2)

    给调试的行加个前缀名便于辨识和定位

    @pysnooper.snoop(prefix='prepare_country_stats_debug: ')

    转载于:https://www.cnblogs.com/shixp/p/10760512.html

    展开全文
  • PySnooper - Never use print for debugging again PySnooper is a poor man's debugger. You're trying to figure out why your Python code isn't doing what you think it should be doing. You'd love to use a ...
  • python pysnooper使用

    2020-12-23 16:32:55
    pysnooper pysnooper下载 pip install pysnooper -i https://mirrors.ustc.edu.cn/pypi/web/simple/ import pysnooper 跟踪整个函数 @pysnooper.snoop() 跟踪相关部分 def … … with pysnooper.snoop(): XXX 1.将...

    pysnooper

    pysnooper下载

    pip install pysnooper -i https://mirrors.ustc.edu.cn/pypi/web/simple/

    import pysnooper

    跟踪整个函数

    @pysnooper.snoop()

    跟踪相关部分

    def …

    with pysnooper.snoop():
    XXX

    1.将输出重定向到文件

    @pysnooper.snoop(“simple_test.log”)

    2.跟踪全局变量

    @pysnooper.snoop(watch=“变量名”)

    3.设置调试日志的前缀

    @pysnooper.snoop(prefix=“XXXX”)

    4.支持多线程调试模式

    @pysnooper.snoop(thread_info=True)

    5.获得程序执行的时间

    @pysnooper.snoop(relative_time=True)

    6.不显示和环境相关的信息

    @pysnooper.snoop(normalize=True)

    整行隐藏
    this.tableData = tableData.filter(v => v.leftNum !== 0)

    展开全文
  • pysnooper安装使用

    2020-02-28 13:15:13
    安装 ...$ pip install pysnooper $ conda install pysnooper 使用 整个函数 import pysnooper @pysnooper.snoop() def number_to_bits(number): if number: bits = [] while number: ...
    • 安装
    $ pip install pysnooper
    $ conda install pysnooper
    
    • 使用
    1. 整个函数
    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:... /my_code/foo.py
    Starting var:.. number = 6
    15:29:11.327032 call         4 def number_to_bits(number):
    15:29:11.327032 line         5     if number:
    15:29:11.327032 line         6         bits = []
    New var:....... bits = []
    15:29:11.327032 line         7         while number:
    15:29:11.327032 line         8             number, remainder = divmod(number, 2)
    New var:....... remainder = 0
    Modified var:.. number = 3
    15:29:11.327032 line         9             bits.insert(0, remainder)
    Modified var:.. bits = [0]
    15:29:11.327032 line         7         while number:
    15:29:11.327032 line         8             number, remainder = divmod(number, 2)
    Modified var:.. number = 1
    Modified var:.. remainder = 1
    15:29:11.327032 line         9             bits.insert(0, remainder)
    Modified var:.. bits = [1, 0]
    15:29:11.327032 line         7         while number:
    15:29:11.327032 line         8             number, remainder = divmod(number, 2)
    Modified var:.. number = 0
    15:29:11.327032 line         9             bits.insert(0, remainder)
    Modified var:.. bits = [1, 1, 0]
    15:29:11.327032 line         7         while number:
    15:29:11.327032 line        10         return bits
    15:29:11.327032 return      10         return bits
    Return value:.. [1, 1, 0]
    
    1. 部分代码
    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()
    

    运行结果

    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)
    

    github地址

    展开全文
  • pysnooper的使用

    2019-06-17 16:41:13
    pysnooper的基本使用 pysnooper主要是使用装饰器的形式进行函数的处理; ''' When you decorate a function with `@pysnooper.snoop()` or wrap a block of code in `with pysnooper.snoop():`, you'll get a ...

    pysnooper的基本使用
    pysnooper主要是使用装饰器的形式进行函数的处理;

        '''
        When you decorate a function with `@pysnooper.snoop()`
        or wrap a block of code in `with pysnooper.snoop():`, you'll get a log of
        every line that ran in the function and a play-by-play of every local
        variable that changed.
    	当您使用“@pysnoop .snoop()”装饰函数时或者在' with pysnoop .snoop(): '中封装一个代码块,
    	您将得到一个日志函数中运行的每一行以及每个局部的逐行运行变量发生了变化。
        If stderr is not easily accessible for you, you can redirect the output to
        a file::可以将输出重定向到一个文件:
            @pysnooper.snoop('/my/log/file.log')
        See values of some expressions that aren't local variables::
        查看一些非局部变量表达式的值:
            @pysnooper.snoop(watch=('foo.bar', 'self.x["whatever"]'))
        Expand values to see all their attributes or items of lists/dictionaries:
            @pysnooper.snoop(watch_explode=('foo', 'self'))
        (see Advanced Usage in the README for more control)
        Show snoop lines for functions that your function calls::
        显示函数调用的函数的snoop行::
            @pysnooper.snoop(depth=2)
        Start all snoop lines with a prefix, to grep for them easily::
        添加前缀,方便进行多个函数之间的区别:
            @pysnooper.snoop(prefix='ZZZ ')
        On multi-threaded apps identify which thread are snooped in output::
            @pysnooper.snoop(thread_info=True)
        Customize how values are represented as strings::
            @pysnooper.snoop(custom_repr=((type1, custom_repr_func1), (condition2, custom_repr_func2), ...))
    
        '''
    

    @pysnoop .snoop()

    @pysnooper.snoop()
    def number_to_bits_1(number):
        if number:
            bits = []
            while number:
                number, remainder = divmod(number, 2)
                bits.insert(0, remainder)
            return bits
        else:
            return [0]
    number_to_bits_1(6)  
    -------------------------------------------------------------
    Starting var:.. number = 6
    16:33:41.396554 call        12 def number_to_bits_1(number):
    16:33:41.397055 line        13     if number:
    16:33:41.397055 line        14         bits = []
    New var:....... bits = []
    16:33:41.397055 line        15         while number:
    16:33:41.397055 line        16             number, remainder = divmod(number, 2)
    Modified var:.. number = 3
    New var:....... remainder = 0
    16:33:41.397055 line        17             bits.insert(0, remainder)
    Modified var:.. bits = [0]
    16:33:41.397055 line        15         while number:
    16:33:41.397055 line        16             number, remainder = divmod(number, 2)
    Modified var:.. number = 1
    Modified var:.. remainder = 1
    16:33:41.397055 line        17             bits.insert(0, remainder)
    Modified var:.. bits = [1, 0]
    16:33:41.397055 line        15         while number:
    16:33:41.397055 line        16             number, remainder = divmod(number, 2)
    Modified var:.. number = 0
    16:33:41.397556 line        17             bits.insert(0, remainder)
    Modified var:.. bits = [1, 1, 0]
    16:33:41.397556 line        15         while number:
    16:33:41.397556 line        18         return bits
    16:33:41.397556 return      18         return bits
    Return value:.. [1, 1, 0]
    

    ** @pysnooper.snoop(’/logs/file.log’)**

    @pysnooper.snoop('/logs/file.log')
    def number_to_bits_2(number):
        if number:
            bits = []
            while number:
                number, remainder = divmod(number, 2)
                bits.insert(0, remainder)
            return bits
        else:
            return [0]
    number_to_bits_2(6)
    

    @pysnooper.snoop(prefix='number_to_bits_3 ') 给调试的行加个前缀名便于辨识和定位

    @pysnooper.snoop(prefix='number_to_bits_3: ')
    def number_to_bits_3(number):
        if number:
            bits = []
            while number:
                number, remainder = divmod(number, 2)
                bits.insert(0, remainder)
            return bits
        else:
            return [0]
    number_to_bits_3(6) 
    --------------------------------------------------------
    number_to_bits_3: Starting var:.. number = 6
    number_to_bits_3: 16:33:41.403571 call        36 def number_to_bits_3(number):
    number_to_bits_3: 16:33:41.403571 line        37     if number:
    number_to_bits_3: 16:33:41.404073 line        38         bits = []
    number_to_bits_3: New var:....... bits = []
    number_to_bits_3: 16:33:41.404073 line        39         while number:
    number_to_bits_3: 16:33:41.404073 line        40             number, remainder = divmod(number, 2)
    number_to_bits_3: Modified var:.. number = 3
    number_to_bits_3: New var:....... remainder = 0
    number_to_bits_3: 16:33:41.404073 line        41             bits.insert(0, remainder)
    number_to_bits_3: Modified var:.. bits = [0]
    number_to_bits_3: 16:33:41.404073 line        39         while number:
    number_to_bits_3: 16:33:41.404073 line        40             number, remainder = divmod(number, 2)
    number_to_bits_3: Modified var:.. number = 1
    number_to_bits_3: Modified var:.. remainder = 1
    number_to_bits_3: 16:33:41.404073 line        41             bits.insert(0, remainder)
    number_to_bits_3: Modified var:.. bits = [1, 0]
    number_to_bits_3: 16:33:41.404073 line        39         while number:
    number_to_bits_3: 16:33:41.404073 line        40             number, remainder = divmod(number, 2)
    number_to_bits_3: Modified var:.. number = 0
    number_to_bits_3: 16:33:41.404073 line        41             bits.insert(0, remainder)
    number_to_bits_3: Modified var:.. bits = [1, 1, 0]
    number_to_bits_3: 16:33:41.404073 line        39         while number:
    number_to_bits_3: 16:33:41.404073 line        42         return bits
    number_to_bits_3: 16:33:41.404073 return      42         return bits
    number_to_bits_3: Return value:.. [1, 1, 0]
    
    展开全文
  • 相信很多小伙伴平时写python的时候都是需要调试程序的,出问题了,需要了解函数内部是怎么跑的,而这个时候很多人都会想到在疑惑的地方使用...下面简单说一下PySnooper的用法: 这个工具使用起来很简单,首先直接可以
  • Debug PyTorch code using PySnooper
  • Python调试神器PySnooper

    2019-07-01 22:45:06
    解放print(),最专业的模块做最专业的事,PySnooper一款牛逼的模块,话不多说直接上代码: 1.安装 pip install pysnooper 或者 conda install -c conda-forge pysnooper 2.测试 import pysnooper @...
  • python调试利器pysnooper

    2019-11-07 14:51:01
    安装 安装方法很简单,可以使用 pip 或 anaconda 两种安装方法: ...一个使用例子如下,首先是需要导入 pysnooper ,然后想知道函数 number_to_bits() 的运行情况,只需要添加 @pysnooper.snoop() ...
  • Python调试神器 PySnooper

    2020-10-28 15:00:43
    pip install pysnooper 使用 import pysnooper @pysnooper.snoop() def test(n): if n<2: return 1 return (test(n-1)) * n test(5) # 详细结果如下 每一行的操作 变量的变化 以及缩进 都清清楚楚 Source ...
  • PySnooper 在 GitHub 上自嘲是一个“乞丐版”调试工具(poor man’s debugger)。 一般情况下,在编写 Python 代码时,如果想弄清楚为什么 Python 代码没有按照预期执行、哪些代码在运行哪些没在运行、局部变量又是...
  • DeBug Python 工具PySnooper

    2019-04-27 19:03:11
    #pip install pysnooper pip3 install pysnooper Test import pysnooper #这句放在要测试的函数前,乱放会报错 @pysnooper.snoop() def print_test(n): for i in range(n): print('now is:' ,...
  • Python模块之pysnooper

    2019-04-25 17:22:00
    调试程序时,很多人喜欢直接用print来代替断点调试,而pysnooper模块比print更方便,以装饰器的形式存在 二、实验环境 操作系统:win10 python版本:python3.6 三、安装pysnooper模块 pip3 install ...
  • debug调试神器pysnooper

    2019-07-31 09:59:56
    异常bug定位,print()函数也可以,但效率上还是慢,后来发现了一个叫PySnooper的装饰器。一般debug调试,都是在我们可能觉得会有问题的地方,去打印输出,看下实际输出了什么,然后思考问题所在。 下载库: pip ...
  • PySnooper教程 引子 作为一名高冷酷且帅的程序开发者,注意,一定要读懂这个帅字…;debug调试的使用频率非常之高,通过debug能找到我们的报错位置,下面让我们来告别传统的断点和print(),提供给大家一个及其简易的...
  • pip install pysnooper import numpy as np import pysnooper @pysnooper.snoop() def one(number): mat = [] while number: mat.append(np.random.normal(0, 1)) number -= 1 return mat one(3) @pysnooper...
  • PySnooper 不使用print调试Python程序 https://github.com/cool-RR/pysnooper
  • PySnooper-永远不要使用print进行调试 PySnooper是一个穷人的调试器。 如果您使用过Bash,则类似于Bash的set -x ,只是它比较高级。 您的故事:您正在试图弄清楚为什么您的Python代码没有按照您的预期去做。 您可能...
  • python debug工具PySnooper

    2019-04-26 17:19:20
    DeBug Python代码还靠print函数?换用这个一天2K+Star的工具吧 项目地址:https://github.com/cool-RR/pysnooper
  • pip install pysnooper 这个包会将中间的执行过程打印出来 代码: import tempfile import requests from PyPDF2 import PdfFileReader import pysnooper @pysnooper.snoop() # 在要Debug的函数前加个装饰器 def ...
  • pysnooper 安装 pip3 install pysnooper 使用 import pyspooner 在要调用的函数前加上 @pysnooper.snoop() PySnooper还提供了参数prefix给debug信息添加前缀的方式便于识别 @pysnooper.snoop(prefix="funcTwo ") ...
  • python debug(PySnooper

    2020-07-23 15:25:09
    不同于其他编程语言,python是没有编译器的,debug全靠print。 两种解决方法: ...②专为python debug服务的简约工具:pysnooper 想要检查哪里,加上装饰器@pysnooper.snoop()到函数前即可。 ...
  • Pysnooper recipe

    2020-12-25 19:52:58
    <p>Checklist <ul><li>[x] License file is packaged (see <a href="https://github.com/conda-forge/staged-recipes/blob/master/recipes/example/meta.yaml#L57-L66">here</a> for an example)</li><li>[x] ...
  • DeBug Python神级工具PySnooper

    千次阅读 2019-04-24 16:53:25
    GitHub项目地址 安装 pip3 install pysnooper import pysnooper @pysnooper.snoop() def number_to_bits(number): if number: bits = [] while number: number, remainder = divmod(num...
  • 软硬件环境windows 10 64bitanaconda3 with python 3.7.4pysnooperpysnooper简介PySnooper是一个用于代码调试的一个第三方库...
  • 后台回复1024,解锁无限快乐!传统的Python调试方式1、打断点2、Print函数3、raise for...什么是PySnooper一般情况下,想要知道哪一行代码在运行、哪一行不运...
  • ''' Pyhon 调试模块pysnooper用法演示 by 郑瑞国 ''' import pysnooper y = 0 @pysnooper.snoop() def abc(): global y for x in range(101): y = y+x print(y) print(y) abc() ...
  • PySnooper 使用起来十分简单,开发者可以在任何庞大的代码库中使用它,而无需进行任何设置。你只需添加装饰器,并为日志输出地址指定路径,方法是将其路径指定为第一个参数。

空空如也

空空如也

1 2 3 4 5 ... 13
收藏数 250
精华内容 100
关键字:

pysnooper