pyyaml 中的默认构造函数参数 [英] Default constructor parameters in pyyaml

查看:50
本文介绍了pyyaml 中的默认构造函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 PyYAML 文档中找到如何执行此操作.我想表示我在 YAML 中定义的 python 类,如果没有在 YAML 中指定,则在构造函数中为参数提供一个默认值.例如:

<预><代码>>>>类测试(yaml.YAMLObject):... yaml_tag = u"!Test"... def __init__(self, foo, bar=3):... self.foo = foo... self.bar = bar... def __repr__(self):... return "%s(foo=%r, bar=%r)" % (self.__class__.__name__, self.foo, self.bar)...>>>yaml.load("""... - - !测试... foo: 5... """)回溯(最近一次调用最后一次):文件<stdin>",第 4 行,在 <module> 中文件<stdin>",第 7 行,在 __repr__ 中AttributeError: 'Test' 对象没有属性 'bar'

我预计它会创建一个带有 bar=3 的 Test 对象,但我猜它在创建对象时会绕过我的构造函数.如果我在 YAML 中包含 bar 的映射,则一切正常:

<预><代码>>>>yaml.load("""... - - !测试... foo: 5...酒吧:42... """)测试(foo=5,bar=42)

有谁知道我如何让它使用默认值?

解决方案

我遇到了同样的问题:yaml_tag 由于某种原因不起作用.所以我使用了替代方法:

导入yamldef 构造函数(加载器,节点):字段 = loader.construct_mapping(节点)返回测试(**字段)yaml.add_constructor('!Test', 构造函数)类测试(对象):def __init__(self, foo, bar=3) :self.foo = fooself.bar = bardef __repr__(self):return "%s(foo=%r, bar=%r)" % (self.__class__.__name__, self.foo, self.bar)打印 yaml.load("""- !Test { foo: 1 }- !Test { foo: 10, bar: 20 }""")

输出:

[测试(foo=1, bar=3), 测试(foo=10, bar=20)]

I haven't been able to find out how to do this in the PyYAML documentation. I want to represent python classes I've defined in YAML, and have a default value given to a parameter in the constructor if it's not specified in the YAML. For example:

>>> class Test(yaml.YAMLObject):
...     yaml_tag = u"!Test"
...     def __init__(self, foo, bar=3):
...             self.foo = foo
...             self.bar = bar
...     def __repr__(self):
...             return "%s(foo=%r, bar=%r)" % (self.__class__.__name__, self.foo, self.bar)
... 
>>> yaml.load("""
... --- !Test
... foo: 5
... """)
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "<stdin>", line 7, in __repr__
AttributeError: 'Test' object has no attribute 'bar'

I expected that it would create a Test object with bar=3, but I guess it bypasses my constructor when it creates the object. If I include a mapping for bar in the YAML, everything works as expected:

>>> yaml.load("""
... --- !Test
... foo: 5
... bar: 42
... """)
Test(foo=5, bar=42)

Does anyone know how I can have it use a default value?

解决方案

I encountered the same problem: yaml_tag doesn't work for some reason. So I used alternative approach:

import yaml

def constructor(loader, node) :
    fields = loader.construct_mapping(node)
    return Test(**fields)

yaml.add_constructor('!Test', constructor)

class Test(object) :
    def __init__(self, foo, bar=3) :
        self.foo = foo
        self.bar = bar
    def __repr__(self):
        return "%s(foo=%r, bar=%r)" % (self.__class__.__name__, self.foo, self.bar)

print yaml.load("""
- !Test { foo: 1 }
- !Test { foo: 10, bar: 20 }""")

Output:

[Test(foo=1, bar=3), Test(foo=10, bar=20)]

这篇关于pyyaml 中的默认构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆