我使用描述符来定义接口类的寄存器:
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) 我正在寻找一种方法来访问测试函数的返回值,以便将该值包含在测试报告文件中(类似于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 ×2
benchmarking ×1
descriptor ×1
docstring ×1
ipython ×1
pytest ×1
python-2.7 ×1
return ×1
return-value ×1