I suggest using ruamel.yaml if you have the requirement to preserve the original content as much as possible. anchor names that are hand-crafted (i.e.flow style sequences ( ‘a: b, c, d’) (based on request and test by Anthony Sottile).block style and key ordering are kept, so you can diff the round-tripped source.Here’s the explanation from ruamel.yaml documentation:Ī round-trip is a YAML load-modify-save sequence and ruamel.yaml tries to preserve, among others: What interests me most is the ability to round-trip in the loading/dumping process. Generally, ruamel.yaml focuses on YAML 1.2 with some opinionated enhancements for the syntax. The differences with PyYAML are listed here. Ruamel.yaml is a fork of PyYAML, it was released in 2009 and continuously maintained in the past decade. There are tons of great articles on the documentation site of strictyaml, definitely worth having a look at if you have thought about YAML and other configuration languages. I suggest using StrictYAML if you have strong security concerns for your application. It is a type-safe YAML parser that parses and validates a restricted subset of the YAML specification. If we restrict our usage to only a subset of its features, it will be as good as it should be. Some people say YAML is too complex and flexible to be a good configuration language,īut I think this is not the problem of YAML, but the problem of how we use it. ![]() This makes it very flexible to use, you can just copy the code to your library and customize it according to your need. It’s worth mentioning that oyaml is a single-file library with only 53 lines of code. I suggest using oyaml if you already use PyYAML in your code. You should remove this keyword argument to keep the code cleaner and less confusing.Īs mentioned above, oyaml is a drop-in replacement for PyYAML which preserves dict ordering. no JSON in YAML).Īccording to PyYAML documentation, default_flow_style=False should be passed to yaml.safe_dump to achieve that.Īfter digging into the source code of the latest PyYaml (6.0), I find it is not needed anymore. Most of the time we don’t want flow style productions in the output (i.e. safe_dump ( d, allow_unicode = True )) a : 你好 No default_flow_style needed (dump) ¶ In Python 3.7+, the order of dict keys is naturally preserved 1, thus the dict you get from yaml.safe_load has the same order of keys as the original file. In short, you should always use yaml.safe_load and yaml.safe_dump as the standard I/O methods for YAML. Warning: It is not safe to call yaml.load with any data received from an untrusted source! yaml.load is as powerful as pickle.load and so may call any Python function. It might be harmful to your application to simply yaml.load a document from an untrusted source such as the Internet and user input. YAML’s ability to construct an arbitrary Python object makes it dangerous to use blindly. ![]() ![]() ![]() Here I want to share some tips and snippets that can make your life with PyYAML easier.Ĭode in this article is only guaranteed to work in Python 3 Always use safe_load/safe_dump ¶ YAML is a data-serialization language that is widely used.Īs a developer, I’m always dealing with YAML from time to time.īut processing YAML, especially using PyYAML in Python is painful and full of traps.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |