Use built-in functions to replace for loops:

Old code:

for x in y:
  f(x)

New Code:

map(f, y)

Also use defaultdict instead of simple dict when advantageous and use list comprehensions
instead of for loops where map() could be used but requires lambdas.


svn: r14135
This commit is contained in:
Gerald Britton
2010-01-25 17:45:21 +00:00
parent fbb8fa2a52
commit 8f0582df8a
49 changed files with 125 additions and 188 deletions

View File

@@ -25,6 +25,7 @@ This Gramplet shows textual distributions of age breakdowns of various types.
"""
import locale
from collections import defaultdict
from gen.plug import Gramplet
from gen.ggettext import gettext as _
@@ -80,9 +81,9 @@ class AgeStatsGramplet(Gramplet):
def main(self):
self.clear_text()
age_dict = {}
mother_dict = {}
father_dict = {}
age_dict = defaultdict(int)
mother_dict = defaultdict(int)
father_dict = defaultdict(int)
age_handles = [[]] * self.max_age
mother_handles = [[]] * self.max_mother_diff
father_handles = [[]] * self.max_father_diff
@@ -105,7 +106,7 @@ class AgeStatsGramplet(Gramplet):
if death_date and birth_date and birth_date.get_year() != 0:
age = death_date.get_year() - birth_date.get_year()
if age >= 0 and age < self.max_age:
age_dict[age] = age_dict.get(age, 0) + 1
age_dict[age] += 1
age_handles[age].append(p.handle)
#else:
# print "Age out of range: %d for %s" % (age,
@@ -138,7 +139,7 @@ class AgeStatsGramplet(Gramplet):
if bdate and birth_date and birth_date.get_year() != 0:
diff = birth_date.get_year() - bdate.get_year()
if diff >= 0 and diff < self.max_father_diff:
father_dict[diff] = father_dict.get(diff, 0) + 1
father_dict[diff] += 1
father_handles[diff].append(f_handle)
#else:
# print "Father diff out of range: %d for %s" % (diff,
@@ -153,7 +154,7 @@ class AgeStatsGramplet(Gramplet):
if bdate and birth_date and birth_date.get_year() != 0:
diff = birth_date.get_year() - bdate.get_year()
if diff >= 0 and diff < self.max_mother_diff:
mother_dict[diff] = mother_dict.get(diff, 0) + 1
mother_dict[diff] += 1
mother_handles[diff].append(m_handle)
#else:
# print "Mother diff out of range: %d for %s" % (diff,

View File

@@ -20,6 +20,7 @@
# $Id$
#
#
from collections import defaultdict
from gen.ggettext import gettext as _
from gen.plug import Gramplet
@@ -58,7 +59,7 @@ class GivenNameCloudGramplet(Gramplet):
def main(self):
self.set_text(_("Processing...") + "\n")
yield True
givensubnames = {}
givensubnames = defaultdict(int)
representative_handle = {}
cnt = 0
@@ -67,7 +68,7 @@ class GivenNameCloudGramplet(Gramplet):
allnames = set(name.get_first_name().strip() for name in allnames)
for givenname in allnames:
for givensubname in givenname.split():
givensubnames[givensubname] = givensubnames.get(givensubname, 0) + 1
givensubnames[givensubname] += 1
representative_handle[givensubname] = person.handle
cnt += 1
if not cnt % _YIELD_INTERVAL:
@@ -98,9 +99,9 @@ class GivenNameCloudGramplet(Gramplet):
line = 0
### All done!
# Now, find out how many we can display without going over top_size:
totals = {}
totals = defaultdict(int)
for (count, givensubname) in cloud_names: # givensubname_sort:
totals[count] = totals.get(count, 0) + 1
totals[count] += 1
sums = sorted(totals, reverse=True)
total = 0
include_greater_than = 0

View File

@@ -74,10 +74,10 @@ class PluginManagerGramplet(Gramplet):
row.append(line[1:].strip())
else:
state = "read"
types = {}
for row in rows:
types[row[1]] = 1
types = dict([row[1], 1] for row in rows)
self.set_text("")
# name, type, ver, desc, use, rating, contact, download
for type in types:
self.render_text("<b>%s Plugins</b>\n" % _(type))

View File

@@ -18,6 +18,8 @@
# $Id$
from collections import defaultdict
#------------------------------------------------------------------------
#
# GRAMPS modules
@@ -78,7 +80,7 @@ class SurnameCloudGramplet(Gramplet):
def main(self):
self.set_text(_("Processing...") + "\n")
yield True
surnames = {}
surnames = defaultdict(int)
representative_handle = {}
cnt = 0
@@ -86,7 +88,7 @@ class SurnameCloudGramplet(Gramplet):
allnames = [person.get_primary_name()] + person.get_alternate_names()
allnames = set([name.get_group_name().strip() for name in allnames])
for surname in allnames:
surnames[surname] = surnames.get(surname, 0) + 1
surnames[surname] += 1
representative_handle[surname] = person.handle
cnt += 1
if not cnt % _YIELD_INTERVAL:
@@ -116,9 +118,9 @@ class SurnameCloudGramplet(Gramplet):
line = 0
### All done!
# Now, find out how many we can display without going over top_size:
totals = {}
totals = defaultdict(int)
for (count, givensubname) in cloud_names: # givensubname_sort:
totals[count] = totals.get(count, 0) + 1
totals[count] += 1
sums = sorted(totals, reverse=True)
total = 0
include_greater_than = 0

View File

@@ -18,6 +18,8 @@
# $Id$
from collections import defaultdict
#------------------------------------------------------------------------
#
# GRAMPS modules
@@ -62,7 +64,7 @@ class TopSurnamesGramplet(Gramplet):
def main(self):
self.set_text(_("Processing...") + "\n")
surnames = {}
surnames = defaultdict(int)
representative_handle = {}
cnt = 0
@@ -70,7 +72,7 @@ class TopSurnamesGramplet(Gramplet):
allnames = [person.get_primary_name()] + person.get_alternate_names()
allnames = set([name.get_group_name().strip() for name in allnames])
for surname in allnames:
surnames[surname] = surnames.get(surname, 0) + 1
surnames[surname] += 1
representative_handle[surname] = person.handle
cnt += 1
if not cnt % _YIELD_INTERVAL: