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

Alan Gauld alan.gauld at btinternet.com
Wed Feb 4 23:33:45 CET 2009


"Timo" <timomlists at gmail.com> wrote

>>>> class Person:
>>>>      def __init__(self, parser):
>>>>          self.first  = parser.get(person, 'firstName')
>
> Hey Alan, thanks for that explanation!
>
> But the class you gave, does almost the same thing as my function.

That was my point. Its just a prettier way of handling it and slightly
easier to use sionce you don;t need to quote the field names,
just use the dot notation.

> both store the info in a dic, so to retrieve info for a certain 
> person, we both have to do the same thing. Or am I missing something 
> here?

The class is less typing. Youcreate a dictionary of dictionaries,
I create a dictionary of objects - which are easier to access.
However the class can also have methods added to do searches,
follow the tree etc. You could also add the people dictionary as a
class variable which would add to the conceptual consistency of
the solution..

But you are right, classes are pretty much fancy dictionaries and
you could write the "methods" as standalone functions operating
on the dictionaries.

Either way the biggest issue I see is the lack of a unique key for
your dictionary. Using names as a key is a bad idea especially
in familiies where the same names tend to get used over and
over. You really should consider adding a unique ID field and
use that to populate the father/mother fields.

>> decision. Normally I would agree with you and use:
>>
>>> class Person:
>>>    def __init__(self, first, last, father, mother):
>>>        self.first  = first
>>>        self.last   = last
>>>        self.father   = father
>>>        self.mother = mother

I would definitely use this one and then you can instantiate it with:

p = Person(parser.get(person,'ID',
                    parser.get(person,'firstname',
                    parser.get(person,'lastname',
                    parser.get(person,'father',
                    parser.get(person,'mother',)
people[p.ID] = p


print people[id].first

> If I do it like this, I will have to parse all the data from the 
> file and then send them to this class and then call this class again 
> to retrieve it.

You can store it in the class as you parse the file just as you
did with your dictionary. You don't call the class again you
use the people dictionary: A big bit of the advantage of OOP
is not that it allows you to do stuff you couldn't do without it
(although occasionaly this is true) but rather that it makes
the code look cleaner, more readable.

> Then I like my function better. But again, I would love to hear if 
> I'm wrong.

Its not a metter of "wrong" its a matter of style. Using classes
usually produces more readable code, with less typing.

You can also add methods to prettty print a person (by
implementing the __str__ method), search the people
dictionary for a person (by implementing comparison
methods)

You then wind up writing code that looks like this:

# imagine you have a person object p1...

for p in people:
     if p1.ID == p.father:
        print  "The father is', p

Using dictionaries you get someting like:

for p in people:
     if p1['ID'] == p['father']:
        print "The father is", p['first'], p['last']

Or if you write a print function:

for p in people:
     if p1['ID'] == p['father']:
        print "The father is", printPerson(p)

I think the first version is easier to read.
But it is admittedly doing the same thing.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list