Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90232

Re: Seralization

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Re: Seralization
Organization Decebal Computing
References <876182xi1w.fsf@Equus.decebal.nl>
Date 2015-05-09 15:04 +0200
Message-ID <87twvlx5p6.fsf@Equus.decebal.nl> (permalink)

Show all headers | 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 | NextPrevious in thread | Find similar | Unroll thread


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