ultra newbie question (don't laugh)
Fredrik Lundh
fredrik at pythonware.com
Tue Sep 26 11:40:47 EDT 2006
John Salerno wrote:
> So you see, what I'm asking for is very basic help, sort of along the
> lines of "what things do I need to consider before I even begin this?"
> Is OOP necessary here? Would utility functions work just as well for
> simply writing the information to a file?
when you get programmers block, telling your brain to "shut up and let
me write some code" is often a good idea. just start tinkering with
your first idea, and see if you can make it work. if you get a little
stuck, hack your way through it. if you get really stuck, take a break,
work on something else for a while, and wait for your brain to come up
with a better idea. repeat.
here's how I would start:
import xml.etree.ElementTree as ET
class Researcher:
# attributes (you can skip this part, if you want)
first_name = None
last_name = None
birth_country = None
birth_state = None
birth_city = None
# the identity is lazily evaluated by getid (see below)
id = None
def getid(self):
if not self.id:
self.id = (self.first_name + self.last_name).lower() # etc
return self.id
def maketree(self):
elem = ET.Element("researcher", id=self.getid())
ET.SubElement(elem, "first_name").text = self.first_name
ET.SubElement(elem, "last_name").text = self.last_name
ET.SubElement(elem, "birth_country").text = self.birth_country
ET.SubElement(elem, "birth_state").text = self.birth_state
ET.SubElement(elem, "birth_city").text = self.birth_city
return elem
x = Researcher()
# simulate the UI's "OK" handler
x.first_name = "John"
x.last_name = "Salerno"
x.birth_country = "United States"
x.birth_state = "Texas"
x.birth_city = "Houston"
print ET.tostring(x.maketree())
</F>
More information about the Python-list
mailing list