Newbie - Recursive calls in class objects...

Eric ewalstad at yahoo.com
Tue Oct 3 16:43:38 EDT 2000


> As an aside, did you know that you can write this line as:
>     for tkey, tvalue in self.__dict__.items()
Very cool.  Thanks.  That makes the code much easier to read...

> Every variable is an object (or, in Python-speak, every name binds to an
> object).
DOH!  It's sinking in...

> I think you mean "isinstance(t[1], CElement)", because t[0] is the key,
> whereas t[1] is the value.
You're right again.  Thanks.  I'll use 'tvalue' in the code...

OK, with Tom's help, I managed to figure out a solution.  Here's the code
followed by a command line example of the output:
### Start Module Code ###
import string

"""The CProject Module contains: CElement, CCZ, CUser, CProject classes"""

class CElement:
    """Base Class for elements in a project"""
    pad = "  "
    def __init__(self):
        self.Name = ""
        self.Type = ""
        self.PadMult = 1 # equals the number of parent nodes this object
will have in the XML structure
        self.XmlTag = self.Type # the node text used to mark the XML node
(i.e., 'User' => <User></User>)

    def toXML(self):
        strXML=""
        strXML = self.pad * self.PadMult + "<" + self.XmlTag + ">\n"
        for tkey, tvalue in self.__dict__.items():
            if isinstance(tvalue, CElement):
                strXML = strXML + tvalue.toXML()
            elif tkey != "PadMult" and tkey != "XmlTag":
                 strXML = strXML + self.pad * (self.PadMult + 1) + "<" +
tkey + ">" + str(tvalue) + "</" + tkey + ">\n"
        strXML=strXML + self.pad * self.PadMult + "</" + self.XmlTag + ">\n"
        return strXML

class CCZ(CElement):
    """Defines Climate Zone objects"""
    def __init__(self):
        CElement.__init__(self)
        self.Name = "New Climate Zone"
        self.Type = "Climate Zone"
        self.City = ""
        self.Zone = 0
        self.PadMult = 1
        self.XmlTag = "CZ"

class CUser(CElement):
    """Defines Users"""
    def __init__(self):
        CElement.__init__(self)
        self.Name = "New User"
        self.Type = "User"
        self.XmlTag = self.Type
        self.Company = ""
        self.Address = ""
        self.City = ""
        self.State = "CA"
        self.Zip = ""
        self.email = ""
        self.Phone = ""
        self.PadMult = 1

class CProject(CElement):
    """Defines a project"""
    def __init__(self):
        CElement.__init__(self)
        self.Name = "New Project"
        self.Type = "Project"
        self.XmlTag = self.Type
        self.Address = ""
        self.City = ""
        self.State = "CA"
        self.Zip = ""
        self.Phone = ""
        self.ConstructionType= ""
        self.CeilHgtAvg = 8
        self.Front = "North"
        self.Stories = 1
        self.CFA = 0.0
        self.DU = 1.0
        self.CZ = CCZ()
        self.User = CUser()
        self.PadMult = 0
### End Module Code ###

### Start Example ###
<module 'CProject' from 'CProject.py'>
>>> P=CProject.CProject()
>>> print P.toXML()
<Project>
  <CZ>
    <Type>Climate Zone</Type>
    <Zone>0</Zone>
    <Name>New Climate Zone</Name>
    <City></City>
  </CZ>
  <Type>Project</Type>
  <DU>1.0</DU>
  <Front>North</Front>
  <ConstructionType></ConstructionType>
  <Address></Address>
  <User>
    <email></email>
    <Type>User</Type>
    <Address></Address>
    <State>CA</State>
    <Phone></Phone>
    <Company></Company>
    <Zip></Zip>
    <Name>New User</Name>
    <City></City>
  </User>
  <Stories>1</Stories>
  <State>CA</State>
  <CeilHgtAvg>8</CeilHgtAvg>
  <Phone></Phone>
  <CFA>0.0</CFA>
  <Zip></Zip>
  <Name>New Project</Name>
  <City></City>
</Project>
>>>
### End Example ###






More information about the Python-list mailing list