Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed.pionier.net.pl!feed.xsnews.nl!border03.ams.xsnews.nl!feeder03.ams.xsnews.nl!abp001.ams.xsnews.nl!frontend-F09-20.ams.news.kpn.nl From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: Seralization Organization: Decebal Computing References: <876182xi1w.fsf@Equus.decebal.nl> 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 13:38:16 +0200 Message-ID: <87y4kyvv4n.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:uvdX891Rz4GwWOqMoLxMGoL2Pkw= MIME-Version: 1.0 Content-Type: text/plain Lines: 56 NNTP-Posting-Host: 81.207.62.244 X-Trace: 1431171906 news.kpn.nl 21214 81.207.62.244@kpn/81.207.62.244:58306 Xref: csiph.com comp.lang.python:90233 Op Saturday 9 May 2015 11:16 CEST schreef Chris Angelico: > On Sat, May 9, 2015 at 6:37 PM, Cecil Westerhof wrote: >> 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 get_any(format, filename): > with open(filename, 'rb') as in_f: > return format.load(in_f) I was thinking about something like that and then let the other three call this one. I think that: data = get_json(json_file) is nicer to do as: data = get_any(json, json_file) > def any_to_any(fmt1, fmt2, fn1, fn2): data_in = get_any(fmt1, fn1) > save_any(fmt2, data_in, fn2) data_out = get_any(fmt2, fn2) if > data_in != data_out: raise SerializationError('Serialization from > {0} to {1} not successful'. format(fn1, fn2)) > > formats = [json, pickle, marshal] > for fmt1 in formats: > for fmt2 in formats: > globals()["%s_to_%s" % (fmt1.__name__, fmt2.__name__)] = \ > functools.partial(any_to_any, fmt1, fmt2) > >> 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)) > > def json_to_pickle(json_file, pickle_file): try: any_to_any(json, > pickle, json_file, pickle_file) except SerializationError: pass > else: raise SerializationError('Serialization from {0} to {1} not > successful'. format(json_file, pickle_file)) Was thinking about that also. Only should there be no conversion to marshal I think. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof