[Tutor] Sort of database & "family tree" question
alan.gauld at btinternet.com
Mon Jan 26 17:11:34 CET 2009
"Timo" <timomlists at gmail.com> wrote
> I'm writing an application that stores people with some info. I'm
> doing this with ConfigParser and it works, but currently (for
> testing), there are only 10 persons in it. What if I add, let's say,
> about 500-600 or even more? Is this still a good choice?
I'd be happy with this for up to say 1000 people(arbitrary choice) but
for more than that I'd go with shelve.
> So my entry's look like this (more or less ;)):
> firstName = foo
> lastName = bar
> father = person2
> mother = person3
And I assume you are reading these into a Person class and
storing these classes in a persons dictionary? Then to access
a father becomes:
x = 'Person5'
> Now, I want to make a family tree out of this, something that looks
> like the following scheme (hope the formating will be kept):
> For person "foo bar", on his father-side, and mother-side the same.
> | father
> | father--<
> | | mother
> | | father
> | mother--<
> | mother
You don't necessarily need to store the tree if you only want to
Using the Persons dictionary approach you can easily write a function
traverse the tree by following the links to the top of the tree. (Note
means some people will have to have None as father/mother!)
Similarly you can write a function to find everyone whose father or
is X and so on.
For small scale data this is probably easier than building a full tree
structure from your config file. Although that's always an option too
and for larger datasets would be more performant. But then I'd move
to pickle or shelve because you could load/save the whole tree in one
Author of the Learn to Program web site
More information about the Tutor