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

View File

@ -48,14 +48,11 @@ class IsDescendantOf(Rule):
category = _('Descendant filters') category = _('Descendant filters')
description = _('Matches all descendants for the specified person') description = _('Matches all descendants for the specified person')
def prepare(self,db): def prepare(self, db):
self.db = db self.db = db
self.map = {} self.map = set()
try: try:
if int(self.list[1]): first = False if int(self.list[1]) else True
first = False
else:
first = True
except IndexError: except IndexError:
first = True first = True
try: try:
@ -65,20 +62,20 @@ class IsDescendantOf(Rule):
pass pass
def reset(self): def reset(self):
self.map = {} self.map.clear()
def apply(self,db,person): def apply(self, db, person):
return person.handle in self.map return person.handle in self.map
def init_list(self,person,first): def init_list(self, person, first):
if not person: if not person:
return return
if not first: if not first:
self.map[person.handle] = 1 self.map.add(person.handle)
for fam_id in person.get_family_handle_list(): for fam_id in person.get_family_handle_list():
fam = self.db.get_family_from_handle(fam_id) fam = self.db.get_family_from_handle(fam_id)
if fam: if fam:
for child_ref in fam.get_child_ref_list(): for child_ref in fam.get_child_ref_list():
self.init_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 " description = _("Matches ancestors of the people on the bookmark list "
"not more than N generations away") "not more than N generations away")
def prepare(self,db): def prepare(self, db):
self.db = db self.db = db
bookmarks = db.get_bookmarks().get() bookmarks = db.get_bookmarks().get()
self.map = set()
if len(bookmarks) == 0: if len(bookmarks) == 0:
self.apply = lambda db,p : False self.apply = lambda db,p : False
else: else:
self.map = {}
self.bookmarks = set(bookmarks) self.bookmarks = set(bookmarks)
self.apply = self.apply_real self.apply = self.apply_real
for self.bookmarkhandle in self.bookmarks: for self.bookmarkhandle in self.bookmarks:
self.init_ancestor_list(self.bookmarkhandle, 1) 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: # if p.get_handle() in self.map:
# loop_error(self.orig,p) # loop_error(self.orig,p)
if not handle: if not handle:
return return
if gen: if gen:
self.map[handle] = 1 self.map.add(handle)
if gen >= int(self.list[0]): if gen >= int(self.list[0]):
return return
@ -87,12 +87,12 @@ class IsLessThanNthGenerationAncestorOfBookmarked(Rule):
m_id = fam.get_mother_handle() m_id = fam.get_mother_handle()
if f_id: if f_id:
self.init_ancestor_list(f_id,gen+1) self.init_ancestor_list(f_id, gen+1)
if m_id: 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 return person.handle in self.map
def reset(self): def reset(self):
self.map = {} self.map.clear()

View File

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

View File

@ -52,24 +52,24 @@ class IsLessThanNthGenerationDescendantOf(Rule):
def prepare(self,db): def prepare(self,db):
self.db = db self.db = db
self.map = {} self.map = set()
try: try:
root_person = db.get_person_from_gramps_id(self.list[0]) root_person = db.get_person_from_gramps_id(self.list[0])
self.init_list(root_person,0) self.init_list(root_person, 0)
except: except:
pass pass
def reset(self): def reset(self):
self.map = {} self.map.clear()
def apply(self,db,person): def apply(self, db, person):
return person.handle in self.map return person.handle in self.map
def init_list(self,person,gen): def init_list(self,person,gen):
if not person: if not person:
return return
if gen: if gen:
self.map[person.handle] = 1 self.map.add(person.handle)
if gen >= int(self.list[1]): if gen >= int(self.list[1]):
return return
@ -77,4 +77,4 @@ class IsLessThanNthGenerationDescendantOf(Rule):
fam = self.db.get_family_from_handle(fam_id) fam = self.db.get_family_from_handle(fam_id)
for child_ref in fam.get_child_ref_list(): for child_ref in fam.get_child_ref_list():
self.init_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): def prepare(self,db):
self.db = db self.db = db
self.map = {} self.map = set()
try: try:
root_handle = db.get_person_from_gramps_id(self.list[0]).get_handle() root_handle = db.get_person_from_gramps_id(self.list[0]).get_handle()
self.init_ancestor_list(root_handle,0) self.init_ancestor_list(root_handle,0)
@ -59,18 +59,18 @@ class IsMoreThanNthGenerationAncestorOf(Rule):
pass pass
def reset(self): def reset(self):
self.map = [] self.map.clear()
def apply(self,db,person): def apply(self,db,person):
return person.handle in self.map 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: # if p.get_handle() in self.map:
# loop_error(self.orig,p) # loop_error(self.orig,p)
if not handle: if not handle:
return return
if gen >= int(self.list[1]): if gen >= int(self.list[1]):
self.map[handle] = 1 self.map.add(handle)
p = self.db.get_person_from_handle(handle) p = self.db.get_person_from_handle(handle)
fam_id = p.get_main_parents_family_handle() fam_id = p.get_main_parents_family_handle()
@ -80,6 +80,6 @@ class IsMoreThanNthGenerationAncestorOf(Rule):
m_id = fam.get_mother_handle() m_id = fam.get_mother_handle()
if f_id: if f_id:
self.init_ancestor_list(f_id,gen+1) self.init_ancestor_list(f_id, gen+1)
if m_id: 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") "person at least N generations away")
def prepare(self,db): def prepare(self ,db):
self.db = db self.db = db
self.map = {} self.map = set()
try: try:
root_person = db.get_person_from_gramps_id(self.list[0]) root_person = db.get_person_from_gramps_id(self.list[0])
self.init_list(root_person,0) self.init_list(root_person, 0)
except: except:
pass pass
@ -65,14 +65,14 @@ class IsMoreThanNthGenerationDescendantOf(Rule):
def apply(self,db,person): def apply(self,db,person):
return person.handle in self.map return person.handle in self.map
def init_list(self,person,gen): def init_list(self, person, gen):
if not person: if not person:
return return
if gen >= int(self.list[1]): 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(): for fam_id in person.get_family_handle_list():
fam = self.db.get_family_from_handle(fam_id) fam = self.db.get_family_from_handle(fam_id)
for child_ref in fam.get_child_ref_list(): for child_ref in fam.get_child_ref_list():
self.init_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): def prepare(self,db):
self.db = db self.db = db
self.map = {} self.map = set()
bookmarks = db.get_bookmarks().get() bookmarks = db.get_bookmarks().get()
if len(bookmarks) == 0: if len(bookmarks) == 0:
self.apply = lambda db,p : False self.apply = lambda db,p : False
@ -69,7 +69,7 @@ class RelationshipPathBetweenBookmarks(Rule):
pass pass
def reset(self): def reset(self):
self.map = {} self.map.clear()
# #
# Returns a name, given a handle. # Returns a name, given a handle.
@ -90,7 +90,7 @@ class RelationshipPathBetweenBookmarks(Rule):
# Given a group of individuals, returns all of their parents. # Given a group of individuals, returns all of their parents.
# The value keyed by the individual handles is the path from # The value keyed by the individual handles is the path from
# the original person up, like generation[gfather]= [son,father,gfather] # the original person up, like generation[gfather]= [son,father,gfather]
def parents(self,generation): def parents(self, generation):
if len(generation) < 1: return None if len(generation) < 1: return None
prev_generation = {} prev_generation = {}
for handle in generation: for handle in generation:
@ -144,30 +144,19 @@ class RelationshipPathBetweenBookmarks(Rule):
return rel_path return rel_path
def init_list(self): def init_list(self):
Map = {} self.map.update(self.bookmarks)
pathmap = {} if len(self.bookmarks) < 2:
bmarks = {} 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 # Go through all bookmarked individuals, and mark all
# of the people in each of the paths betweent them. # of the people in each of the paths betweent them.
for i in range(1, nb): lb = len(bmarks)
handle1 = bmarks[i] for i in range(lb-1):
for j in range(i+1, nb+1): for j in range(i+1, lb):
handle2 = bmarks[j]
try: try:
pathmap = self.rel_path_for_two(handle1, handle2) pathmap = self.rel_path_for_two(bmarks[i], bmarks[j])
for handle in pathmap: self.map.update(pathmap)
self.map[handle] = 1
except: except:
pass pass