pyyaml

Formatting custom class output in PyYAML

有些话、适合烂在心里 提交于 2019-12-04 13:47:11
问题 I'm working on a simple example here, but the docs still leave me a bit confused. Here is the Example code: class A(yaml.YAMLObject): yaml_tag = u'!A' def __init__(self, val): self.val = val if __name__ == '__main__': t = datetime.time() a = A(t) print yaml.dump(a) print yaml.load(yaml.dump(a)).val == t The output is !A val: !!python/object/apply:datetime.time ["\0\0\0\0\0\0"] True So, it appears to be faithfully d-/serializing, but the default time object format leaves something to be

How to force PyYAML to load strings as unicode objects?

浪子不回头ぞ 提交于 2019-12-04 09:54:33
问题 The PyYAML package loads unmarked strings as either unicode or str objects, depending on their content. I would like to use unicode objects throughout my program (and, unfortunately, can't switch to Python 3 just yet). Is there an easy way to force PyYAML to always strings load unicode objects? I do not want to clutter my YAML with !!python/unicode tags. # Encoding: UTF-8 import yaml menu= u"""--- - spam - eggs - bacon - crème brûlée - spam """ print yaml.load(menu) Output: ['spam', 'eggs',

ScannerError: mapping values are not allowed here

不想你离开。 提交于 2019-12-04 06:43:54
问题 Using Tavern and trying to run tavern-ci against this yaml: test_name: tavern poc - name: list request: url: https://xxx.xxx.xxx.us/api/v3/institutions/ method: GET response: status_code: 200 headers: content-type: application/json save: body: content: content Am getting E ScannerError: mapping values are not allowed here E in "/Users/xxx/xxx/xxx/test_poc.tavern.yaml", line 3, column 9 Have tried many of the solutions presented here (most of which are 'put a space after the colon') without

Is it possible to use PyYAML to read a text file written with a “YAML front matter” block inside?

狂风中的少年 提交于 2019-12-04 04:58:23
问题 I'm sorry, I know very little of both YAML and PyYAML but I felt in love with the idea of supporting a configuration file written in the same style used by "Jekyll" (http://jekyllrb.com/docs/frontmatter/) that AFAIK have these "YAML Front Matter" blocks that looks very cool and sexy to me. So I installed PyYAML on my computer and I wrote a small file with this block of text: --- First Name: John Second Name: Doe Born: Yes --- Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do

how to keep null value in yaml file while dumping though ruamel.yaml

ぐ巨炮叔叔 提交于 2019-12-04 03:15:53
问题 I have YAML file site.yaml : Kvm_BLOCK: ip_address: 10.X.X.X property: null server_type: zone loaded and then dumped with: ruamel.yaml.dump(site_yaml, new_file, Dumper=ruamel.yaml.RoundTripDumper) it becomes Kvm_BLOCK: ip_address: 10.X.X.X property: server_type: zone how to retain this null value in property block 回答1: The null value in YAML 1.2 (constructed as Python's None ) can be represented as null , Null , NULL and ~ , as specified here. Additionally: Nodes with empty content are

forcing pyYAML to dump consistently

爱⌒轻易说出口 提交于 2019-12-03 13:03:37
In [136]: a = [1,2,3,4,5] In [137]: print yaml.dump(a) [1, 2, 3, 4, 5] In [138]: a = [1,2,3,4,5, [1,2,3]] In [139]: print yaml.dump(a) - 1 - 2 - 3 - 4 - 5 - [1, 2, 3] why are the outputs of above two dumps different? Is it possible to force pyYAML to split the list always? From the documentation : print yaml.dump(a, default_flow_style=False) The value can be True , False , or None . If None or unspecified (that is, the default), it chooses automatically whether to use inline or block-style output. False never uses inline, True is always inline. 来源: https://stackoverflow.com/questions/14024783

I don't understand what a YAML tag is

大城市里の小女人 提交于 2019-12-03 10:32:19
问题 I get it on some level, but I have yet to see an example that didn't bring up more questions than answers. http://rhnh.net/2011/01/31/yaml-tutorial # Set.new([1,2]).to_yaml --- !ruby/object:Set hash: 1: true 2: true I get that we're declaring a Set tag. I don't get what the subsequent hash mapping has to do with it. Are we declaring a schema? Can someone show me an example with multiple tag declarations? I've read through the spec: http://yaml.org/spec/1.2/spec.html#id2761292 %TAG ! tag

How to force PyYAML to load strings as unicode objects?

北城以北 提交于 2019-12-03 04:20:49
The PyYAML package loads unmarked strings as either unicode or str objects, depending on their content. I would like to use unicode objects throughout my program (and, unfortunately, can't switch to Python 3 just yet). Is there an easy way to force PyYAML to always strings load unicode objects? I do not want to clutter my YAML with !!python/unicode tags. # Encoding: UTF-8 import yaml menu= u"""--- - spam - eggs - bacon - crème brûlée - spam """ print yaml.load(menu) Output: ['spam', 'eggs', 'bacon', u'cr\xe8me br\xfbl\xe9e', 'spam'] I would like: [u'spam', u'eggs', u'bacon', u'cr\xe8me br\xfbl

pyyaml: dumping without tags

为君一笑 提交于 2019-12-03 02:31:21
问题 I have >>> import yaml >>> yaml.dump(u'abc') "!!python/unicode 'abc'\n" But I want >>> import yaml >>> yaml.dump(u'abc', magic='something') 'abc\n' What magic param forces no tagging? 回答1: You can use safe_dump instead of dump . Just keep in mind that it won't be able to represent arbitrary Python objects then. Also, when you load the YAML, you will get a str object instead of unicode . 回答2: How about this: def unicode_representer(dumper, uni): node = yaml.ScalarNode(tag=u'tag:yaml.org,2002

I don't understand what a YAML tag is

天大地大妈咪最大 提交于 2019-12-03 01:03:55
I get it on some level, but I have yet to see an example that didn't bring up more questions than answers. http://rhnh.net/2011/01/31/yaml-tutorial # Set.new([1,2]).to_yaml --- !ruby/object:Set hash: 1: true 2: true I get that we're declaring a Set tag. I don't get what the subsequent hash mapping has to do with it. Are we declaring a schema? Can someone show me an example with multiple tag declarations? I've read through the spec: http://yaml.org/spec/1.2/spec.html#id2761292 %TAG ! tag:clarkevans.com,2002: Is this declaring a schema? Is there something else a parser has to do in order to