pyyaml

How do I write a representer for PyYAML?

三世轮回 提交于 2019-12-21 12:11:46
问题 I want to have a custom function that serializes arbitrary python objects, like how the json.dump function has an optional arg called 'default', that should be a function that the json dumper will call if the object is not json serializable. I simply want to do the equivalent of this from the json package. json.dump(tests_dump, file('somefile', 'w+'), default = lambda x: x.__dict__) It looks like I need to write yaml.add_representer, from the PyYAML docs, but it really isn't clear how to do

How to deserialize an object with PyYAML using safe_load?

£可爱£侵袭症+ 提交于 2019-12-21 04:51:33
问题 Having a snippet like this: import yaml class User(object): def __init__(self, name, surname): self.name= name self.surname= surname user = User('spam', 'eggs') serialized_user = yaml.dump(user) #Network deserialized_user = yaml.load(serialized_user) print "name: %s, sname: %s" % (deserialized_user.name, deserialized_user.surname) Yaml docs says that it is not safe to call yaml.load with any data received from an untrusted source; so, what should I modify to my snippet\class to use safe_load

Can I dump blank instead of null in yaml/pyyaml?

蓝咒 提交于 2019-12-21 04:27:08
问题 Using PyYAML, if I read in a file with blank values in a dict: test_str = ''' attrs: first: second: value2 ''' This returns None for the key first : >>> data = yaml.load(test_str) >>> data {'attrs': {'second': 'value2', 'first': None}} But when writing, the None value is replaced with null : >>> print(yaml.dump(data, default_flow_style=False)) attrs: first: null second: value2 Is there a way to format the dump output to print a blank scalar rather than null ? 回答1: Based on @Anthon's excellent

Why is PyYAML spending so much time in just parsing a YAML File?

南楼画角 提交于 2019-12-21 04:13:15
问题 I am parsing a YAML file with around 6500 lines with this format: foo1: bar1: blah: { name: "john", age: 123 } metadata: { whatever1: "whatever", whatever2: "whatever" } stuff: thing1: bluh1: { name: "Doe1", age: 123 } bluh2: { name: "Doe2", age: 123 } thing2: ... thingN: foo2: ... fooN: I just want to parse it with the PyYAML library (I think there is no more alternatives to it in Python: How can I parse a YAML file in Python). Just for testing, I write that code to parse my file: import

forcing pyYAML to dump consistently

允我心安 提交于 2019-12-21 04:03:14
问题 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? 回答1: 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

Cygwin - How to install ansible?

佐手、 提交于 2019-12-20 10:41:59
问题 How to get / install ansible using Cygwin? I tried the following steps but it's didn't work during bullet 5 (while running " python setup.py install "). Steps taken from: Taken from https://servercheck.in/blog/running-ansible-within-windows 1) Download and install Cygwin, with at least the following packages selected (you can select the packages during the install process): libyaml libyaml-devel curl python (2.7.x) python-crypto python-openssl python-paramiko python-setuptools git (2.1.x) vim

How can I ignore a member when serializing an object with PyYAML?

旧街凉风 提交于 2019-12-20 07:04:41
问题 How can ignore the member Trivial._ignore when serializing this object? import yaml class Trivial(yaml.YAMLObject): yaml_tag = u'!Trivial' def __init__(self): self.a = 1 self.b = 2 self._ignore = 3 t = Trivial() print(yaml.dump(t)) prints !Trivial _ignore: 3 a: 1 b: 2 回答1: def my_yaml_dump(yaml_obj): my_ob = deepcopy(yaml_obj) for item in dir(my_ob): if item.startswith("_") and not item.startswith("__"): del my_ob.__dict__[item] return yaml.dump(my_ob) something like this would ignore

Python and PYAML - yaml.scanner.ScannerError: mapping values are not allowed here

狂风中的少年 提交于 2019-12-19 05:12:56
问题 I am on ubunty 64 with python 2.7 and using PyYAML-3.10 Below is my yaml file: host:localhost username:root password:test database:test operations_database:operations treeroot: branch1: name: Node 1 branch1-1: name: Node 1-1 branch2: name: Node 2 branch2-1: name: Node 2-1 When I run the below code I get the below error. But if I remove the lines above the treeroot the code works: from yaml import load, dump try: from yaml import CLoader as Loader, CDumper as Dumper except ImportError: from

pyYAML Errors on “!” in a string

送分小仙女□ 提交于 2019-12-17 19:35:18
问题 First, a disclaimer: I'm not too familiar with YAML. I'm trying to parse a YAML doc into Key Value Pairs (don't worry about how I'm doing it. I've got that bit handled) My file used to look something like: world: people: name:Suzy address:chez-bob Then, someone went and changed it. world: people: name:!$uzy address:chez-bob And I get this parse error: yaml.constructor.ConstructorError: could not determine a constructor for the tag '!$uzy' What does this even mean? How would I go about getting

Save/dump a YAML file with comments in PyYAML

可紊 提交于 2019-12-17 18:00:51
问题 I have a yaml file that looks like this: # The following key opens a door key: value Is there a way I can load and dump this data while maintaining the comment? 回答1: PyYAML throws away comments at a very low level (in Scanner.scan_to_next_token ). While you could adapt or extend it to handle comments in its whole stack, this would be a major modification. Dump ing (=emitting) comments seems to be easier and was discussed in ticket 114 on the old PyYAML bug tracker. 回答2: If you are using block