[Tutor] Sort of database & "family tree" question

ALAN GAULD alan.gauld at btinternet.com
Wed Feb 4 02:16:17 CET 2009


> > And I assume you are reading these into a Person class and
> > storing these classes in a persons dictionary? 


> Can you explain this a little more for me please?


Sure. 
(I didn't notice this on gmane so apologies if others already answered)

> The current way of reading the data is this:
> 
> parser = ConfigParser.ConfigParser()
> parser.read(personFile)
> 
> def get_info(person)
>    infoDic = {}
>    infoDic['first']      = parser.get(person, 'firstName')
>    infoDic['last']       = parser.get(person, 'lastName')
>    infoDic['father']   = parser.get(person, 'father')
>    infoDic['mother'] = parser.get(person, 'mother')
>    return infoDic

TYhis is almost the same but you are using a dict. A sligtly more readable version is to define a class Person:

class Person:
     def __init__(self, parser):
         self.first  = parser.get(person, 'firstName')
         self.last   = parser.get(person, 'lastName')
         self.father   = parser.get(person, 'father')
         self.mother = parser.get(person, 'mother')

Now you can create your person records with:

parser = ConfigParser.ConfifgParser()
parser.read(personFile)

people = {}
# some kind of loop here?
# for record in parser...
      p = Person(parser)
      people[p.first,p.last] = p

Now you will have a dictionary full of persons and you can access them 
by the first,last tuple of their name.

BUT how is father/mother stored? Is it a first/last tuple or some ID number? 
If an ID you need to add that to your Person definition and use it as the 
people key.

Does that help?

Alan G.


More information about the Tutor mailing list