pyyaml

Default constructor parameters in pyyaml

試著忘記壹切 提交于 2019-11-29 17:45:23
问题 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

Specifying styles for portions of a PyYAML dump

送分小仙女□ 提交于 2019-11-29 13:50:56
I'm using YAML for a computer and human-editable and readable input format for a simulator. For human readability, some parts of the input are mostly amenable to block style, while flow style suits others better. The default for PyYAML is to use block style wherever there are nested maps or sequences, and flow style everywhere else. *default_flow_style* allows one to choose all-flow-style or all-block-style. But I'd like to output files more of the form bonds: - { strength: 2.0 } - ... tiles: - { color: red, edges: [1, 0, 0, 1], stoic: 0.1} - ... args: block: 2 Gse: 9.4 As can be seen, this

YAML loads 5e-6 as string and not a number

谁都会走 提交于 2019-11-29 11:21:28
问题 When I load a number with e form a JSON dump with YAML, the number is loaded as a string and not a float. I think this simple example can explain my problem. import json import yaml In [1]: import json In [2]: import yaml In [3]: All = {'one':1,'low':0.000001} In [4]: jAll = json.dumps(All) In [5]: yAll = yaml.safe_load(jAll) In [6]: yAll Out[6]: {'low': '1e-06', 'one': 1} YAML loads 1e-06 as a string and not as a number? How can I fix it? 回答1: The problem lies in the fact that the YAML

Is there a way to construct an object using PyYAML construct_mapping after all nodes complete loading?

[亡魂溺海] 提交于 2019-11-29 09:17:28
I am trying to make a yaml sequence in python that creates a custom python object. The object needs to be constructed with dicts and lists that are deconstructed after __init__ . However, it seems that the construct_mapping function does not construct the entire tree of embedded sequences (lists) and dicts. Consider the following: import yaml class Foo(object): def __init__(self, s, l=None, d=None): self.s = s self.l = l self.d = d def foo_constructor(loader, node): values = loader.construct_mapping(node) s = values["s"] d = values["d"] l = values["l"] return Foo(s, d, l) yaml.add_constructor

How to upgrade disutils package PyYAML?

泪湿孤枕 提交于 2019-11-29 06:03:57
问题 I was trying to install chatterbot which has a dependency on PyYAML=3.12 . In my Ubuntu machine installed PyYAML version is 3.11. So I used the following command to upgrade PyYAML : sudo -H pip3 install --upgrade PyYAML But it gives the following error: Cannot uninstall 'PyYAML'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall. My pip3 version is 10.0.0. How to resolve this? 回答1: Try using the -

How to update yaml file using python

安稳与你 提交于 2019-11-29 02:26:46
问题 I have a some.yaml file with the below contents. init_config: {} instances: - host: <IP> username: <username> password: <password> The yaml file should be parsed and updated as below. init_config: {} instances: - host: 1.2.3.4 username: Username password: Password How do I parse the values and update them appropriately? 回答1: The ruamel.yaml package was specifically enhanced (by me starting from PyYAML) to do this kind of round-trip, programmatic, updating. If you start with (please note I

Parsing YAML, return with line number

我的未来我决定 提交于 2019-11-29 00:59:26
问题 I'm making a document generator from YAML data, which would specify which line of the YAML file each item is generated from. What is the best way to do this? So if the YAML file is like this: - key1: item 1 key2: item 2 - key1: another item 1 key2: another item 2 I want something like this: [ {'__line__': 1, 'key1': 'item 1', 'key2': 'item 2'}, {'__line__': 3, 'key1': 'another item 1', 'key2': 'another item 2'}, ] I'm currently using PyYAML, but any other library is OK if I can use it from

How can I write data in YAML format in a file?

家住魔仙堡 提交于 2019-11-28 16:05:08
问题 I need to write the below data to yaml file using Python: {A:a, B:{C:c, D:d, E:e}} i.e., dictionary in a dictionary. How can I achieve this? 回答1: import yaml data = dict( A = 'a', B = dict( C = 'c', D = 'd', E = 'e', ) ) with open('data.yml', 'w') as outfile: yaml.dump(data, outfile, default_flow_style=False) The default_flow_style=False parameter is necessary to produce the format you want (flow style), otherwise for nested collections it produces block style: A: a B: {C: c, D: d, E: e} 回答2:

Why does PyYAML use generators to construct objects?

爱⌒轻易说出口 提交于 2019-11-28 12:01:38
I've been reading the PyYAML source code to try to understand how to define a proper constructor function that I can add with add_constructor . I have a pretty good understanding of how that code works now, but I still don't understand why the default YAML constructors in the SafeConstructor are generators. For example, the method construct_yaml_map of SafeConstructor : def construct_yaml_map(self, node): data = {} yield data value = self.construct_mapping(node) data.update(value) I understand how the generator is used in BaseConstructor.construct_object as follows to stub out an object and

Parse an AWS CloudFormation template with the PyYAML library

心已入冬 提交于 2019-11-28 11:43:29
I am writing a custom Python application using the PyYAML library that needs to read in AWS CloudFormation YAML templates. I know the templates are valid CloudFormation templates, because I tested them using validate-template: ▶ aws cloudformation validate-template --template-body file://cloudformation.yml When I try to read them using the PyYAML library, however, I get errors like: yaml.scanner.ScannerError: mapping values are not allowed here and could not determine a constructor for the tag "!Sub" and others. By way of example, I try this AWS example template: ▶ curl -s \ https://raw