Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.tele2net.at!news.panservice.it!feed.xsnews.nl!border02.ams.xsnews.nl!feeder04.ams.xsnews.nl!abp002.ams.xsnews.nl!frontend-F10-10.ams.news.kpn.nl From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Seralization Organization: Decebal Computing X-Face: "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR,v+Pti8=Vi/Z"g^?b"E X-Homepage: http://www.decebal.nl/ Date: Sat, 09 May 2015 10:37:47 +0200 Message-ID: <876182xi1w.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:uWmK3V7zGLYzRqpoDpKloXENrKI= MIME-Version: 1.0 Content-Type: text/plain Lines: 73 NNTP-Posting-Host: 81.207.62.244 X-Trace: 1431161105 news.kpn.nl 21104 81.207.62.244@kpn/81.207.62.244:37204 Xref: csiph.com comp.lang.python:90221 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