Common tasks

While this manual does not document the gramps database interface, this section shows a few common tasks.

Printing names of people

This example shows how to display the name of people in the database. It assumes that the database is called db. To get a list of people, it calls the getPersonMap method, which returns a map of gramps ID to Person objects. Calling the valus method of the returned map returns a list of people. For each person, the primary name is extracted, and then the Name object's getName method is called to build a presentable name from the individual name components.


for person in db.getPersonMap().values():
    name = person.getPrimaryName()
    print name.getName()

	

Figure 6. Displaying names

Displaying the events of person

This example shows how to display the public events associated with a person. It assumes that the person is called person.


for event in person.getEventList():
    if event.getPrivacy() == 0:
        print "Event:",event.getName()
	print "Date:",event.getDate()
	print "Place:",event.getPlaceName()

	

Figure 7. Displaying Event Information

Print the members of each family

This example shows how to display the parents and children of each family in the database. It assumes that the database is called db.


for family in db.getFamilyMap().values:
    print "-------------------"
    print "Family ID:",family.getId()
    father = family.getFather()
    if father != None:
        print "Father:",father.getPrimaryName().getName()
    mother = family.getMother()
    if mother != None:
        print "Mother:",mother.getPrimaryName().getName()
    for child in family.getChildList():
        print "Child:",child.getPrimaryName().getName()

	

Figure 8. Displaying Family Information

Display the marriages/relationships of a person

This example shows how to display the families and relationships in which the person is considered a spouse or parent. It assumes that the person is called person.

Relationships between people can be complex. Because someone is male, does not necessarily mean that the person will be considered the "Father" of a relationship. In relationships of type "Partners", the "father" and "mother" of the relationship should be of the same gender. So to determine the spouse of a person, it is usually best to compare the person against what is returned by getFather and getMother to find the one that is not equal. It should also be noted that the getFather and getMother methods will return None if noone has been associated with that role in the family.


for family in person.getFamilyList():
    print "-------------------"
    print "Family ID:",family.getId()
    print "Relationship Type:",family.getRelationship()
    father = family.getFather()
    if father != None and father != person:
        print "Spouse:",father.getPrimaryName().getName()
    mother = family.getMother()
    if mother != None and mother != person:
        print "Spouse:",mother.getPrimaryName().getName()

	

Figure 9. Displaying Relationship Information