3

I am developing an API in Unix environment for virtual machines. I have to store some information in a table about virtual machines.

Currently I am using python dictionary of virtual machine objects and storing the same in pickle.

I would like to know about other best ways(if any) to store the data in command line API's. Any suggestion would be helpful.

2
  • 1
    If you are working with YAML you might have a look at ruamel.yaml. This question was one of the reason I finally started that solving some of the PyYAML issues in that implementation. Commented Nov 24, 2014 at 13:12
  • I appreciate your effort for the implementation. And I am very glad that this question is one of the reason for that. Sure, I will use this module for YAML. Commented Nov 27, 2014 at 7:15

1 Answer 1

4

For an application used by others you should probably not use pickle as it is not save to load pickle data from untrusted sources (alternatively you would have to make 100% sure the pickled data could not be changed on disc between writing and reading).

If the data ever needs modifying by users, you should have a look at writing it out using YAML. A Python implementation is available via ruamel.yaml (disclaimer: I am the author of that YAML 1.2 parser/dumper).

Alternatives are JSON and with more work XML.

All of these require you to do translation to and from internal types to the types supported by the storage format (dictionaries/maps, lists, primitive types (ints, strings, boolean)).

YAML also support user defined types with the same security issues as pickled Python data, but at least allows some safe loading, something that pickle doesn't support. YAML, through the explicit user types has less problems loading data after changing your source layout (renaming modules, moving classes) as you can experience with pickle.

2
  • YAML is a good choice. It is similar to JSON, but slightly more powerful. Pickling (and serialization in general) is a bad choice for storing persistent config data. For one thing, it is not human readable. Commented Nov 9, 2014 at 10:49
  • @FaheemMitha It is possible to pickle in text form (Protocol version 0), that makes it similar to XML: having the inefficiencies of ASCII combined with the unreadability of binary. Commented Nov 9, 2014 at 10:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.