Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90232
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!weretis.net!feeder4.news.weretis.net!storethat.news.telefonica.de!telefonica.de!news.panservice.it!feed.xsnews.nl!border02.ams.xsnews.nl!feeder04.ams.xsnews.nl!abp001.ams.xsnews.nl!frontend-F10-17.ams.news.kpn.nl |
|---|---|
| From | Cecil Westerhof <Cecil@decebal.nl> |
| 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<J%a^F2lh4[N~Yz4 nSp#c+aQo1b5=?HcNEkQ7QzF<])O3X4MDL/AYjys&*mt>,v+Pti8=Vi/Z"g^?b"E |
| X-Homepage | http://www.decebal.nl/ |
| Date | Sat, 09 May 2015 15:04:37 +0200 |
| Message-ID | <87twvlx5p6.fsf@Equus.decebal.nl> (permalink) |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
| Cancel-Lock | sha1:VdhTehqDYhp60P23pVE3nppb6lw= |
| MIME-Version | 1.0 |
| Content-Type | text/plain |
| Lines | 62 |
| NNTP-Posting-Host | 81.207.62.244 |
| X-Trace | 1431178153 news.kpn.nl 1237 81.207.62.244@kpn/81.207.62.244:48491 |
| Xref | csiph.com comp.lang.python:90232 |
Show key headers only | View raw
Op Saturday 9 May 2015 10:37 CEST schreef Cecil Westerhof:
> 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.
I used the example of Chris to make it a lot cleaner. I like DRY, but
did not know it was that easy in Python.
The code is now:
def convert_serialization(format_in, format_out, filename_in, filename_out):
data_in = get_serialization(format_in, filename_in)
save_serialization(format_out, data_in, filename_out)
data_out = get_serialization(format_out, filename_out)
if data_in != data_out:
raise SerializationError('Serialization from {0} to {1} not successful'.
format(filename_in, filename_out))
def get_serialization(format, filename):
with open(filename, 'rb') as in_f:
return format.load(in_f)
def save_serialization(format, data, filename):
with open(filename, 'wb') as out_f:
format.dump(data, out_f)
_all_serialization_formats = [json, marshal, pickle]
_convert_to_serialization_formats = [json, pickle]
for format_in in _all_serialization_formats:
globals()[
'save_%s' % (format_in.__name__)
] = functools.partial(save_serialization, format_in)
globals()[
'get_%s' % (format_in.__name__)
] = functools.partial(get_serialization, format_in)
for format_out in _convert_to_serialization_formats:
if format_in == format_out:
continue
globals()[
'%s_to_%s' % (format_in.__name__, format_out.__name__)
] = functools.partial(convert_serialization, format_in, format_out)
del format_in, format_out
I keep the all_serialization_formats and
_convert_to_serialization_formats so you can peek which ones are
supported at the moment.
> Are there other seralizations that is handy to take care of?
That is now a cinch to implement: you only have to append the two
lists. ;-)
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Seralization Cecil Westerhof <Cecil@decebal.nl> - 2015-05-09 10:37 +0200
Re: Seralization Chris Angelico <rosuav@gmail.com> - 2015-05-09 19:16 +1000
Re: Seralization Cecil Westerhof <Cecil@decebal.nl> - 2015-05-09 13:38 +0200
Re: Seralization Cecil Westerhof <Cecil@decebal.nl> - 2015-05-09 15:04 +0200
csiph-web