Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何抑制';无';Python中yaml.dump的输出?_Python_Class_Python 3.x_Data Structures_Yaml - Fatal编程技术网

如何抑制';无';Python中yaml.dump的输出?

如何抑制';无';Python中yaml.dump的输出?,python,class,python-3.x,data-structures,yaml,Python,Class,Python 3.x,Data Structures,Yaml,我有一个(简化的)类,可以打印出它的结构,而不需要重新加载对象(yaml.load) 是否有一种快速方法来抑制None输出(包括设置为None的变量)?也许可以以某种方式使用yaml.representer import yaml class A: def __init__(self): self.a = None self.b = [1,2,3] def __repr__(self): return yaml.dump(sel

我有一个(简化的)类,可以打印出它的结构,而不需要重新加载对象(
yaml.load

是否有一种快速方法来抑制
None
输出(包括设置为
None
的变量)?也许可以以某种方式使用
yaml.representer

import yaml

class A:
    def __init__(self):
        self.a = None
        self.b = [1,2,3]

    def __repr__(self):
        return yaml.dump(self)

A()
输出

!!python/object:__main__.A
a: null
b: [1, 2, 3]
鉴于我需要:

!!python/object:__main__.A
b: [1, 2, 3]

此帖子仍处于活动状态。我寻求简洁/稳健的想法。

以下是一种粗糙的技术,将转储的输出转换为字符串,然后格式化,但您可以用一种方法很好地将其抽象出来:

>>> A()
!!python/object:__main__.A
a: null
b: [1, 2, 3]

>>> test = A()
test_string = str(test)
test_split = test_string.split("\n")

>>> test_split
['!!python/object:__main__.A', 'a: null', 'b: [1, 2, 3]', '']

>>> print test_split[0] + "\n" + test_split[2] + "\n"
!!python/object:__main__.A
b: [1, 2, 3]
您可以将其塞进repr函数中。另一个标准库解决方案,我认为最后一个,祝你好运:

import yaml

class A:
    def __init__(self):
        self.a = None
        self.b = [1,2,3]
        self.c = None
        self.d = 'wheee'

    def __repr__(self):
        string = re.sub(r'.*: null', '',str(yaml.dump(self)))
        string_split = string.split('\n')
        lines = [line for line in string_split if line.strip() != '']
        return '\n'.join(map(str, lines))
在这种情况下,我们仍然可以得到预期的结果:

!!python/object:__main__.A
b: [1, 2, 3]
d: wheee

通过将转储的输出转换为字符串,然后进行格式化,以下是一种粗糙的技术,但您可以在方法中很好地将其抽象出来:

>>> A()
!!python/object:__main__.A
a: null
b: [1, 2, 3]

>>> test = A()
test_string = str(test)
test_split = test_string.split("\n")

>>> test_split
['!!python/object:__main__.A', 'a: null', 'b: [1, 2, 3]', '']

>>> print test_split[0] + "\n" + test_split[2] + "\n"
!!python/object:__main__.A
b: [1, 2, 3]
您可以将其塞进repr函数中。另一个标准库解决方案,我认为最后一个,祝你好运:

import yaml

class A:
    def __init__(self):
        self.a = None
        self.b = [1,2,3]
        self.c = None
        self.d = 'wheee'

    def __repr__(self):
        string = re.sub(r'.*: null', '',str(yaml.dump(self)))
        string_split = string.split('\n')
        lines = [line for line in string_split if line.strip() != '']
        return '\n'.join(map(str, lines))
在这种情况下,我们仍然可以得到预期的结果:

!!python/object:__main__.A
b: [1, 2, 3]
d: wheee

您可以通过子类化
YAMLObject
并定义
to_yaml
类方法来实现这一点。对于您的特定示例:

import yaml

class A(yaml.YAMLObject):
    yaml_tag = u'!A'

    def __init__(self):
        self.a = None
        self.b = [1, 2, 3]

    def __repr__(self):
        return yaml.dump(self)

    @classmethod
    def to_yaml(cls, dumper, data):
        cleaned_data = dict((k, v) for (k, v) in data.__dict__.items() if v is not None)
        return dumper.represent_mapping(cls.yaml_tag, cleaned_data)
上面的类方法跳过任何
None
的值,只给出我们想要的实例值。然后,当我们使用它时,我们会得到我们期望的输出:

>> print yaml.dump(A())
!A
b: [1, 2, 3]
关于子类化YAMLObject的文档:

yaml
自卸车的相关文档:

您可以通过子类化
YAMLObject
并定义
to_yaml
类方法来实现这一点。对于您的特定示例:

import yaml

class A(yaml.YAMLObject):
    yaml_tag = u'!A'

    def __init__(self):
        self.a = None
        self.b = [1, 2, 3]

    def __repr__(self):
        return yaml.dump(self)

    @classmethod
    def to_yaml(cls, dumper, data):
        cleaned_data = dict((k, v) for (k, v) in data.__dict__.items() if v is not None)
        return dumper.represent_mapping(cls.yaml_tag, cleaned_data)
上面的类方法跳过任何
None
的值,只给出我们想要的实例值。然后,当我们使用它时,我们会得到我们期望的输出:

>> print yaml.dump(A())
!A
b: [1, 2, 3]
关于子类化YAMLObject的文档

yaml
自卸车的相关文档:

谢谢。有没有一种快速的方法来自动化它?我提到了将其抽象出来,我已经用一种可能的解决方案更新了代码。谢谢。我确实搞清楚了抽象:)我只是不知道如何设计它,使它对于
yaml.dump
任何复杂的输出都干净而不凌乱。谢谢。有没有一种快速的方法来自动化它?我提到了将其抽象出来,我已经用一种可能的解决方案更新了代码。谢谢。我确实搞清楚了抽象:)我只是不知道如何设计它,使它对于任何复杂的
yaml.dump
输出都干净而不凌乱。如果我的对象继承类
yaml.YAMLObject
,它们会有更大的内存占用吗?性能/内存缺陷的粗略衡量?仅供参考:我可能有数百个子类实例……我不确定子类化该类的内存占用。抱歉。如果我的对象继承类
yaml.YAMLObject
,它们会有更大的内存占用吗?性能/内存缺陷的粗略衡量?仅供参考:我可能有数百个子类实例……我不确定子类化该类的内存占用。很抱歉