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


Groups > comp.lang.python > #46114 > unrolled thread

Re: serialize a class to XML and back

Started byChris Rebert <clp2@rebertia.com>
First post2013-05-26 13:36 -0700
Last post2013-05-31 13:21 +0200
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: serialize a class to XML and back Chris Rebert <clp2@rebertia.com> - 2013-05-26 13:36 -0700
    Re: serialize a class to XML and back Roy Smith <roy@panix.com> - 2013-05-26 16:48 -0400
      Re: serialize a class to XML and back Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-05-27 00:40 +0200
        Re: serialize a class to XML and back Roy Smith <roy@panix.com> - 2013-05-26 20:39 -0400
          Re: serialize a class to XML and back Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-05-27 20:45 +0200
      Re: serialize a class to XML and back Schneider <js@globe.de> - 2013-05-31 13:21 +0200

#46114 — Re: serialize a class to XML and back

FromChris Rebert <clp2@rebertia.com>
Date2013-05-26 13:36 -0700
SubjectRe: serialize a class to XML and back
Message-ID<mailman.2197.1369600623.3114.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

On May 23, 2013 3:42 AM, "Schneider" <js@globe.de> wrote:
>
> Hi list,
>
> how can I serialize a python class to XML? Plus a way to get the class
back from the XML?

There's pyxser: http://pythonhosted.org/pyxser/

> My aim is to store instances of this class in a database.

Honestly, I would avoid XML if you can. Consider using JSON (Python
includes the `json` module in the std lib) or pickle instead. Compared to
XML: The former is more standardized (in the context of serializing
objects) and less verbose; the latter is more efficient (if you don't care
about cross-language accessibility); both have more convenient APIs.

Cheers,
Chris

[toc] | [next] | [standalone]


#46117

FromRoy Smith <roy@panix.com>
Date2013-05-26 16:48 -0400
Message-ID<roy-D5172C.16482026052013@news.panix.com>
In reply to#46114
In article <mailman.2197.1369600623.3114.python-list@python.org>,
 Chris Rebert <clp2@rebertia.com> wrote:

> On May 23, 2013 3:42 AM, "Schneider" <js@globe.de> wrote:
> >
> > Hi list,
> >
> > how can I serialize a python class to XML? Plus a way to get the class
> back from the XML?
> 
> There's pyxser: http://pythonhosted.org/pyxser/
> 
> > My aim is to store instances of this class in a database.
> 
> Honestly, I would avoid XML if you can. Consider using JSON (Python
> includes the `json` module in the std lib) or pickle instead. Compared to
> XML: The former is more standardized (in the context of serializing
> objects) and less verbose; the latter is more efficient (if you don't care
> about cross-language accessibility); both have more convenient APIs.

Some other points...

If you care about efficiency and want to use json, don't use the one 
that comes packaged with the standard library.  There are lots of 
third-party json packages (ujson is the one we use) which are 
significantly faster.  Not sure if that's true of the newest python 
releases, but it was certainly true in 2.6.

The advantage of pickle over json is that pickle can serialize many 
types of objects that json can't.  The other side of the coin is that 
pickle is python-specific, so if you think you'll ever need to read your 
data from other languages, pickle is right out.

[toc] | [prev] | [next] | [standalone]


#46139

FromIrmen de Jong <irmen.NOSPAM@xs4all.nl>
Date2013-05-27 00:40 +0200
Message-ID<51a28f42$0$15870$e4fe514c@news.xs4all.nl>
In reply to#46117
On 26-5-2013 22:48, Roy Smith wrote:

> The advantage of pickle over json is that pickle can serialize many 
> types of objects that json can't.  The other side of the coin is that 
> pickle is python-specific, so if you think you'll ever need to read your 
> data from other languages, pickle is right out.

That is not entirely true :)  I've written a pickle implementation for Java and .NET
that is almost feature complete; it is part of http://pythonhosted.org/Pyro4/pyrolite.html

Still, pickle may not be the best choice here.

Cheers
Irmen

[toc] | [prev] | [next] | [standalone]


#46150

FromRoy Smith <roy@panix.com>
Date2013-05-26 20:39 -0400
Message-ID<roy-2C62C9.20390126052013@news.panix.com>
In reply to#46139
In article <51a28f42$0$15870$e4fe514c@news.xs4all.nl>,
 Irmen de Jong <irmen.NOSPAM@xs4all.nl> wrote:

> On 26-5-2013 22:48, Roy Smith wrote:
> 
> > The advantage of pickle over json is that pickle can serialize many 
> > types of objects that json can't.  The other side of the coin is that 
> > pickle is python-specific, so if you think you'll ever need to read your 
> > data from other languages, pickle is right out.
> 
> That is not entirely true :)  I've written a pickle implementation for Java 
> and .NET
> that is almost feature complete; it is part of 
> http://pythonhosted.org/Pyro4/pyrolite.html

Very cool

> Still, pickle may not be the best choice here.

Perhaps not, but lots of points for the awesomeness factor.

[toc] | [prev] | [next] | [standalone]


#46217

FromIrmen de Jong <irmen.NOSPAM@xs4all.nl>
Date2013-05-27 20:45 +0200
Message-ID<51a3a9b5$0$15963$e4fe514c@news.xs4all.nl>
In reply to#46150
On 27-5-2013 2:39, Roy Smith wrote:
> In article <51a28f42$0$15870$e4fe514c@news.xs4all.nl>,
>  Irmen de Jong <irmen.NOSPAM@xs4all.nl> wrote:
> 
>> On 26-5-2013 22:48, Roy Smith wrote:
>>
>>> The advantage of pickle over json is that pickle can serialize many 
>>> types of objects that json can't.  The other side of the coin is that 
>>> pickle is python-specific, so if you think you'll ever need to read your 
>>> data from other languages, pickle is right out.
>>
>> That is not entirely true :)  I've written a pickle implementation for Java 
>> and .NET
>> that is almost feature complete; it is part of 
>> http://pythonhosted.org/Pyro4/pyrolite.html
> 
> Very cool
> 
>> Still, pickle may not be the best choice here.
> 
> Perhaps not, but lots of points for the awesomeness factor.
> 

Thanks for the praise :)

There's another interesting thing perhaps to also mention about Pyrolite. Its Pickle
implementation obviously maps built in types to their counterparts in Java/.NET, but it
is also capable of unpickling custom classes. It defaults to outputting a simple
hashtable with the class's properties in it, but you can also provide a custom
deserializer to recreate a custom object (it already does this automatically for a few
complex types such as DateTime, BigDecimal, and a bunch of Pyro specific types).

As the unpicklers are not executing any Java or .NET code dynamically they are not
susceptible to the security issue that Python's pickle has. Still: tread with care.
(Especially if you use the lib to pickle stuff and unpickle it on the Python side).

Also, the one missing feature is memo-ing when pickling so that you can pickle recursive
object graphs. It now throws a stack overflow exception instead.


Irmen

[toc] | [prev] | [next] | [standalone]


#46596

FromSchneider <js@globe.de>
Date2013-05-31 13:21 +0200
Message-ID<mailman.2488.1369999368.3114.python-list@python.org>
In reply to#46117
On 26.05.2013 22:48, Roy Smith wrote:
> In article <mailman.2197.1369600623.3114.python-list@python.org>,
>   Chris Rebert <clp2@rebertia.com> wrote:
>
>> On May 23, 2013 3:42 AM, "Schneider" <js@globe.de> wrote:
>>> Hi list,
>>>
>>> how can I serialize a python class to XML? Plus a way to get the class
>> back from the XML?
>>
>> There's pyxser: http://pythonhosted.org/pyxser/
>>
>>> My aim is to store instances of this class in a database.
>> Honestly, I would avoid XML if you can. Consider using JSON (Python
>> includes the `json` module in the std lib) or pickle instead. Compared to
>> XML: The former is more standardized (in the context of serializing
>> objects) and less verbose; the latter is more efficient (if you don't care
>> about cross-language accessibility); both have more convenient APIs.
> Some other points...
>
> If you care about efficiency and want to use json, don't use the one
> that comes packaged with the standard library.  There are lots of
> third-party json packages (ujson is the one we use) which are
> significantly faster.  Not sure if that's true of the newest python
> releases, but it was certainly true in 2.6.

I think performance can be a problem in future. This question is part of 
a multi-user rss-reader solution, which I'm going to develop.

I want to store the feed entries (+ some additional data) as XML in a 
database.

> The advantage of pickle over json is that pickle can serialize many
> types of objects that json can't.  The other side of the coin is that
> pickle is python-specific, so if you think you'll ever need to read your
> data from other languages, pickle is right out.


-- 
GLOBE Development GmbH
Königsberger Strasse 260
48157 MünsterGLOBE Development GmbH
Königsberger Strasse 260
48157 Münster
0251/5205 390

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web