durusmail: quixote-users: Using dulcinea.specified for type information
Using dulcinea.specified for type information
2003-06-05
Using dulcinea.specified for type information
A.M. Kuchling
2003-06-05
I'm confronted with the task of writing some code that will parse some
XML and create
a bunch of Python objects. e.g.:

  
     WackyName
     45
     ...
   

There are 35 classes, all with different sets of attributes, so I'd like
to parse all these files without writing reams of parsing code for each
class.  I'm thinking of using dulcinea.specified to define all the
expected attributes -- each class would subclass Specified -- and then
use that information to convert strings to integers (or whatever) as
needed.  The only missing piece would be a convert_value(text, spec)
which took the string 'text' and converted it to whatever Python type
defined by 'spec'.  Considering that it probably won't have to handle
sequence types, convert_value() shouldn't be too difficult to write.

Does this approach seem reasonable?  Is there anything in
dulcinea.specified that would make it a bad idea?  Performance might be
one reason, but 'specified' seems to stay nicely out of the way
until you actually want to check an object against the specification.

Hmm... while Googling this, I just turned up
http://gnosis.cx/publish/programming/xml_matters_20.txt, which describes
an approach that resembles
dulcinea.specified quite a bit.  Maybe this idea isn't as insane as I
thought.

--amk



reply