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


Groups > comp.lang.python > #90223

Re: Seralization

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; "'rb')": 0.16; 'filename):': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; '{0}': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; "shouldn't": 0.24; 'cc:2**0': 0.24; 'pass': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'there.': 0.32; 'skip:m 30': 0.32; 'maybe': 0.34; 'except': 0.35; 'skip:s 30': 0.35; 'received:google.com': 0.35; 'data,': 0.36; 'skip:j 20': 0.36; 'pm,': 0.38; 'skip:p 20': 0.39; '2015': 0.84; 'formats:': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=duEhfKQF3qlZQ+h5MT9qelwPRHctcwiKWANYBP8slS8=; b=vANLzj96jlUSivPX3rfCZO/M4EbHl3FvSIrdHU+86caA7ieW5M8v0f/BFTOFT5povF HHI2HzcLqNcNg1dxQjL03F5Kqkh7e7fdtQr+eCtRAuB/L4g/KENYqukqm9aKRSkcQwNm nlYSj2ZTZ8uspspp0/uuUsOdgvY8oYJzpQPDGVuqBsRUVm3yW7jxrY+TF7FH5bgBHtbb hgy0OwkK7hhBm3cykRhwUImSRvyDqntnEM4HprEUMsrDzXkI8fO+uUZ3rBaJOiXDIyTA qCv9zEeuskR4rylfLWA2Hv21gO92wjKXA1StlRjJTQtLGNL9x7SHrmDdViWFS4iJ+9Cf 5iQg==
MIME-Version 1.0
X-Received by 10.107.160.202 with SMTP id j193mr2327254ioe.43.1431163014467; Sat, 09 May 2015 02:16:54 -0700 (PDT)
In-Reply-To <876182xi1w.fsf@Equus.decebal.nl>
References <876182xi1w.fsf@Equus.decebal.nl>
Date Sat, 9 May 2015 19:16:54 +1000
Subject Re: Seralization
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.279.1431163016.12865.python-list@python.org> (permalink)
Lines 93
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1431163016 news.xs4all.nl 2918 [2001:888:2000:d::a6]:54862
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:90223

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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