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

spir denis.spir at free.fr
Mon Jan 26 15:54:37 CET 2009


Le Mon, 26 Jan 2009 14:58:43 +0100,
Timo <timomlists at gmail.com> a écrit :

> Hello,
> 
> 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?
> 
> So my entry's look like this (more or less ;)):
> [person1]
> firstName = foo
> lastName = bar
> father = person2
> mother = person3
> 
> 
> 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
> person2--<
>                  |               | father
>                  | mother--<
>                                  | mother
> 
> Greets,
> Timo
> 

You may build a custom type with 2 attributes 'father' & 'mother', plus a name.
The list of objects of this type would be directly populated from your data file.
"Terminal" persons (the tree's leaves) could have a special value (e.g. None) for their parent attributes, or you could add a special 'leaf' logical attribute to identify them.
This type can also implement a proper output using __str__ or __repr__

Not tested:

class Person(object):
	def __init__(self, name, father=None, mother=None):
		self.name = name
		self,father,self.mother = father,mother
		if mother and father:
			self.is_leaf = True
		else:
			self.is_leaf = False
	def __str__(self):
		if self.is_leaf:
			return self.name
		return "\t\t%s\n%s %s" % (self.name,self.father.name,self.mother.name)

[As the "family" structure is recursive, you can also easily define a recursive "treeView" method that recursively calls parent's own treeView.]

Denis

------
la vida e estranya


More information about the Tutor mailing list