[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

XML pickle

Aaron Brady

2/13/2008 9:44:00 PM

Readability of the Pickle module. Can one export to XML, from cost of
speed and size, to benefit of user-readability?

It does something else: plus functions do not export their code,
either in interpreter instructions, or source, or anything else; and
classes do not export their dictionaries, just their names. But it
does export in ASCII.

Pickle checks any __safe_for_unpickling__ and __setstate__ methods,
which enable a little encapsulating, but don't go far.

At the other end of the spectrum, there is an externally-readable
datafile:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook
xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet ss:Name="Sheet1">
<Table>
<Row>
<Cell><Data ss:Type="String">abc</Data></Cell>
<Cell><Data ss:Type="Number">123</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>

Classes can be arranged to mimic this hierarchy:

class XMLable:
def __init__( self, **kwar ):
self.attrs= kwar
class Workbook( XMLable ):
cattrs= {
'xmlns': "urn:schemas-microsoft-com:office:spreadsheet",
'xmlns:ss': "urn:schemas-microsoft-com:office:spreadsheet" }
class Worksheet( XMLable ):
cattrs= { 'name': 'ss:Name' }
class Table( XMLable ): pass
class Row( XMLable ): pass
class Cell( XMLable ): pass
class Data( XMLable ):
cattrs= { 'type': 'ss:Type' }

data= Data( content= 'abc', type= 'String' )
cell= Cell( data= data )
row= Row( cells= [ cell ] )
table= Table( rows= [ row ] )
sheet= Worksheet( table= table, name= "Sheet1" )
book= Workbook( sheets= [ sheet ] )

(These might make things cleaner, but are not allowed:

#data= Data( 'abc', 'ss:Type'= 'String' )
#sheet= Worksheet( table= table, 'ss:Name'= "Sheet1" )

For keys can only be identifiers in keyword argument syntax.)

How close to this end can the standard library come? Is it more
prevalent than something else that's currently in it? What does the
recipie look like to convert this to XML, either using import xml or
not?

import pickle
print( pickle.dumps( book ) )

is not quite what I have in mind.

I guess I'm not convinced that 'is currently in use' has always been
or even is the standard by which standard library additions are
judged. If it's not, then I hold that XML is a good direction to go.
Will core developers listen to reason? Does +1 = +1?
21 Answers

George Sakkis

2/14/2008 4:41:00 AM

0

On Feb 13, 4:43 pm, castiro...@gmail.com wrote:

> Readability of the Pickle module. Can one export to XML, from cost
> of speed and size, to benefit of user-readability?

Take a look at gnosis.xml.pickle, it seems a good starting point.

George

Aaron Brady

2/14/2008 5:39:00 AM

0

On Feb 13, 10:41 pm, George Sakkis <george.sak...@gmail.com> wrote:
> On Feb 13, 4:43 pm, castiro...@gmail.com wrote:
>
> > Readability of the Pickle module.  Can one export to XML, from cost
> > of speed and size, to benefit of user-readability?
>
> Take a look at gnosis.xml.pickle, it seems a good starting point.
>
> George

The way the OP specifies it, dumps-loads pairs are broken: say if
Table and Worksheet are defined in different modules. He'd have to
have some kind of unifying pair sequence, that says that "Worksheet"
document elements come from WS.py, etc.

Stefan Behnel

2/14/2008 6:46:00 AM

0

Hi,

castironpi@gmail.com wrote:
> Readability of the Pickle module. Can one export to XML, from cost of
> speed and size, to benefit of user-readability?

Regarding pickling to XML, lxml.objectify can do that:

http://codespeak.net/lxml/obje...

however:

> It does something else: plus functions do not export their code,
> either in interpreter instructions, or source, or anything else; and
> classes do not export their dictionaries, just their names. But it
> does export in ASCII.
>
> Pickle checks any __safe_for_unpickling__ and __setstate__ methods,
> which enable a little encapsulating, but don't go far.

I'm having a hard time to understand what you are trying to achieve. Could you
state that in a few words? That's usually better than asking for a way to do X
with Y. Y (i.e. pickling in this case) might not be the right solution for you.

Stefan

Aaron Brady

2/14/2008 6:12:00 PM

0

On Feb 14, 12:45 am, Stefan Behnel <stefan...@behnel.de> wrote:
> Hi,
>
> castiro...@gmail.com wrote:
> > Readability of the Pickle module.  Can one export to XML, from cost of
> > speed and size, to benefit of user-readability?
>
> Regarding pickling to XML, lxml.objectify can do that:
>
> http://codespeak.net/lxml/obje...
>
> however:
>
> > It does something else: plus functions do not export their code,
> > either in interpreter instructions, or source, or anything else; and
> > classes do not export their dictionaries, just their names.  But it
> > does export in ASCII.
>
> > Pickle checks any __safe_for_unpickling__ and __setstate__ methods,
> > which enable a little encapsulating, but don't go far.
>
> I'm having a hard time to understand what you are trying to achieve. Could you
> state that in a few words? That's usually better than asking for a way to do X
> with Y. Y (i.e. pickling in this case) might not be the right solution for you.
>
> Stefan

The example isn't so bad. It's not clear that it isn't already too
specific. Pickling isn't what I want. XML is persistent too.

XML could go a couple ways. You could export source, byte code, and
type objects. (Pickle could do that too, thence the confusion
originally.)

gnosis.xml and lxml have slightly different outputs. What I'm going
for has been approached a few different times a few different ways
already. If all I want is an Excel-readable file, that's one end of
the spectrum. If you want something more general, but still include
Excel, that's one of many decisions to make. Ideas.

How does lxml export: b= B(); a.b= b; dumps( a )?

It looks like he can create the XML from the objects already.

Stefan Behnel

2/14/2008 6:31:00 PM

0

castironpi@gmail.com wrote:
> On Feb 14, 12:45 am, Stefan Behnel <stefan...@behnel.de> wrote:
>> castiro...@gmail.com wrote:
>>> Readability of the Pickle module. Can one export to XML, from cost of
>>> speed and size, to benefit of user-readability?
>> Regarding pickling to XML, lxml.objectify can do that:
>>
>> http://codespeak.net/lxml/obje...
>>
>> however:
>>
>>> It does something else: plus functions do not export their code,
>>> either in interpreter instructions, or source, or anything else; and
>>> classes do not export their dictionaries, just their names. But it
>>> does export in ASCII.
>>> Pickle checks any __safe_for_unpickling__ and __setstate__ methods,
>>> which enable a little encapsulating, but don't go far.
>> I'm having a hard time to understand what you are trying to achieve. Could you
>> state that in a few words? That's usually better than asking for a way to do X
>> with Y. Y (i.e. pickling in this case) might not be the right solution for you.
>>
>> Stefan
>
> The example isn't so bad. It's not clear that it isn't already too
> specific. Pickling isn't what I want. XML is persistent too.
>
> XML could go a couple ways. You could export source, byte code, and
> type objects. (Pickle could do that too, thence the confusion
> originally.)

What I meant was: please state what you are trying to do. What you describe
are the environmental conditions and possible solutions that you are thinking
of, but it doesn't tell me what problem you are actually trying to solve.


> gnosis.xml and lxml have slightly different outputs. What I'm going
> for has been approached a few different times a few different ways
> already. If all I want is an Excel-readable file, that's one end of
> the spectrum. If you want something more general, but still include
> Excel, that's one of many decisions to make. Ideas.
>
> How does lxml export: b= B(); a.b= b; dumps( a )?
>
> It looks like he can create the XML from the objects already.

In lxml.objectify, the objects *are* the XML tree. It's all about objects
being bound to specific elements in the tree.

Stefan

Aaron Brady

2/14/2008 7:08:00 PM

0

On Feb 14, 12:31 pm, Stefan Behnel <stefan...@behnel.de> wrote:
> castiro...@gmail.com wrote:
> > On Feb 14, 12:45 am, Stefan Behnel <stefan...@behnel.de> wrote:
> >> castiro...@gmail.com wrote:
> >>> Readability of the Pickle module.  Can one export to XML, from cost of
> >>> speed and size, to benefit of user-readability?
> >> Regarding pickling to XML, lxml.objectify can do that:
>
> >>http://codespeak.net/lxml/obje...
>
> >> however:
>
> >>> It does something else: plus functions do not export their code,
> >>> either in interpreter instructions, or source, or anything else; and
> >>> classes do not export their dictionaries, just their names.  But it
> >>> does export in ASCII.
> >>> Pickle checks any __safe_for_unpickling__ and __setstate__ methods,
> >>> which enable a little encapsulating, but don't go far.
> >> I'm having a hard time to understand what you are trying to achieve. Could you
> >> state that in a few words? That's usually better than asking for a way to do X
> >> with Y. Y (i.e. pickling in this case) might not be the right solution for you.
>
> >> Stefan
>
> > The example isn't so bad.  It's not clear that it isn't already too
> > specific.  Pickling isn't what I want.  XML is persistent too.
>
> > XML could go a couple ways.  You could export source, byte code, and
> > type objects.  (Pickle could do that too, thence the confusion
> > originally.)
>
> What I meant was: please state what you are trying to do. What you describe
> are the environmental conditions and possible solutions that you are thinking
> of, but it doesn't tell me what problem you are actually trying to solve.

What problem -am- I trying to solve? Map the structure -in- to XML.

> > gnosis.xml and lxml have slightly different outputs.  What I'm going
> > for has been approached a few different times a few different ways
> > already.  If all I want is an Excel-readable file, that's one end of
> > the spectrum.  If you want something more general, but still include
> > Excel, that's one of many decisions to make.  Ideas.
>
> > How does lxml export: b= B(); a.b= b; dumps( a )?
>
> > It looks like he can create the XML from the objects already.
>
> In lxml.objectify, the objects *are* the XML tree. It's all about objects
> being bound to specific elements in the tree.
>
> Stefan- Hide quoted text -
>
> - Show quoted text -

Objects first. Create. The use case is a simulated strategy
tournament.

Stefan Behnel

2/14/2008 7:49:00 PM

0

Hi,

castironpi@gmail.com wrote:
> Stefan Behnel wrote:
>> What I meant was: please state what you are trying to do. What you describe
>> are the environmental conditions and possible solutions that you are
>> thinking of, but it doesn't tell me what problem you are actually trying
>> to solve.

http://catb.org/~esr/faqs/smart-questions...

> What problem -am- I trying to solve? Map the structure -in- to XML.

http://catb.org/~esr/faqs/smart-questions.html...

Is it a fixed structure you have, or are you free to use whatever you like?


> Objects first. Create.

http://catb.org/~esr/faqs/smart-questions.html...

My guess is that this is supposed to mean: "I want to create Python objects
and then write their structure out as XML". Is that the right translation?

There are many ways to do so, one is to follow these steps:

http://codespeak.net/lxml/objectify.html#tree-generation-with-the...
http://codespeak.net/lxml/objectify.html#element-access-through-object-...
http://codespeak.net/lxml/objectify.html#python-...
then maybe this:
http://codespeak.net/lxml/objectify.html#defining-additional-da...
and finally this:
http://codespeak.net/lxml/tutorial.html#ser...

But as I do not know enough about the problem you are trying to solve, except:

> The use case is a simulated strategy tournament.

I cannot tell if the above approach will solve your problem or not.

Stefan

Aaron Brady

2/14/2008 11:32:00 PM

0

On Feb 14, 1:49 pm, Stefan Behnel <stefan...@behnel.de> wrote:
> Hi,
>
> castiro...@gmail.com wrote:
> > Stefan Behnel wrote:
> >> What I meant was: please state what you are trying to do. What you describe
> >> are the environmental conditions and possible solutions that you are
> >> thinking of, but it doesn't tell me what problem you are actually trying
> >> to solve.
>
> http://catb.org/~esr/faqs/smart-questions...
>
> > What problem -am- I trying to solve?  Map the structure -in- to XML.
>
> http://catb.org/~esr/faqs/smart-questions.html...
>
> Is it a fixed structure you have, or are you free to use whatever you like?
>
> > Objects first.  Create.
>
> http://catb.org/~esr/faqs/smart-questions.html...
>
> My guess is that this is supposed to mean: "I want to create Python objects
> and then write their structure out as XML". Is that the right translation?
>
> There are many ways to do so, one is to follow these steps:
>
> http://codespeak.net/lxml/objectify.html#tree-generation-with-the-e-f...http://codespeak.net/lxml/objectify.html#element-access-through-objec...http://codespeak.net/lxml/objectify.html#python-...
> then maybe this:http://codespeak.net/lxml/objectify.html#defining-additiona......
> and finally this:http://codespeak.net/lxml/tutorial.html#ser...
>
> But as I do not know enough about the problem you are trying to solve, except:
>
> > The use case is a simulated strategy tournament.
>
> I cannot tell if the above approach will solve your problem or not.
>
> Stefan

I was trying to start a discussion on a cool OO design. Problem's
kind of solved; downer, huh?

I haven't completed it, but it's a start. I expect I'll post some
thoughts along with progress. Will Excel read it? We'll see.

A design difference:

Worksheet= lambda parent: etree.SubElement( parent, "Worksheet" )
Table= lambda parent: etree.SubElement( parent, "Table" )
sheet= Worksheet( book ) #parent
table= Table( sheet )
vs.

table= Table() #empty table
sheet= Worksheet( table= table ) #child

I want to call sheet.table sometimes. Is there a lxml equivalent?

Aaron Brady

2/15/2008 12:09:00 AM

0

On Feb 14, 5:31 pm, castiro...@gmail.com wrote:
> On Feb 14, 1:49 pm, Stefan Behnel <stefan...@behnel.de> wrote:
>
>
>
>
>
> > Hi,
>
> > castiro...@gmail.com wrote:
> > > Stefan Behnel wrote:
> > >> What I meant was: please state what you are trying to do. What you describe
> > >> are the environmental conditions and possible solutions that you are
> > >> thinking of, but it doesn't tell me what problem you are actually trying
> > >> to solve.
>
> >http://catb.org/~esr/faqs/smart-questions...
>
> > > What problem -am- I trying to solve?  Map the structure -in- to XML.
>
> >http://catb.org/~esr/faqs/smart-questions.html...
>
> > Is it a fixed structure you have, or are you free to use whatever you like?
>
> > > Objects first.  Create.
>
> >http://catb.org/~esr/faqs/smart-questions.html...
>
> > My guess is that this is supposed to mean: "I want to create Python objects
> > and then write their structure out as XML". Is that the right translation?
>
> > There are many ways to do so, one is to follow these steps:
>
> >http://codespeak.net/lxml/objectify.html#tree-generation-wi......
> > then maybe this:http://codespeak.net/lxml/objectify.html#defining-additiona......
> > and finally this:http://codespeak.net/lxml/tutorial.html#ser...
>
> > But as I do not know enough about the problem you are trying to solve, except:
>
> > > The use case is a simulated strategy tournament.
>
> > I cannot tell if the above approach will solve your problem or not.
>
> > Stefan
>
> I was trying to start a discussion on a cool OO design.  Problem's
> kind of solved; downer, huh?
>
> I haven't completed it, but it's a start.  I expect I'll post some
> thoughts along with progress.  Will Excel read it?  We'll see.
>
> A design difference:
>
> Worksheet= lambda parent: etree.SubElement( parent, "Worksheet" )
> Table= lambda parent: etree.SubElement( parent, "Table" )
> sheet= Worksheet( book ) #parent
> table= Table( sheet )
> vs.
>
> table= Table() #empty table
> sheet= Worksheet( table= table ) #child
>
> I want to call sheet.table sometimes.  Is there a lxml equivalent?- Hide quoted text -
>
> - Show quoted text -

Minimize redundancy. Are there some possibilities ignored, such as
reading a class structure from an existing Excel XML file, downloading
the official spec, and if one is coding in Windows, how bulky is the
equiavelent COM code? One doesn't want to be re-coding the "wheel" if
it's big and hairy.

Ben Finney

2/15/2008 12:14:00 AM

0

castironpi@gmail.com writes:

> Minimize redundancy.

Please do so by trimming the quoted material; remove anything not
relevant to people reading your reply.

--
\ "I moved into an all-electric house. I forgot and left the |
`\ porch light on all day. When I got home the front door wouldn't |
_o__) open." -- Steven Wright |
Ben Finney