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

@ -50,12 +50,9 @@ class IsDescendantOf(Rule):
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,7 +62,7 @@ class IsDescendantOf(Rule):
pass
def reset(self):
self.map = {}
self.map.clear()
def apply(self, db, person):
return person.handle in self.map
@ -74,7 +71,7 @@ class IsDescendantOf(Rule):
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)

View File

@ -59,10 +59,10 @@ class IsLessThanNthGenerationAncestorOfBookmarked(Rule):
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:
@ -75,7 +75,7 @@ class IsLessThanNthGenerationAncestorOfBookmarked(Rule):
if not handle:
return
if gen:
self.map[handle] = 1
self.map.add(handle)
if gen >= int(self.list[0]):
return
@ -95,4 +95,4 @@ class IsLessThanNthGenerationAncestorOfBookmarked(Rule):
return person.handle in self.map
def reset(self):
self.map = {}
self.map.clear()

View File

@ -53,11 +53,11 @@ 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
@ -68,7 +68,7 @@ class IsLessThanNthGenerationAncestorOfDefaultPerson(Rule):
if not handle:
return
if gen:
self.map[handle] = 1
self.map.add(handle)
if gen >= int(self.list[0]):
return
@ -88,4 +88,4 @@ class IsLessThanNthGenerationAncestorOfDefaultPerson(Rule):
return person.handle in self.map
def reset(self):
self.map = {}
self.map.clear()

View File

@ -52,7 +52,7 @@ 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)
@ -60,7 +60,7 @@ class IsLessThanNthGenerationDescendantOf(Rule):
pass
def reset(self):
self.map = {}
self.map.clear()
def apply(self, db, person):
return person.handle in self.map
@ -69,7 +69,7 @@ class IsLessThanNthGenerationDescendantOf(Rule):
if not person:
return
if gen:
self.map[person.handle] = 1
self.map.add(person.handle)
if gen >= int(self.list[1]):
return

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,7 +59,7 @@ class IsMoreThanNthGenerationAncestorOf(Rule):
pass
def reset(self):
self.map = []
self.map.clear()
def apply(self,db,person):
return person.handle in self.map
@ -70,7 +70,7 @@ class IsMoreThanNthGenerationAncestorOf(Rule):
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()

View File

@ -52,7 +52,7 @@ class IsMoreThanNthGenerationDescendantOf(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)
@ -69,7 +69,7 @@ class IsMoreThanNthGenerationDescendantOf(Rule):
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)

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.
@ -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