In the same way, YAML is not a markup language (after all, it originally stood for "Yaml Ain't a Markup Language"), it is a data format. That order is something you're not supposed to be able to mess with because if a faster way of implementing dictionaries is discovered tomorrow the runtime needs to implement it without breaking everyone's code that depended on dictionaries being a helpful abstraction of a hash table. A dictionary is a hash table, internally ordered exclusively for search speed. You are asking for order out of something that is inherently unordered. So I define my order, then pack an ordered list of lists, and then dump that. 'add-ons' comes before 'name', so I have an ordering problem. Normally PyYAML orders dictionary keys in alphanumeric order when you call dump(), but this isn't reliable because that might change in the future and there is nothing in the YAML standard that requires this, so I have no guarantee that a different YAML utility will do things this way. In this example I want to have a project 'name' come first in the file, then 'version' number elements, then 'add-ons'. You may have to recursively descend the structure if you have a nested structure which requires its order preserved (this is easier to do in code than to explain in prose - which is something you probably already know). To read it back in read it as normal, then pass that structure to dict() and you're back to the original dictionary you started with. Use that list to generate an ordered list of lists (not list of tuples, though, because you'll get the !!python/tuple type in YAML, and that sucks). If you have an ordering of keys you want preserved, write that into an ordered list/tuple. That said, it can take pretty YAML as initial input (yay!), and scripting the conversion from un-pretty, ordered YAML to pretty, still-ordered, dict-style YAML is straightforward (which I leave as an exercise for you). This doesn't produce as pretty a YAML document as you might hope. 'version': į.write(yaml.dump(things, default_flow_style=False, allow_unicode=True)) The rest of this post is example/verification code and an explanation of why things are this way. This situation is identical for JSON, by the way, as YAML is a superset of JSON and neither guarantee the order of keys - which means the workarounds break the first time a standard-compliant tool/user messes with the file.) (There are semi-magical ways of coercing an ordered dict in YAML without the !!python/ordered_dict or !!omap mess everywhere, but they are fragile, contradict the very definition of dictionaries, and will likely break as the underlying YAML library evolves. Ultimately you are dealing with two independent sets of data: the dict itself, and a metadata that defines the ordering of the keys. My vote goes to !!omap, but I'm unsure how many tools/libs support it (I'm pretty sure fewer tools support !!python/ordered_dict, though). If you don't mind horribly ugly explicit types like !!python/ordered_dict or !!omap littering your file then you can go that route as well. TL DR: The solution is in the two lines commented "LOOK HERE!" It is possible to deal with YAML as dicts within your program and with an ordering in the stored file/text if you accept that the output will be lists of lists.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |