Replace dictionaries with sets and set logic where possible

svn: r13308
This commit is contained in:
Gerald Britton 2009-10-05 18:04:33 +00:00
parent 435aca1e95
commit 44ccad19c4
8 changed files with 56 additions and 70 deletions

View File

@ -50,7 +50,7 @@ class IsChildOfFilterMatch(MatchesFilter):
def prepare(self,db):
self.db = db
self.map = {}
self.map = set()
filt = MatchesFilter(self.list)
filt.prepare(db)
for person in db.iter_people():
@ -59,7 +59,7 @@ class IsChildOfFilterMatch(MatchesFilter):
filt.reset()
def reset(self):
self.map = {}
self.map.clear()
def apply(self,db,person):
return person.handle in self.map
@ -69,5 +69,5 @@ class IsChildOfFilterMatch(MatchesFilter):
return
for fam_id in person.get_family_handle_list():
fam = self.db.get_family_from_handle(fam_id)
for child_ref in fam.get_child_ref_list():
self.map[child_ref.ref] = 1
self.map.update(child_ref.ref
for child_ref in fam.get_child_ref_list())

View File

@ -48,14 +48,11 @@ class IsDescendantOf(Rule):
category = _('Descendant filters')
description = _('Matches all descendants for the specified person')
def prepare(self,db):
def prepare(self, db):
self.db = db
self.map = {}
self.map = set()
try:
if int(self.list[1]):
first = False
else:
first = True
first = False if int(self.list[1]) else True
except IndexError:
first = True
try:
@ -65,20 +62,20 @@ class IsDescendantOf(Rule):
pass
def reset(self):
self.map = {}
self.map.clear()
def apply(self,db,person):
def apply(self, db, person):
return person.handle in self.map
def init_list(self,person,first):
def init_list(self, person, first):
if not person:
return
if not first:
self.map[person.handle] = 1
self.map.add(person.handle)
for fam_id in person.get_family_handle_list():
fam = self.db.get_family_from_handle(fam_id)
if fam:
for child_ref in fam.get_child_ref_list():
self.init_list(
self.db.get_person_from_handle(child_ref.ref),0)
self.db.get_person_from_handle(child_ref.ref), 0)

View File

@ -56,26 +56,26 @@ class IsLessThanNthGenerationAncestorOfBookmarked(Rule):
description = _("Matches ancestors of the people on the bookmark list "
"not more than N generations away")
def prepare(self,db):
def prepare(self, db):
self.db = db
bookmarks = db.get_bookmarks().get()
self.map = set()
if len(bookmarks) == 0:
self.apply = lambda db,p : False
else:
self.map = {}
self.bookmarks = set(bookmarks)
self.apply = self.apply_real
for self.bookmarkhandle in self.bookmarks:
self.init_ancestor_list(self.bookmarkhandle, 1)
def init_ancestor_list(self, handle,gen):
def init_ancestor_list(self, handle, gen):
# if p.get_handle() in self.map:
# loop_error(self.orig,p)
if not handle:
return
if gen:
self.map[handle] = 1
self.map.add(handle)
if gen >= int(self.list[0]):
return
@ -87,12 +87,12 @@ class IsLessThanNthGenerationAncestorOfBookmarked(Rule):
m_id = fam.get_mother_handle()
if f_id:
self.init_ancestor_list(f_id,gen+1)
self.init_ancestor_list(f_id, gen+1)
if m_id:
self.init_ancestor_list(m_id,gen+1)
self.init_ancestor_list(m_id, gen+1)
def apply_real(self,db,person):
def apply_real(self, db, person):
return person.handle in self.map
def reset(self):
self.map = {}
self.map.clear()

View File

@ -53,22 +53,22 @@ class IsLessThanNthGenerationAncestorOfDefaultPerson(Rule):
def prepare(self,db):
self.db = db
self.map = set()
p = db.get_default_person()
if p:
self.def_handle = p.get_handle()
self.apply = self.apply_real
self.map = {}
self.init_ancestor_list(self.def_handle, 1)
else:
self.apply = lambda db,p: False
def init_ancestor_list(self, handle,gen):
def init_ancestor_list(self, handle, gen):
# if p.get_handle() in self.map:
# loop_error(self.orig,p)
if not handle:
return
if gen:
self.map[handle] = 1
self.map.add(handle)
if gen >= int(self.list[0]):
return
@ -80,12 +80,12 @@ class IsLessThanNthGenerationAncestorOfDefaultPerson(Rule):
m_id = fam.get_mother_handle()
if f_id:
self.init_ancestor_list(f_id,gen+1)
self.init_ancestor_list(f_id, gen+1)
if m_id:
self.init_ancestor_list(m_id,gen+1)
self.init_ancestor_list(m_id, gen+1)
def apply_real(self,db,person):
return person.handle in self.map
def reset(self):
self.map = {}
self.map.clear()

View File

@ -52,24 +52,24 @@ class IsLessThanNthGenerationDescendantOf(Rule):
def prepare(self,db):
self.db = db
self.map = {}
self.map = set()
try:
root_person = db.get_person_from_gramps_id(self.list[0])
self.init_list(root_person,0)
self.init_list(root_person, 0)
except:
pass
def reset(self):
self.map = {}
self.map.clear()
def apply(self,db,person):
def apply(self, db, person):
return person.handle in self.map
def init_list(self,person,gen):
if not person:
return
if gen:
self.map[person.handle] = 1
self.map.add(person.handle)
if gen >= int(self.list[1]):
return
@ -77,4 +77,4 @@ class IsLessThanNthGenerationDescendantOf(Rule):
fam = self.db.get_family_from_handle(fam_id)
for child_ref in fam.get_child_ref_list():
self.init_list(
self.db.get_person_from_handle(child_ref.ref),gen+1)
self.db.get_person_from_handle(child_ref.ref), gen+1)

View File

@ -51,7 +51,7 @@ class IsMoreThanNthGenerationAncestorOf(Rule):
def prepare(self,db):
self.db = db
self.map = {}
self.map = set()
try:
root_handle = db.get_person_from_gramps_id(self.list[0]).get_handle()
self.init_ancestor_list(root_handle,0)
@ -59,18 +59,18 @@ class IsMoreThanNthGenerationAncestorOf(Rule):
pass
def reset(self):
self.map = []
self.map.clear()
def apply(self,db,person):
return person.handle in self.map
def init_ancestor_list(self, handle,gen):
def init_ancestor_list(self, handle, gen):
# if p.get_handle() in self.map:
# loop_error(self.orig,p)
if not handle:
return
if gen >= int(self.list[1]):
self.map[handle] = 1
self.map.add(handle)
p = self.db.get_person_from_handle(handle)
fam_id = p.get_main_parents_family_handle()
@ -80,6 +80,6 @@ class IsMoreThanNthGenerationAncestorOf(Rule):
m_id = fam.get_mother_handle()
if f_id:
self.init_ancestor_list(f_id,gen+1)
self.init_ancestor_list(f_id, gen+1)
if m_id:
self.init_ancestor_list(m_id,gen+1)
self.init_ancestor_list(m_id, gen+1)

View File

@ -50,12 +50,12 @@ class IsMoreThanNthGenerationDescendantOf(Rule):
"person at least N generations away")
def prepare(self,db):
def prepare(self ,db):
self.db = db
self.map = {}
self.map = set()
try:
root_person = db.get_person_from_gramps_id(self.list[0])
self.init_list(root_person,0)
self.init_list(root_person, 0)
except:
pass
@ -65,14 +65,14 @@ class IsMoreThanNthGenerationDescendantOf(Rule):
def apply(self,db,person):
return person.handle in self.map
def init_list(self,person,gen):
def init_list(self, person, gen):
if not person:
return
if gen >= int(self.list[1]):
self.map[person.handle] = 1
self.map.add(person.handle)
for fam_id in person.get_family_handle_list():
fam = self.db.get_family_from_handle(fam_id)
for child_ref in fam.get_child_ref_list():
self.init_list(
self.db.get_person_from_handle(child_ref.ref),gen+1)
self.db.get_person_from_handle(child_ref.ref), gen+1)

View File

@ -57,7 +57,7 @@ class RelationshipPathBetweenBookmarks(Rule):
def prepare(self,db):
self.db = db
self.map = {}
self.map = set()
bookmarks = db.get_bookmarks().get()
if len(bookmarks) == 0:
self.apply = lambda db,p : False
@ -69,7 +69,7 @@ class RelationshipPathBetweenBookmarks(Rule):
pass
def reset(self):
self.map = {}
self.map.clear()
#
# Returns a name, given a handle.
@ -90,7 +90,7 @@ class RelationshipPathBetweenBookmarks(Rule):
# Given a group of individuals, returns all of their parents.
# The value keyed by the individual handles is the path from
# the original person up, like generation[gfather]= [son,father,gfather]
def parents(self,generation):
def parents(self, generation):
if len(generation) < 1: return None
prev_generation = {}
for handle in generation:
@ -144,30 +144,19 @@ class RelationshipPathBetweenBookmarks(Rule):
return rel_path
def init_list(self):
Map = {}
pathmap = {}
bmarks = {}
self.map.update(self.bookmarks)
if len(self.bookmarks) < 2:
return
bmarks = list(self.bookmarks)
# Handle having fewer than 2 bookmarks, or unrelated people.
nb = 0
for handle in self.bookmarks:
nb = nb + 1
self.map[handle] = 1
bmarks[nb] = handle
#print "bmarks[", nb, "] = ", handle, self.hnm(handle)
if nb <= 1: return
#print "bmarks = ", bmarks
#
# Go through all bookmarked individuals, and mark all
# of the people in each of the paths betweent them.
for i in range(1, nb):
handle1 = bmarks[i]
for j in range(i+1, nb+1):
handle2 = bmarks[j]
lb = len(bmarks)
for i in range(lb-1):
for j in range(i+1, lb):
try:
pathmap = self.rel_path_for_two(handle1, handle2)
for handle in pathmap:
self.map[handle] = 1
pathmap = self.rel_path_for_two(bmarks[i], bmarks[j])
self.map.update(pathmap)
except:
pass