XML pickle

castironpi at gmail.com castironpi at gmail.com
Wed Feb 13 22:43:30 CET 2008


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?



More information about the Python-list mailing list