python - 在pyyaml中表示具有相同基类的不同类的实例

标签 python yaml pyyaml

我有一组单元测试,想将每次测试运行的结果存储为 YAML 文件以供进一步分析。以 YAML 格式转储数据以多种方式满足我的需求。但是测试属于不同的花色,结果有不同的父类,这是我的例子:

>>> rz # shorthand for result
[<upstream_suite.fftest.SimpleTest testMethod=test_is_fsType_not_set>,
<upstream_suite.openfolder.UfoOpenTest testMethod=test_is_A>,
<upstream_suite.openfolder.UfoOpenTest testMethod=test_is_A_a_glyph_instance>,
<upstream_suite.openfolder.UfoOpenTest testMethod=test_is_ended_ufo>,
<upstream_suite.openfolder.UfoOpenTest testMethod=test_is_folder>,
<upstream_suite.openfolder.UfoOpenTest testMethod=test_is_fsType_eq_1>,
<upstream_suite.openfolder.UfoOpenTest testMethod=test_it_exists>]
# etc.

每个测试都可以有自己的父类。

>>> type(rz[0]) == type(rz[1])
False

我已经使用 __getstate__ 方法为所有类添加了我自己的基类,但由于某些原因它不起作用:

>>> rz[0].__getstate__()
{'targets': ['upstream'], 'methodDoc': 'Is the OS/2 table fsType set to 0?', 'methodName': 'test_is_fsType_not_set', 'tool': 'FontForge', 'name': 'upstream_suite.fftest'}
>>> yaml.safe_dump(rz[0])
*** RepresenterError: cannot represent an object: test_is_fsType_not_set (upstream_suite.fftest.SimpleTest)

文档说我可以为每个类创建代表,所以我试了一下并写了这段代码:

import yaml
from somepath import MyTestCase

def repr_testcase(dumper, data):
    return dumper.represent_mapping(u'tag:yaml.org,2002:map', {
        'methodDoc': data._testMethodDoc,
        'tool': data.tool,
        'name': data.name,
        'methodName': data._testMethodName,
        'targets': data.targets,
        })

# Register representer 
yaml.SafeDumper.add_representer(MyTestCase, repr_testcase)

同时:

>>> isinstance(rz[0], MyTestCase)
True

即使在那之后 safe_dump 也无法正常工作。但是如果我做了这个把戏,那么它就会起作用,我可以得到结果:

>>> yaml.SafeDumper.add_representer(type(rz[0]), repr_testcase)
>>> print(yaml.safe_dump(rz[0]))
methodDoc: Is the OS/2 table fsType set to 0?
methodName: test_is_fsType_not_set
name: checker.upstream_suite.fontforge_suite.fftest
targets: [upstream]
tool: FontForge

但这意味着我需要记录所有可能出现的情况,嗯……至少,这看起来并不明智。实际上,我真正的 result 是更复杂的字典、从不同地方收集的测试等。

>>> for x in rz: yaml.SafeDumper.add_representer(type(x), repr_testcase)
>>> yaml.safe_dump(rz)
... here is expected result

所以,问题是,我在 __getstate__ 上做错了什么,或者我如何使 pyyaml 与特定基类的所有实例一起工作,只有一个代表注册。

最佳答案

我找到了完成这项工作的方法。简短的回答是,还有另一种注册代表的方法,它称为 add_multi_representer。所以修复是:

# Change this line:
# yaml.SafeDumper.add_representer(MyTestCase, repr_testcase)

# to this line:
yaml.SafeDumper.add_multi_representer(MyTestCase, repr_testcase)

更新。多代表允许实现我想要实现的目标:为基类注册代表。

关于python - 在pyyaml中表示具有相同基类的不同类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19744971/

相关文章:

python - 有没有办法从单个数据列表的表格中形成数据框?

Python - youtube-dl.utils.DownloadError : ERROR: audio conversion failed: file: mp3 invalid argument

python - 在 Python 中使用 concurrent.futures 的多线程不起作用

go - 读取一个通用的 yaml 文件

python - 统计几个范围内的天数

flutter - 为什么没有定义 ChangeNotifierProvider 类?

azure - 在 Azure Dev Ops : How do I view the content of the package location directory $(Build. ArtifactStagingDirectory 上)

python - 当数字包含前导零和是/否值时无法正确解析 YAML

python - 将 SQL 按原样保存到 YAML

python - 是否可以在 YAML 中锚定文字 block ?