pyyaml

PyYAML automatically converting certain keys to boolean values

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-02 03:34:24
问题 I've been working with a the PyYAML parser for a few months now to convert file types as part of a data pipeline. I've found the parser to be quite idiosyncratic at times and it seems that today I've stumbled on another strange behavior. The file I'm currently converting contains the following section: off: yes: "Flavor text for yes" no: "Flavor text for no" I keep a list of the current nesting in the dictionary so that I can construct a flat document, but save the nesting to convert back to

YAML key consisting of a list of elements

丶灬走出姿态 提交于 2019-12-30 14:40:49
问题 I need to have a list of elements as a key, so that I can check if several conditions are met. Example (don't know if this is possible and if the syntax is correct): mapping: c_id: [pak, gb]: '4711' [pak, ch]: '4712' [pak]: '4713' d_id: . . . Now I need to know if it is possible to have an approach as in the example. 回答1: In addition to Anthon's answer, here is how you can do it with PyYaml: mapping: c_id: !!python/tuple [pak, gb]: '4711' !!python/tuple [pak, ch]: '4712' !!python/tuple [pak]:

Why does PyYAML use generators to construct objects?

本小妞迷上赌 提交于 2019-12-28 20:33:11
问题 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

improper output with pyyaml

安稳与你 提交于 2019-12-25 16:41:43
问题 I have a YAML file, test.yaml : test: server_group_1: type: OS::Nova::ServerGroup properties: name: { get_param: [server_groups, 5] } policies: [ { get_param: [server_group_types, 5] } ] and when I use PyYAML to read and print the output it gives me below output, which is different from the input test: server_group_1: properties: name: get_param: - server_groups - 5 policies: - get_param: - server_group_types - 5 type: OS::Nova::ServerGroup code: import yaml print yaml.dump(yaml.load(open('

improper output with pyyaml

会有一股神秘感。 提交于 2019-12-25 16:41:37
问题 I have a YAML file, test.yaml : test: server_group_1: type: OS::Nova::ServerGroup properties: name: { get_param: [server_groups, 5] } policies: [ { get_param: [server_group_types, 5] } ] and when I use PyYAML to read and print the output it gives me below output, which is different from the input test: server_group_1: properties: name: get_param: - server_groups - 5 policies: - get_param: - server_group_types - 5 type: OS::Nova::ServerGroup code: import yaml print yaml.dump(yaml.load(open('

ERROR: serverlessrepo 0.1.8 has requirement pyyaml~=3.12

蹲街弑〆低调 提交于 2019-12-24 22:50:17
问题 Below is the docker file with base image python 3.7: FROM python:3.7-alpine3.9 ENV HOME /home/someteam ENV PATH $HOME/.local/bin:$PATH RUN ln -fs /usr/share/zoneinfo/Etc/UTC /etc/localtime RUN apk add --no-cache --virtual .build-deps python2-dev python3-dev gcc linux-headers musl-dev && \ addgroup someteam --gid=5566; \ adduser -G someteam -Du 5566 -h /home/someteam -s /bin/bash someteam; \ chown -R someteam $HOME; RUN apk add --no-cache groff less bash jq curl py-pip tzdata USER someteam

Unnecessary quotation of subkey and iteration through primary key in PyYaml event structure

房东的猫 提交于 2019-12-24 20:27:20
问题 I have the next code: import gnupg import re import textwrap from pprint import pprint import yaml from yaml.events import * class AppendableEvents: def __init__(self, path, events): self.path = path self.events = events def correct_position(self, levels): if len(self.path) != len(levels): return False for index, expected in enumerate(self.path): if expected != levels[index].cur_id: return False return True class Level: def __init__(self, mode): self.mode = mode self.cur_id = -1 if mode ==

yaml use key or parent key as value

杀马特。学长 韩版系。学妹 提交于 2019-12-24 12:44:09
问题 I’ve just started using YAML (through pyyaml) and I was wondering if there is any way to state that the value of a key is the key name itself or the parent key. For example --- foo: &FOO bar: !. baz: !.. foo2: <<: *FOO … {‘foo’: {‘bar’: ‘bar’, ‘baz’: ’foo’}, ‘foo2’:{‘bar’:’bar’, ‘baz’:’foo2’}} (notice the dot and double dot on bar and baz respectively - those are just placeholders for getting the key name and parent key name) I've tried using add_constructor : def key_construct(loader, node):

How to print a value with double quotes and spaces in YAML?

廉价感情. 提交于 2019-12-24 04:31:45
问题 I am trying to dump a Python dictionary to YAML which has some strings as value fields. import yaml str1 = "hello" str2 = "world" mystr = "\"" + str1 + str(" ") + str2 + "\"" mydict = {"a" : mystr} f = open("temp.yaml", "w") yaml.dump(mydict, f, default_flow_style = False, \ explicit_start = "---", explicit_end = "...", encoding = 'UTF-8') f.close() the YAML I get is: a: '"hello world"' Notice, the value "hello world" is spilling to the next line. I am using python 3.5 and YAML module version

Generating anchors with PyYAML.dump()?

僤鯓⒐⒋嵵緔 提交于 2019-12-23 20:19:11
问题 I'd like to be able to generate anchors in the YAML generated by PyYAML's dump() function. Is there a way to do this? Ideally the anchors would have the same name as the YAML nodes. Example: import yaml yaml.dump({'a': [1,2,3]}) 'a: [1, 2, 3]\n' What I'd like to be able to do is generate YAML like: import yaml yaml.dump({'a': [1,2,3]}) 'a: &a [1, 2, 3]\n' Can I write a custom emitter or dumper to do this? Is there another way? 回答1: By default, anchors are only emitted when it detects a