Re: XML pickle

2008-02-18 Thread castironpi
 from lxml import etree

 class XMLable:
         cname= ''
         Text= object()
         class CTor:
                 def __init__( self, *ar ):
                         self.ar, self.kwar= ar, dict( ar )
         ctor= CTor()
         FTor= dict
         ftor= {}
         def __init__( self, par= None, *ar, **kwar ):
                 nsmap= kwar.pop( 'nsmap', None )
                 if par is None:
                         self.node= etree.Element( self.cname or 
 self.__class__.__name__,
 nsmap= nsmap )
                 else:
                         self.node= etree.SubElement( par.node, self.cname or
 self.__class__.__name__, nsmap= nsmap )
                 for a, ca in zip( ar, self.ctor.ar ):
                         if ca[0] in self.ftor:
                                 a= self.ftor[ ca[0] ]( a )
                         if ca[1] is XMLable.Text:
                                 self.node.text= a
                         else:
                                 self.node.set( ca[1], a )
                 for k, v in kwar.items():
                         if k in self.ftor:
                                 v= self.ftor[ k ]( v )
                         if self.ctor.kwar[ k ] is XMLable.Text:
                                 self.node.text= v
                         else:
                                 self.node.set( self.ctor.kwar[ k ], str( v ) )

 SS= '{urn:schemas-microsoft-com:office:spreadsheet}'
 X= '{urn:schemas-microsoft-com:office:excel}'

 class Workbook( XMLable ):
         #jtor= JTor( 'xmlns', req= 'urn:schemas-microsoft-
 com:office:spreadsheet' )
         def __init__( self ):
                 nns= { 'x': 'urn:schemas-microsoft-com:office:excel',
                         'ss': 'urn:schemas-microsoft-com:office:spreadsheet' }
                 XMLable.__init__( self, nsmap= nns )
                 self.node.set( 'xmlns', 'urn:schemas-microsoft-
 com:office:spreadsheet' )
                 self.styles= Styles( self )
 class Worksheet( XMLable ):
         ctor= XMLable.CTor( ( 'name', SS+ 'Name' ) )
 class Table( XMLable ): pass
 class Row( XMLable ):
         ctor= XMLable.CTor( ( 'index', SS+ 'Index' ) )
 class Cell( XMLable ):
         ctor= XMLable.CTor( ( 'index', SS+ 'Index' ), ( 'style', SS+
 'StyleID' ) )
         ftor= XMLable.FTor( { 'style': lambda x: x.styleid } )
 class Data( XMLable ):
         ctor= XMLable.CTor( ( 'type', SS+ 'Type' ), ( 'data',
 XMLable.Text ) )
 class Styles( XMLable ): pass
 class Font( XMLable ):
         #jtor= JTor( 'family', X+ 'Family', req='Swiss' ), Jtor( 'bold', SS+
 'Bold', lambda x: str( int( x ) ) )
         ctor= XMLable.CTor( ( 'family', X+ 'Family' ), ( 'bold', SS+
 'Bold' ) )
         ftor= XMLable.FTor( { 'bold': lambda x: str( int( x ) ) } )
 class Style( XMLable ):
         styles= {}
         ctor= XMLable.CTor( ( 'styleid', SS+ 'ID' ) )
         def __init__( self, par= None, *ar, **kwar ):
                 self.styleid= 's%i'% ( 21+ len( Style.styles ) )
                 Style.styles[ self.styleid ]= self
                 XMLable.__init__( self, par.styles, self.styleid )
                 Font( self, *ar, **kwar )

 book= Workbook()
 sheet= Worksheet( book, 'WSheet1' )
 table= Table( sheet )
 row= Row( table, index= '2' )
 style= Style( book, 'Swiss', True )
 celli= Cell( row, style= style )
 datai= Data( celli, 'Number', '123' )
 cellj= Cell( row, index= 3 )
 dataj= Data( cellj, 'String', 'abc' )

 out= etree.tostring( book.node, pretty_print= True,
 xml_declaration=True )
 print( out )
 open( 'xl.xml', 'w' ).write( out )
 new= etree.XML( out )
 etree.XML( etree.tostring( book.node ) )
 out= etree.tostring( new, pretty_print= True, xml_declaration=True )
 print( out )

This is one way of eliminating a particular redundancy that showed up
in the first implementation.  I've already excluded some potential
uses of XMLable.  I've, in other words, made assumptions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-17 Thread castironpi
  Which xmlns:ns1 gets redefined because I just didn't figure out how
  get xmlns:ns0 definition into the Workbook tag.  But too bad for me.

 What about actually *reading* the links I post?

 http://codespeak.net/lxml/tutorial.html#the-e-factory

 Hint: look out for the nsmap keyword argument.

That solved the problem.  nsmap doesn't set the root xmlns, but el.set
does.  So that cleared up the [hugevariable] gripe.

Revision separates code from data.

def Cell( p, index= None, styleid= None ):
el= etree.SubElement( p, 'Cell' )
if index is not None: el.set( SS+ 'Index', index )
if styleid is not None: el.set( SS+ 'StyleID', styleid )
return el

becomes

class Cell( XMLable ):
ctor= XMLable.CTor( ( 'index', SS+ 'Index' ), ( 'style', SS+
'StyleID' ) )
ftor= XMLable.FTor( { 'style': lambda x: x.styleid } )

29 lines, loosely packed, define base class; 29 define the subclasses
Workbook, Worksheet, c., also loose.

class Data( XMLable ):
ctor= XMLable.CTor( ( 'type', SS+ 'Type' ), ( 'data',
XMLable.Text ) )

XMLable.Text pseudo-flag indicates to call node.data= X where
node.set( tag, X ) is called.  Tag present in XMLable.ftor indicates
to call node.set( tag, ftor[tag]( X ) ).

class Font( XMLable ):
#jtor= JTor( 'family', X+ 'Family', req='Swiss' ), JTor( 'bold', SS+
'Bold', lambda x: str( int( x ) ) )
ctor= XMLable.CTor( ( 'family', X+ 'Family' ), ( 'bold', SS+
'Bold' ) )
ftor= XMLable.FTor( { 'bold': lambda x: str( int( x ) ) } )

JTor combines CTor and FTor allowing for extension, separating data
into data structure, but is developing.  One could even put that spec
in XML!

Yes: in the example, the base class + derivatives comprise more code,
29 + 29 = 58 lines over the earlier 46.  Actual construction underwent
a slight adjustment, still 9 lines.  Hearing arguments on payoff and
extensibility.

Full implementation present; remove in replies.

Aside, in the C++ equivalent, each XMLable derivative has a class-
static list of JTor derivatives, static JTor* specs[];, the
population of which is declared and initialized globally, and a
virtual JTor* GetSpec( int i ) { return specs[ i ]; }
implementation.  CMIIW, correct me if I'm wrong; +1 on Python.

The for-statements in XMLable.__init__ could conflate; parameter
extraction, which parameters may be specified by place or keyword, is
unclear.  Is if ca[1] is XMLable.Text: a special case, and if so, is
it handled correctly?  Ought JTor to contain a visit method to the
end, which calls node.set in the base class?  It leads to redundancy,
but code is code.  Thence comes the moral, 'No functions in
constructors', and if ca[1] is XMLable.Text: is not a callback.
Aside, what is?



from lxml import etree

class XMLable:
cname= ''
Text= object()
class CTor:
def __init__( self, *ar ):
self.ar, self.kwar= ar, dict( ar )
ctor= CTor()
FTor= dict
ftor= {}
def __init__( self, par= None, *ar, **kwar ):
nsmap= kwar.pop( 'nsmap', None )
if par is None:
self.node= etree.Element( self.cname or 
self.__class__.__name__,
nsmap= nsmap )
else:
self.node= etree.SubElement( par.node, self.cname or
self.__class__.__name__, nsmap= nsmap )
for a, ca in zip( ar, self.ctor.ar ):
if ca[0] in self.ftor:
a= self.ftor[ ca[0] ]( a )
if ca[1] is XMLable.Text:
self.node.text= a
else:
self.node.set( ca[1], a )
for k, v in kwar.items():
if k in self.ftor:
v= self.ftor[ k ]( v )
if self.ctor.kwar[ k ] is XMLable.Text:
self.node.text= v
else:
self.node.set( self.ctor.kwar[ k ], str( v ) )

SS= '{urn:schemas-microsoft-com:office:spreadsheet}'
X= '{urn:schemas-microsoft-com:office:excel}'

class Workbook( XMLable ):
#jtor= JTor( 'xmlns', req= 'urn:schemas-microsoft-
com:office:spreadsheet' )
def __init__( self ):
nns= { 'x': 'urn:schemas-microsoft-com:office:excel',
'ss': 'urn:schemas-microsoft-com:office:spreadsheet' }
XMLable.__init__( self, nsmap= nns )
self.node.set( 'xmlns', 'urn:schemas-microsoft-
com:office:spreadsheet' )
self.styles= Styles( self )
class Worksheet( XMLable ):
ctor= XMLable.CTor( ( 'name', SS+ 'Name' ) )
class Table( XMLable ): pass
class Row( XMLable ):
ctor= XMLable.CTor( ( 'index', SS+ 'Index' ) )
class Cell( XMLable ):
ctor= XMLable.CTor( ( 'index', SS+ 'Index' ), ( 'style', SS+
'StyleID' ) )
ftor= XMLable.FTor( { 

Re: XML pickle

2008-02-15 Thread Stefan Behnel
[EMAIL PROTECTED] wrote:
 I cannot tell if the above approach will solve your problem or not.
 
 Well, declare me a persistent object.

Ok, from now on, you are a persistent object. :)


 from lxml import etree
 
 SS= '{urn:schemas-microsoft-com:office:spreadsheet}'
 book= etree.Element( 'Workbook' )
 book.set( 'xmlns', 'urn:schemas-microsoft-com:office:spreadsheet' )
 sheet= etree.SubElement(book, Worksheet)
 sheet.set( SS+ 'Name', 'WSheet1' )
 table= etree.SubElement(sheet, Table)
 row= etree.SubElement(table, Row)
 cell1= etree.SubElement(row, Cell)
 data1= etree.SubElement(cell1, Data )
 data1.set( SS+ 'Type', Number )
 data1.text= '123'
 cell2= etree.SubElement(row, Cell)
 data2= etree.SubElement(cell2, Data )
 data2.set( SS+ 'Type', String )
 data2.text= 'abc'
 out= etree.tostring( book, pretty_print= True, xml_declaration=True )
 print( out )
 open( 'xl.xml', 'w' ).write( out )

http://codespeak.net/lxml/tutorial.html#namespaces
http://codespeak.net/lxml/tutorial.html#the-e-factory
http://codespeak.net/lxml/objectify.html#tree-generation-with-the-e-factory


 Can you use set( '{ss}Type' ) somehow?

What is 'ss' here? A prefix?

What about actually reading the tutorial?

http://codespeak.net/lxml/tutorial.html#namespaces


 And any way to make this look
 closer to the original?

What's the difference you experience?

Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-15 Thread castironpi
On Feb 15, 12:07 pm, [EMAIL PROTECTED] wrote:
 On Feb 15, 11:10 am, [EMAIL PROTECTED] wrote:

Can you use set( '{ss}Type' ) somehow?

   What is 'ss' here? A prefix?

   What about actually reading the tutorial?

  http://codespeak.net/lxml/tutorial.html#namespaces

And any way to make this look
closer to the original?

   What's the difference you experience?

 Something else that crept up is:

 ?xml version='1.0' encoding='ASCII'?
 Workbook xmlns=[hugethingA]
   Worksheet xmlns:ns0=[hugethingA] ns0:name=WSheet1
   /Worksheet
   Styles
     Style xmlns:ns1=[hugethingA] ns1:ID=s21/
   /Styles
 /Workbook

 Which xmlns:ns1 gets redefined because I just didn't figure out how
 get xmlns:ns0 definition into the Workbook tag.  But too bad for me.

In Economics, they call it Economy to Scale- the effect, and the
point, and past it, where the cost to produce N goods on a supply
curve on which 0 goods costs 0 exceeds that on one on which 0 goods
costs more than 0: the opposite of diminishing returns.  Does the
benefit of encapsulating the specifics of the XML file, including the
practice, exceed the cost of it?

For an only slightly more complex result, the encapsulated version is
presented; and the hand-coded, unencapsulated one is left as an
exercise to the reader.

book= Workbook()
sheet= Worksheet( book, 'WSheet1' )
table= Table( sheet )
row= Row( table, index= '2' )
style= Style( book, bold= True )
celli= Cell( row, styleid= style )
datai= Data( celli, 'Number', '123' )
cellj= Cell( row )
dataj= Data( cellj, 'String', 'abc' )

46 lines of infrastructure, moderately packed.  Note that:

etree.XML( etree.tostring( book ) )

succeeds.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-15 Thread castironpi
  Can you use set( '{ss}Type' ) somehow?

 What is 'ss' here? A prefix?

 What about actually reading the tutorial?

 http://codespeak.net/lxml/tutorial.html#namespaces

  And any way to make this look
  closer to the original?

 What's the difference you experience?

Target:
?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
CellData ss:Type=Stringabc/Data/Cell
CellData ss:Type=Number123/Data/Cell
   /Row
  /Table
 /Worksheet
/Workbook

It helped get me the working one, actually-- the tutorial.  'ss' is,
and I don't know the jargon for it, a local variable, or namespace
variable, prefix?, or something. xmlns:ss=urn:schemas-microsoft-
com:office:spreadsheet.  The ElementMaker example is closest, I
think, but it's working, so, ...

I'm more interested in a simplification of the construction code, and
at this point I can get goofy and brainstorm.  Ideas?




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-15 Thread castironpi
 In Economics, they call it Economy to Scale- the effect, and the
 point, and past it, where the cost to produce N goods on a supply
 curve on which 0 goods costs 0 exceeds that on one on which 0 goods
 costs more than 0: the opposite of diminishing returns.  Does the
 benefit of encapsulating the specifics of the XML file, including the
 practice, exceed the cost of it?

And for all the management out there, yes.  As soon as possible does
mean as crappy as possible.  Extra is extra.  Assume the sooner the
crappier and the theorem follows.  (Now, corroborate the premise...)

P.S.  Gluttony is American too.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-15 Thread castironpi
On Feb 15, 11:10 am, [EMAIL PROTECTED] wrote:
   Can you use set( '{ss}Type' ) somehow?

  What is 'ss' here? A prefix?

  What about actually reading the tutorial?

 http://codespeak.net/lxml/tutorial.html#namespaces

   And any way to make this look
   closer to the original?

  What's the difference you experience?

Something else that crept up is:

?xml version='1.0' encoding='ASCII'?
Workbook xmlns=[hugethingA]
  Worksheet xmlns:ns0=[hugethingA] ns0:name=WSheet1
  /Worksheet
  Styles
Style xmlns:ns1=[hugethingA] ns1:ID=s21/
  /Styles
/Workbook

Which xmlns:ns1 gets redefined because I just didn't figure out how
get xmlns:ns0 definition into the Workbook tag.  But too bad for me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-15 Thread castironpi
On Feb 15, 2:58 pm, [EMAIL PROTECTED] wrote:
  In Economics, they call it Economy to Scale- the effect, and the
  point, and past it, where the cost to produce N goods on a supply
  curve on which 0 goods costs 0 exceeds that on one on which 0 goods
  costs more than 0: the opposite of diminishing returns.  Does the
  benefit of encapsulating the specifics of the XML file, including the
  practice, exceed the cost of it?

 And for all the management out there, yes.  As soon as possible does
 mean as crappy as possible.  Extra is extra.  Assume the sooner the
 crappier and the theorem follows.  (Now, corroborate the premise...)

The sooner the crappier or the parties waste time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-15 Thread Stefan Behnel
[EMAIL PROTECTED] wrote:
 On Feb 15, 11:10 am, [EMAIL PROTECTED] wrote:
 Can you use set( '{ss}Type' ) somehow?
 What is 'ss' here? A prefix?
 What about actually reading the tutorial?
 http://codespeak.net/lxml/tutorial.html#namespaces
 And any way to make this look
 closer to the original?
 What's the difference you experience?
 
 Something else that crept up is:
 
 ?xml version='1.0' encoding='ASCII'?
 Workbook xmlns=[hugethingA]
   Worksheet xmlns:ns0=[hugethingA] ns0:name=WSheet1
   /Worksheet
   Styles
 Style xmlns:ns1=[hugethingA] ns1:ID=s21/
   /Styles
 /Workbook
 
 Which xmlns:ns1 gets redefined because I just didn't figure out how
 get xmlns:ns0 definition into the Workbook tag.  But too bad for me.

What about actually *reading* the links I post?

http://codespeak.net/lxml/tutorial.html#the-e-factory

Hint: look out for the nsmap keyword argument.

Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread Stefan Behnel
[EMAIL PROTECTED] wrote:
 On Feb 14, 12:45 am, Stefan Behnel [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] 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/objectify.html

 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread castironpi
On Feb 14, 12:45 am, Stefan Behnel [EMAIL PROTECTED] wrote:
 Hi,

 [EMAIL PROTECTED] 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/objectify.html

 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread castironpi
On Feb 14, 12:31 pm, Stefan Behnel [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
  On Feb 14, 12:45 am, Stefan Behnel [EMAIL PROTECTED] wrote:
  [EMAIL PROTECTED] 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/objectify.html

  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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread Stefan Behnel
Hi,

[EMAIL PROTECTED] 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.html#goal

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

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

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#writewell

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-factory
http://codespeak.net/lxml/objectify.html#element-access-through-object-attributes
http://codespeak.net/lxml/objectify.html#python-data-types
then maybe this:
http://codespeak.net/lxml/objectify.html#defining-additional-data-classes
and finally this:
http://codespeak.net/lxml/tutorial.html#serialisation

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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread castironpi
On Feb 14, 1:49 pm, Stefan Behnel [EMAIL PROTECTED] wrote:
 Hi,

 [EMAIL PROTECTED] 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.html#goal

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

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

 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#writewell

 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-data-types
 then maybe 
 this:http://codespeak.net/lxml/objectify.html#defining-additional-data-cla...
 and finally this:http://codespeak.net/lxml/tutorial.html#serialisation

 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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread castironpi
On Feb 14, 5:31 pm, [EMAIL PROTECTED] wrote:
 On Feb 14, 1:49 pm, Stefan Behnel [EMAIL PROTECTED] wrote:





  Hi,

  [EMAIL PROTECTED] 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.html#goal

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

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

  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#writewell

  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...
  then maybe 
  this:http://codespeak.net/lxml/objectify.html#defining-additional-data-cla...
  and finally this:http://codespeak.net/lxml/tutorial.html#serialisation

  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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread Ben Finney
[EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread castironpi
Great!

--
 \          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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread castironpi
 I cannot tell if the above approach will solve your problem or not.

Well, declare me a persistent object.

from lxml import etree

SS= '{urn:schemas-microsoft-com:office:spreadsheet}'
book= etree.Element( 'Workbook' )
book.set( 'xmlns', 'urn:schemas-microsoft-com:office:spreadsheet' )
sheet= etree.SubElement(book, Worksheet)
sheet.set( SS+ 'Name', 'WSheet1' )
table= etree.SubElement(sheet, Table)
row= etree.SubElement(table, Row)
cell1= etree.SubElement(row, Cell)
data1= etree.SubElement(cell1, Data )
data1.set( SS+ 'Type', Number )
data1.text= '123'
cell2= etree.SubElement(row, Cell)
data2= etree.SubElement(cell2, Data )
data2.set( SS+ 'Type', String )
data2.text= 'abc'
out= etree.tostring( book, pretty_print= True, xml_declaration=True )
print( out )
open( 'xl.xml', 'w' ).write( out )

Can you use set( '{ss}Type' ) somehow?  And any way to make this look
closer to the original?  But it works.

?xml version='1.0' encoding='ASCII'?
Workbook xmlns=urn:schemas-microsoft-com:office:spreadsheet
  Worksheet xmlns:ns0=urn:schemas-microsoft-com:office:spreadsheet
ns0:Name=WSheet1
Table
  Row
Cell
  Data ns0:Type=Number123/Data
/Cell
Cell
  Data ns0:Type=Stringabc/Data
/Cell
  /Row
/Table
  /Worksheet
/Workbook
-- 
http://mail.python.org/mailman/listinfo/python-list


XML pickle

2008-02-13 Thread castironpi
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
CellData ss:Type=Stringabc/Data/Cell
CellData ss:Type=Number123/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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-13 Thread castironpi
On Feb 13, 10:41 pm, George Sakkis [EMAIL PROTECTED] wrote:
 On Feb 13, 4:43 pm, [EMAIL PROTECTED] 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-13 Thread George Sakkis
On Feb 13, 4:43 pm, [EMAIL PROTECTED] 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-13 Thread Stefan Behnel
Hi,

[EMAIL PROTECTED] 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/objectify.html

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
-- 
http://mail.python.org/mailman/listinfo/python-list


XML Pickle with PyGraphLib - Problems

2005-09-05 Thread Mike P.
Hi all,

I'm working on a simulation (can be considered a game) in Python where I
want to be able to dump the simulation state to a file and be able to load
it up later. I have used the standard Python pickle module and it works fine
pickling/unpickling from files.

However, I want to be able to use a third party tool like an XML editor (or
other custom tool) to setup the initial state of the simulation, so I have
been playing around with the gnosis set of tools written by David Mertz. In
particular I have been using the gnosis.xml.pickle module. Anyway, this
seems to work fine for most data structures, however I have problems getting
it to work with pygraphlib version 0.6.0.1 (from sourceforge). My simulation
needs to store data in a set of graphs, and pygraphlib seems like a nice
simple python graph library, that I found easy to use.

Anyway, the problem is that I can successfully pickle to XML a data
structure containing a pygraphlib graph, but I have trouble
reloading/unpickling the very same data structure that was produced by the
XML pickle module. I get an XMLUnpicklingError. Anyone have any ideas?

Here's some output from the interactive prompt of a small example which
demonstrates the error:

==
C:\home\srcpython
ActivePython 2.4 Build 244 (ActiveState Corp.) based on
Python 2.4 (#60, Feb  9 2005, 19:03:27) [MSC v.1310 32 bit (Intel)] on win32
Type help, copyright, credits or license for more information.
 from pygraphlib import pygraph
 graph = pygraph.DGraph()
 graph.add_edge('Melbourne', 'Sydney')
 graph.add_edge('Melbourne', 'Brisbane')
 graph.add_edge('Melbourne', 'Adelaide')
 graph.add_edge('Adelaide', 'Perth')
 print graph
DGraph: 5 nodes, 4 edges
 graph
DGraph: 5 nodes, 4 edges
Melbourne - ['Sydney', 'Brisbane', 'Adelaide']
Brisbane - []
Perth - []
Sydney - []
Adelaide - ['Perth']
 import gnosis.xml.pickle
 file = open('graph.xml', 'w')
 gnosis.xml.pickle.dump(graph, file)
 file.close()
 f2 = open('graph.xml', 'r')
 g2 = gnosis.xml.pickle.load(f2)
Traceback (most recent call last):
  File stdin, line 1, in ?
  File C:\Python24\Lib\site-packages\gnosis\xml\pickle\_pickle.py, line
152, in load
return parser(fh, paranoia=paranoia)
  File C:\Python24\lib\site-packages\gnosis\xml\pickle\parsers\_dom.py,
line 42, in thing_from_dom
return _thing_from_dom(minidom.parse(fh),None,paranoia)
  File C:\Python24\lib\site-packages\gnosis\xml\pickle\parsers\_dom.py,
line 175, in _thing_from_dom
container = unpickle_instance(node, paranoia)
  File C:\Python24\lib\site-packages\gnosis\xml\pickle\parsers\_dom.py,
line 59, in unpickle_instance
raw = _thing_from_dom(node, _EmptyClass(), paranoia)
  File C:\Python24\lib\site-packages\gnosis\xml\pickle\parsers\_dom.py,
line 234, in _thing_from_dom
node_val = unpickle_instance(node, paranoia)
  File C:\Python24\lib\site-packages\gnosis\xml\pickle\parsers\_dom.py,
line 95, in unpickle_instance
raise XMLUnpicklingError, \
gnosis.xml.pickle.XMLUnpicklingError: Non-DictType without setstate violates
pickle protocol.(PARANOIA setting may be too high)

===

I find it strange that:
a) the standard pickle/unpickle works
b) the xml pickle dumps the file but the xml unpickle can't load it.

I'm guessing the problem lies somewhere with implementing __setstate__ and
__getstate__ for pygraphlib (I don't know much about this - haven't used
them before). However, I am a bit reluctant to go in and start playing
around with the internals pygraphlib, as I was hoping to just use it, and
ignore the internal implementation (as you would with any library). Funny
how the standard pickle module doesn't need to do this.

Another thing I tried was the generic xml marshaller (xml.marshal.generic)
that comes with PyXML 0.8.4 (for Python 2.4 on windows).
This also fails but for different reasons. The marshaller doesn't support
the boolean and set types which are part of Python 2.4 and are used in
pygraphlib.
I get errors of the form:

AttributeError: Marshaller instance has no attribute 'tag_bool'
AttributeError: Marshaller instance has no attribute 'm_Set'

Again strange given that bool and sets should be supported in Python 2.4.

Anyway, back to my question - does anyone have any suggestions as to where I
could proceed next? I was hoping to be able to XML Pickle/Unpickle Python
data structures containing graphs from pygraphlib fairly easily without
having to stuff around in the internals of third party libraries.
It would be nice if I could just concentrate on my application logic :-)

Any ideas?

Cheers.

Mike P.



-- 
http://mail.python.org/mailman/listinfo/python-list