pyyaml

Why fatal error: 'yaml.h' file not found when installing PyYAML?

大憨熊 提交于 2019-12-06 17:07:30
问题 I'm trying out downloading PyYAML and install it following the instructions here http://pyyaml.org/wiki/PyYAML So I downloaded the ZIP package: http://pyyaml.org/download/pyyaml/PyYAML-3.11.zip and then cd into that folder and run python setup.py --with-libyaml install , the error message I got is ext/_yaml.h:2:10: fatal error: 'yaml.h' file not found But I checked the PyYAML-3.11 folder, the yaml.h is there... UPDATE: I tried the methods here http://sandlininc.com/?p=500 $ sudo easy_install

Serializing namedtuples via PyYAML

心不动则不痛 提交于 2019-12-06 09:43:42
I'm looking for some reasonable way to serialize namedtuples in YAML using PyYAML. A few things I don't want to do: Rely on a dynamic call to add a constructor/representor/resolver upon instantiation of the namedtuple. These YAML files may be stored and re-loaded later, so I cannot rely on the same runtime environment existing when they are restored. Register the namedtuples in global. Rely on the namedtuples having unique names I was thinking of something along these lines: class namedtuple(object): def __new__(cls, *args, **kwargs): x = collections.namedtuple(*args, **kwargs) class New(x):

Creating Custom Tag in PyYAML

坚强是说给别人听的谎言 提交于 2019-12-06 06:45:52
问题 I'm trying to use Python's PyYAML to create a custom tag that will allow me to retrieve environment variables with my YAML. import os import yaml class EnvTag(yaml.YAMLObject): yaml_tag = u'!Env' def __init__(self, env_var): self.env_var = env_var def __repr__(self): return os.environ.get(self.env_var) settings_file = open('conf/defaults.yaml', 'r') settings = yaml.load(settings_file) And inside of defaults.yaml is simply: example: !ENV foo The error I keep getting: yaml.constructor

How to dump YAML with explicit references?

北城余情 提交于 2019-12-05 08:31:48
Recursive references work great in ruamel.yaml or pyyaml : $ ruamel.yaml.dump(ruamel.yaml.load('&A [ *A ]')) '&id001 - *id001' However it (obviously) does not work on normal references: $ ruamel.yaml.dump(ruamel.yaml.load("foo: &foo { a: 42 }\nbar: { <<: *foo }")) bar: {a: 42} foo: {a: 42} I would like is to explicitly create a reference: data = {} data['foo'] = {'foo': {'a': 42}} data['bar'] = { '<<': data['foo'], 'b': 43 } $ ruamel.yaml.dump(data, magic=True) foo: &foo a: 42 bar: <<: *foo b: 43 This will be very useful to generate YAML output of large data structures that have lots of common

PyYAML automatically converting certain keys to boolean values

感情迁移 提交于 2019-12-05 06:43:23
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 later on. I got a TypeError saying I was trying to concatenate a str and bool type together. I

How to load a pyYAML file and access it using attributes instead of using the dictionary notation?

强颜欢笑 提交于 2019-12-05 06:37:50
I have an YAML config that looks like: config: - id: foo - name: bar content: - run: xxx - remove: yyy I am using Python YAML module to load it but I want to access it in better ways like: stream = open(filename) config = load(stream, Loader=Loader) print(config['content']) What I want is to be able to do: print(config.content) . Chris You can use object notation with dictionaries using the following class, as discussed in this answer: class DictAsMember(dict): def __getattr__(self, name): value = self[name] if isinstance(value, dict): value = DictAsMember(value) return value This class in

How can I add a python tuple to a YAML file using pyYAML?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 05:16:10
The title is fairly self-explanatory. When I save a tuple to a YAML file, I get something that looks like this: ambient: !!python/tuple [0.3, 0.3 ,0.3] When I try to load it with yaml.safe_load(file_object), I keep getting an error that reads: yaml.constructor.ConstructorError: could not determine a constructor for the tag 'tag:yaml.org,2002:python/tuple' What needs to be done? In pyyaml, the SafeLoader does not include a loader for the python native types, only the types defined in the yaml spec. You can see the types for the SafeLoader and the Loader here in the interaction sample below. You

Changing a value in a yaml file using Python

好久不见. 提交于 2019-12-05 02:19:16
问题 I have a .yaml file I want to update with a Python code. Let's say it looks something like that: state: 'present' I'd like to have a code that changes the state and saves the file. I'm trying with something like this and fail: def set_state(state): with open("file_to_edit.yaml", 'rw') as f: doc = yaml.load(f) doc['state'] = state yaml.dump(f) I am using the 'yaml' package for Python. 回答1: The problem is that yaml.dump(doc) doesn't actually write to a file. Instead, it returns the modified

PyYAML : Control ordering of items called by yaml.load()

半城伤御伤魂 提交于 2019-12-05 00:15:45
I have a yaml setting file which creates some records in db: setting1: name: [item,item] name1: text anothersetting2: name: [item,item] sub_setting: name :[item,item] when i update this file with setting3 and regenerate records in db by: import yaml fh = open('setting.txt', 'r') setting_list = yaml.load(fh) for i in setting_list: add_to_db[i] it's vital that the order of them settings (id numbers in db) stay the same each time as im addig them to the db... and setting3 just gets appended to the yaml.load()'s end so that its id doesn't confuse any records which are already in the db ... At the

pretty output with pyyaml

我怕爱的太早我们不能终老 提交于 2019-12-04 16:59:12
问题 I have a python project where I'd like to use YAML (pyYaml 3.11), particularly because it is "pretty" and easy for users to edit in a text editor if and when necessary. My problem, though, is if I bring the YAML into a python application (as I will need to) and edit the contents (as I will need to) then writing the new document is typically not quite as pretty as what I started with. The pyyaml documentation is pretty poor - does not even document the parameters to the dump function. I found