Dynamic Class Creation

Gerard Flanagan grflanagan at gmail.com
Wed Mar 17 09:54:14 CET 2010

Josh English wrote:
> Chris,
> Thanks. This worked for the attributes, but I think the tactic is
> still misleading. There are child elements I can't quite determine how
> to deal with:
> <market code='anlg' tier='ProMarket' mail='True'>
>     <title field="pref">Analog Science Fiction and Fact</title>
>     <nickname>Analog</nickname>
>     <keyword>Science Fiction</keyword>
>     <keyword>First Contact</keyword>
>     <keyword>Hard Science Fiction</keyword>
>     <address>
>         <attnline>Stanley Schmidt, Editor</attnline>
>         <address1>267 Broadway, 4th Floor</address1>
>         <address2>New York, NY 10007-2352</address2>
>     </address>
>     <website>http://www.analogsf.com</website>
> </market>
> A child element with text and an attribute or two, for example, pose a
> problem. I can call Market.title but should I try Market.title.field
> or Market.title_field.
> Multiple elements, such as keywords, are allowed in xml but harder to
> map to the object. I don't know if I want to go create a list and
> methods for accessing those keywords as a list, or redefine the rules
> of my XML to not allow multiple child elements with the same tag. I
> can't decide.
> Then the address is a bit of a bear.
> In short, I have to figure out the whole object interface to the XML
> and how I want that to work.

Have you heard of or considered PyXB (http://pyxb.sourceforge.net/)? Not 
that I've much experience with it, but you can use it to generate python 
code from XML Schema files, and, I think, WSDL definitions. It's a bit 
of a rabbit hole, but then isn't that half the fun!

The following might give you the idea. You start with a Schema 
schema.xsd, generate a python module from it, then read in some XML with 
the generated classes to get your custom objects.

---------------- schema.xsd ----------------------

<xs:schema targetNamespace="http://www.josh.com/schema/0.1"

     <xs:element name="market" type="josh:MarketType"/>

     <xs:complexType name="MarketType">
             <xs:element name="title" type="josh:TitleType"/>
             <xs:element name="nickname" type="xs:string"/>
             <xs:element name="keyword" type="xs:string" 
             <xs:element name="address" type="josh:USAddress"/>
             <xs:element name="website" type="xs:string"/>
         <xs:attributeGroup ref="josh:MarketAttributes"/>

     <xs:complexType name="TitleType">
             <xs:extension base="xs:string">
                 <xs:attribute name="field" type="xs:string"/>

     <xs:attributeGroup name="MarketAttributes">
         <xs:attribute name="code" type="xs:string"/>
         <xs:attribute name="tier" type="xs:string"/>
         <xs:attribute name="mail" type="xs:string"/>

     <xs:complexType name="Address" abstract="true">
             <xs:element name="name"   type="xs:string"/>
             <xs:element name="street" type="xs:string"/>
             <xs:element name="city"   type="xs:string"/>

     <xs:complexType name="USAddress">
             <xs:extension base="josh:Address">
                     <xs:element name="state" type="josh:USState" 
                     <xs:element name="zip"   type="xs:positiveInteger" 

     <xs:simpleType name="USState">
         <xs:restriction base="xs:string">
             <xs:enumeration value="AK"/>
             <xs:enumeration value="AL"/>
             <xs:enumeration value="AR"/>
             <!-- and so on ... -->


---------------- genbindings.sh ---------------------------


pyxbgen -u schema.xsd -m market_binding -r

---------------- demo.py -----------------------------------

s = """<?xml version="1.0"?>

     <title field="pref">Analog Science Fiction and Fact</title>
     <keyword>Science Fiction</keyword>
     <keyword>First Contact</keyword>
     <keyword>Hard Science Fiction</keyword>
         <name>Stanley Schmidt, Editor</name>
         <street>267 Broadway, 4th Floor</street>
         <city>New York</city>

import xml.dom.minidom
import market_binding

market = 

print market.tier
print market.code
print market.keyword
assert len(market.keyword) == 3
print market.address.name
print market.toxml()

More information about the Python-list mailing list