Dynamic Class Creation

Aahz aahz at pythoncraft.com
Fri Apr 2 10:32:52 EDT 2010


In article <8d79f0cb-9c5b-4243-8891-a15fb311f652 at z18g2000prh.googlegroups.com>,
Josh English  <joshua.r.english at gmail.com> wrote:
>
><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.

You should always think "LIST!" any time you have the potential for
multiple elements with the same semantic tag.  Whether you do it as a
list for all elements or as a combo dict/list is something you need to
decide; the latter is faster in many ways but is more cumbersome:

Market.title.keyword[1]

(Instance attributes and dict keys are almost trivially convertible.)

You would probably get some mileage out of looking at the ElementTree
implementation.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?  



More information about the Python-list mailing list