I am working on a python desktop application in wxPython and I'm worried about the file format for files that the application saves (like a .DOC file for MSOffice).
Instead of using the struct module (struct.pack and struct.unpack) to write the binary data to a file, I made use of Pickle/cPickle since the data structure to be stored was very complex (and constantly changing during development). Since using cPickle was a "one-liner" I took the easy route first.
I took into account:
- Make sure not to store names I don't control: For example, even though a cnumpy.core.multiarray can be serialized, I convert it to python list.
- Class Names: They MUST remain the same after each application update or de-serialization of old files will fail.
- Class Variable Names: They MUST remain the same after each application update or use of de-serialization of old data could fail.
- Cant serialize PySWIG objects: modified __getstate__ to replace these objects with a tuple of their important arguments and modified __setstate__ to read those arguments and produce the right wxPython/PySWIG object (this had to be done for drag/drop operations within the program anyway).
- Application updates (Adding variables to objects): If any objects which were serialized with a previous version of the application are missing a variable upon de-serialization, it is set to a default value in __setstate__:
>> def __setstate__(self,dict):
>> #Set missing value to some default
>> if "NewValue" not in dict: dict["NewValue"] = 'Some default'
>> self.__dict__.update(dict)
>> return
Some of these appear to be reasonable things to do anyway between application builds but other than the gorilla in the room of not being able to easily port this file format to other environments (Different python program, C++, Java, whatever), does anyone see this biting me later?
Or once development is done, should I just do the work to use struct to write the file?
thanks in advance,
Ben