Dynamic Class Creation

Gerard Flanagan grflanagan at gmail.com
Wed Mar 17 04:54:14 EDT 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"
         xmlns:xs="http://www.w3.org/2001/XMLSchema"
         xmlns:josh="http://www.josh.com/schema/0.1">


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

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

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

     <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:attributeGroup>

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

     <xs:complexType name="USAddress">
         <xs:complexContent>
             <xs:extension base="josh:Address">
                 <xs:sequence>
                     <xs:element name="state" type="josh:USState" 
minOccurs="0"/>
                     <xs:element name="zip"   type="xs:positiveInteger" 
minOccurs="0"/>
                 </xs:sequence>
             </xs:extension>
         </xs:complexContent>
     </xs:complexType>

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

</xs:schema>

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

#!/bin/sh

pyxbgen -u schema.xsd -m market_binding -r

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

s = """<?xml version="1.0"?>
<josh:market
     xmlns:josh="http://www.josh.com/schema/0.1"
     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>
         <name>Stanley Schmidt, Editor</name>
         <street>267 Broadway, 4th Floor</street>
         <city>New York</city>
     </address>
     <website>http://www.analogsf.com</website>
</josh:market>
"""

import xml.dom.minidom
import market_binding

market = 
market_binding.CreateFromDOM(xml.dom.minidom.parseString(s).documentElement)

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