To make serialization a bit easier I made a few functions to get, save and convert between the different types. As I see it pickle and json are probably used the most. I also have a get and save for marshal. But no conversion to marshal, because in principle you should not use it, so a conversion to it is not useful. I did define conversion from.
Are there other seralizations that is handy to take care of? I understood that because the differences between platforms it is better to do the file operatins in binary mode. Is that true? The code: def get_json(json_file): with open(json_file, 'rb') as in_f: return json.load(in_f) def get_marshal(marshal_file): with open(marshal_file, 'rb') as in_f: return marshal.load(in_f) def get_pickle(pickle_file): with open(pickle_file, 'rb') as in_f: return pickle.load(in_f) def save_json(data, json_file): with open(json_file, 'wb') as out_f: json.dump(data, out_f) def save_marshal(data, marshal_file): with open(marshal_file, 'wb') as out_f: marshal.dump(data, out_f) def save_pickle(data, pickle_file): with open(pickle_file, 'wb') as out_f: pickle.dump(data, out_f) def marshal_to_pickle(marshal_file, pickle_file): data_in = get_marshal(marshal_file) save_pickle(data_in, pickle_file) data_out = get_pickle(pickle_file) if data_in != data_out: raise SerializationError('Serialization from {0} to {1} not succesfull'. format(marshal_file, pickle_file)) def marshal_to_json(marshal_file, json_file): data_in = get_marshal(marshal_file) save_json(data_in, json_file) data_out = get_json(json_file) if data_in != data_out: raise SerializationError('Serialization from {0} to {1} not succesfull'. format(marshal_file, json_file)) def pickle_to_json(pickle_file, json_file): data_in = get_pickle(pickle_file) save_json(data_in, json_file) data_out = get_json(json_file) if data_in != data_out: raise SerializationError('Serialization from {0} to {1} not succesfull'. format(pickle_file, json_file)) def json_to_pickle(json_file, pickle_file): data_in = get_json(json_file) save_pickle(data_in, pickle_file) data_out = get_pickle(pickle_file) if data_in == data_out: raise SerializationError('Serialization from {0} to {1} not succesfull'. format(json_file, pickle_file)) -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof -- https://mail.python.org/mailman/listinfo/python-list