开发者

I don't like how "yaml.dump()" saves its files. Is there an alternative?

I've discovered YAML recently and it has turned out to be a fantastic format for my project (I'm creating a text-based RPG). It's handling everything I need flawlessly - area files (rooms with descs, exits, objs, npcs, etc), script files, npc files, object files... it tackles them all! There's just one thing...

When updating values in these files I of course need to write them back to disk to keep the changes. Unfortunately YAML's dump() met开发者_如何学运维hod really makes a mess of things. First it alphabetizes all the keys, and then it appears to put one k:v pair per line. This effectively took a file with 5 lines and turned it into 94 lines.

I've tried writing the YAML objects back to disk using write() but it throws

TypeError: expected a character buffer object

at me. Pickle would save it, but I think that would destroy the YAML-ness of the data. Is there a good way to save a YAML object without dump()? I've heard mention of SQLite, would that work the same as YAML? I have little to no database experience but it seems like YAML objects are basically databases. Would it be worth re-coding everything to use SQLite? Is it easy to save changed values with it, unlike the problem I have having now?

Any assistance here would be great. I love YAML a lot. I struggled for a long time trying to find a way to store all the data I need for my game and when I found YAML it was as if it glowed with holy light as a chorus of angels sang their glory upon it - so you can understand if I'd first like to know of a way to save YAML data to disk without using dump(). If not, suggestions are appreciated!


YAML is a way of serialising and deserialising dictionaries and lists between Python and a text-editable format. It isn't suited for storing rich data, multi-user access, fast lookups of data, guaranteeing data integrity and all the other things that make a database a database.

You're using YAML as a database, but it's not a database. For example, the reordering of the keys is an artefact of the fact that key: value pairs are represented as a Python dictionary, which is inherently unordered.

You definitely need to spend some time learning a bit about databases.

SQLite is as good a place to start as any. You could also look at Postgres or MySQL. You'll probably want to read some beginner's guides to SQL and to doing database access with Python. There are plenty on the web.


It is possible to preserve order with YAML and Python.

Python has OrderedDict and YAML has an equivalent ordered mapping called omap.

Unfortunately when you dump an OrderedDict with YAML it tries to preserve the OrderedDict python object type instead of converting it to YAML omap. This is in part because the YAML specification is meant to play nice with a plethora of languages, and technically a YAML map or omap allow duplicate keys I believe (I'd have to re-research that, but I've seen it cited as a 'key' difference between YAML and JSON -- YAML declares how it handles duplicate keys where JSON doesn't.)

Anyways, there is a way to tell YAML to load data into an OrderedDict and to dump the OrderedDict sequencially into a map or omap. The most concise source I have found is py-lessly. Though there are some other implementations around the web. I also like that the py-lessly code dumps to a simple yaml map which doesn't require !!omap next to every dictionary - keeps the YAML pretty!

There is also layered-yaml-attrdict-config which loads YAML data into an OrderedDict subclass called AttrDict with some additional features. I had to add some stuff to this implementation to work for me - I think it lost the order when dumping, so I added code from the py-lessly implementation. I also wanted to work with combined dict and list structure and had to write some extra code to apply AttrDict to the whole data structure recursively.

From those examples you can see how to use YAML to work with ordered mappings. I'm currently working on implementing this for application settings which I would like to stay relatively human-readable and for which order is important.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜