Seralization

Chris Angelico rosuav at gmail.com
Sat May 9 05:16:54 EDT 2015


On Sat, May 9, 2015 at 6:37 PM, Cecil Westerhof <Cecil at decebal.nl> 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)

>     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 save_any(format, data, filename):
    with open(filename,'wb') as out_f:
        format.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 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))

There. Much simpler. And maybe you can see that the last one actually
shouldn't exist :)

ChrisA



More information about the Python-list mailing list