小编Leo*_*aus的帖子

描述符的Python docstring

我使用描述符来定义接口类的寄存器:

class Register(object):
    def __init__(self, address, docstring="instance docstring"):
        self.address = address    
        self.__doc__ = docstring

    def __get__(self, obj, objtype):
        return obj.read(self.address)

    def __set__(self, obj, val):
        return obj.write(self.address, val)

class Interface(object):
    r = Register(0x00, docstring="the first register") 
Run Code Online (Sandbox Code Playgroud)

我喜欢ipython的用户能够执行以下操作之一:

i = Interface()
i.r? #should show the docstring "the first register"
Run Code Online (Sandbox Code Playgroud)

要么

i = Interface()
i.r(  #should show the docstring "the first register" when parentheses are opened
Run Code Online (Sandbox Code Playgroud)

但是,docstring始终是obj.read返回的int对象中的文件串,而不是指定的docstring.有没有办法在这种情况下显示正确的文档字符串?

如果我不使用描述符但是手动定义它们,则在括号打开时它会起作用:

class Interface(object):
    @property
    def r(self):
        """this docstring will be shown alternatively"""
        return self.read(0x0)

    @r.setter
    def …
Run Code Online (Sandbox Code Playgroud)

python docstring descriptor ipython python-2.7

7
推荐指数
1
解决办法
325
查看次数

使用 pytest.hookimpl 将 pytest 测试函数返回值写入文件

我正在寻找一种方法来访问测试函数的返回值,以便将该值包含在测试报告文件中(类似于http://doc.pytest.org/en/latest/example/simple.html#post -process-test-reports-failures)。

我想使用的代码示例:

# modified example code from http://doc.pytest.org/en/latest/example/simple.html#post-process-test-reports-failures

import pytest
import os.path

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()

    if rep.when == "call" and rep.passed:
        mode = "a" if os.path.exists("return_values") else "w"
        with open("return_values.txt", mode) as f:
            # THE FOLLOWING LINE IS THE ONE I CANNOT FIGURE OUT
            # HOW DO I ACCESS THE TEST FUNCTION RETURN VALUE?
            return_value = item.return_value
            f.write(rep.nodeid + …
Run Code Online (Sandbox Code Playgroud)

python benchmarking return return-value pytest

3
推荐指数
2
解决办法
3125
查看次数