Bug 3018: remove calls to keys() and values() dictionary method where possible

svn: r12579
This commit is contained in:
Gerald Britton 2009-05-27 17:33:45 +00:00
parent 1edada53d0
commit 81a0848490
11 changed files with 60 additions and 49 deletions

View File

@ -202,7 +202,7 @@ class StandardCustomSelector(object):
else:
int_val = self.custom_key
str_val = self.selector.child.get_text().strip()
if str_val in self.mapping.values():
if str_val in self.mapping.itervalues():
for key in self.mapping:
if str_val == self.mapping[key]:
int_val = key

View File

@ -1685,7 +1685,7 @@ class TextDoc(object):
else:
tagspos[start] = [(tag, FIRST)]
if end in tagspos:
tagspos[end] = [(tag, LAST)] + tagspos[end]
tagspos[end] += [(tag, LAST)]
else:
tagspos[end] = [(tag, LAST)]
start = 0
@ -1705,10 +1705,8 @@ class TextDoc(object):
while splitpos <> -1:
otext += self.ESCAPE_FUNC()(text[start:start+splitpos])
#close open tags
opentags.reverse()
for opentag in opentags:
for opentag in reversed(opentags):
otext += opentag[1]
opentags.reverse()
#add split text
otext += self.ESCAPE_FUNC()(split)
#open the tags again
@ -1722,10 +1720,8 @@ class TextDoc(object):
#write out tags
for tag in tagspos[pos]:
#close open tags starting from last open
opentags.reverse()
for opentag in opentags:
for opentag in reversed(opentags):
otext += opentag[1]
opentags.reverse()
#if start, add to opentag in beginning as first to open
if tag[1] == FIRST:
opentags = [tag[0]] + opentags
@ -1744,8 +1740,7 @@ class TextDoc(object):
if opentags:
print 'WARNING: TextDoc : More style tags in text than length '\
'of text allows.\n', opentags
opentags.reverse()
for opentag in opentags:
for opentag in reversed(opentags):
otext += opentag[1]
return otext

View File

@ -1077,7 +1077,8 @@ class MyScrolledWindow(gtk.ScrolledWindow):
# Hack to get around show_all that shows hidden items
# do once, the first time showing
if self.viewpage:
gramplets = [g for g in self.viewpage.gramplet_map.values() if g is not None]
gramplets = (g for g in self.viewpage.gramplet_map.itervalues()
if g is not None)
self.viewpage = None
for gramplet in gramplets:
gramplet.gvoptions.hide()
@ -1171,7 +1172,8 @@ class GrampletView(PageView.PersonNavView):
"""
Detach all of the mainframe gramplets from the columns.
"""
gramplets = [g for g in self.gramplet_map.values() if g is not None]
gramplets = (g for g in self.gramplet_map.itervalues()
if g is not None)
for gramplet in gramplets:
if (gramplet.state == "detached" or gramplet.state == "closed"):
continue
@ -1183,12 +1185,13 @@ class GrampletView(PageView.PersonNavView):
"""
Place the gramplet mainframes in the columns.
"""
gramplets = [g for g in self.gramplet_map.values() if g is not None]
gramplets = [g for g in self.gramplet_map.itervalues()
if g is not None]
# put the gramplets where they go:
# sort by row
gramplets.sort(lambda a, b: cmp(a.row, b.row))
cnt = 0
for gramplet in gramplets:
gramplets.sort(key=lambda x: x.row)
for cnt, gramplet in enumerate(gramplets):
# see if the user wants this in a particular location:
# and if there are that many columns
if gramplet.column >= 0 and gramplet.column < self.column_count:
@ -1213,7 +1216,6 @@ class GrampletView(PageView.PersonNavView):
gramplet.detach()
elif gramplet.state == "closed":
gramplet.close()
cnt += 1
def load_gramplets(self):
self.column_count = 2 # default for new user
@ -1609,7 +1611,8 @@ class GrampletView(PageView.PersonNavView):
return False
def on_delete(self):
gramplets = [g for g in self.gramplet_map.values() if g is not None]
gramplets = (g for g in self.gramplet_map.itervalues()
if g is not None)
for gramplet in gramplets:
# this is the only place where the gui runs user code directly
if gramplet.pui:

View File

@ -132,7 +132,8 @@ class GenChart(object):
new_y = 0
for key, i in self.array.iteritems():
old_y = key
if self.not_blank(i.values()):
print __name__, i
if self.not_blank(i.itervalues()):
self.compress_map[old_y] = new_y
new_array[new_y] = i
x = 0

View File

@ -534,19 +534,15 @@ class StatisticsChart(Report):
"""creates & stores a sorted index for the items"""
# sort by item keys
index = data.keys()
index.sort()
if reverse:
index.reverse()
index = sorted(data, reverse=True if reverse else False)
if sort == _options.SORT_VALUE:
# set for the sorting function
self.lookup_items = data
# then sort by value
index.sort(self.lookup_compare)
if reverse:
index.reverse()
index.sort(self.lookup_compare,
reverse=True if reverse else False)
return index

View File

@ -200,8 +200,9 @@ class AgeStatsGramplet(Gramplet):
def compute_stats(self, hash):
""" Returns the statistics of a dictionary of data """
print "compute_stats", hash
hashkeys = sorted(hash)
count = sum(hash.values())
count = sum(hash.itervalues())
sumval = sum([k * hash[k] for k in hash])
minval = min(hashkeys)
maxval = max(hashkeys)
@ -239,6 +240,7 @@ class AgeStatsGramplet(Gramplet):
where the key is the age, and the value stored is the count.
"""
# first, binify:
print "create_bargraph", hash
bin = [0] * (max_val/bin_size)
for value, hash_value in hash.iteritems():
bin[value/bin_size] += hash_value
@ -246,22 +248,36 @@ class AgeStatsGramplet(Gramplet):
max_bin = float(max(bin))
if max_bin != 0:
i = 0
self.append_text("--------" + self.format("", graph_width-4, fill = "-", borders="++") + "-----\n")
self.append_text(column.center(8) + self.format(title, graph_width-4, align="center") + " % " + "\n")
self.append_text("--------" + self.format("", graph_width-4, fill = "-", borders="++") + "-----\n")
self.append_text("--------" +
self.format("", graph_width-4, fill = "-", borders="++") +
"-----\n")
self.append_text(column.center(8) +
self.format(title, graph_width-4, align="center") +
" % " + "\n")
self.append_text("--------" +
self.format("", graph_width-4, fill = "-", borders="++") +
"-----\n")
for bin in bin:
self.append_text((" %3d-%3d" % (i * 5, (i+1)* 5,)))
selected = self.make_handles_set(i * 5, (i+1) *5, handles)
self.link(self.format("X" * int(bin/max_bin * (graph_width-4)), graph_width-4),
self.link(self.format("X" * int(bin/max_bin * (graph_width-4)),
graph_width-4),
'PersonList',
selected,
tooltip=_("Double-click to see %d people") % len(selected))
procent = float(len(selected))/(float(sum(hash.values())))*100
tooltip=_("Double-click to see %d people") %
len(selected))
procent = (float(len(selected)) /
(float(sum(hash.itervalues())))*100)
self.append_text(locale.format("%#5.2f", procent))
self.append_text("\n")
i += 1
self.append_text("--------" + self.format("", graph_width-4, fill = "-", borders="++") + "-----\n")
self.append_text(" % " + self.ticks(graph_width-4, start = 0, stop = int(max_bin/(float(sum(hash.values())))*100)) + "\n\n")
self.append_text("--------" +
self.format("", graph_width-4, fill = "-", borders="++") +
"-----\n")
self.append_text(" % " +
self.ticks(graph_width-4, start = 0,
stop = int(max_bin/(float(sum(hash.itervalues())))*100)) +
"\n\n")
self.append_text(self.compute_stats(hash))
self.append_text("\n")

View File

@ -101,8 +101,7 @@ class GivenNameCloudGramplet(Gramplet):
totals = {}
for (count, givensubname) in cloud_names: # givensubname_sort:
totals[count] = totals.get(count, 0) + 1
sums = sorted(totals)
sums.reverse()
sums = sorted(totals, reverse=True)
total = 0
include_greater_than = 0
for s in sums:

View File

@ -114,8 +114,7 @@ class SurnameCloudGramplet(Gramplet):
totals = {}
for (count, givensubname) in cloud_names: # givensubname_sort:
totals[count] = totals.get(count, 0) + 1
sums = sorted(totals)
sums.reverse()
sums = sorted(totals, reverse=True)
total = 0
include_greater_than = 0
for s in sums:

View File

@ -107,19 +107,19 @@ def set_font_families():
families = pangocairo.cairo_font_map_get_default().list_families()
family_names = [family.get_name() for family in families]
fam = [f for f in _TTF_FREEFONT.values() if f in family_names]
fam = [f for f in _TTF_FREEFONT.itervalues() if f in family_names]
if len(fam) == len(_TTF_FREEFONT):
font_families = _TTF_FREEFONT
log.debug('Using FreeFonts: %s' % font_families)
return
fam = [f for f in _MS_TTFONT.values() if f in family_names]
fam = [f for f in _MS_TTFONT.itervalues() if f in family_names]
if len(fam) == len(_MS_TTFONT):
font_families = _MS_TTFONT
log.debug('Using MS TrueType fonts: %s' % font_families)
return
fam = [f for f in _GNOME_FONT.values() if f in family_names]
fam = [f for f in _GNOME_FONT.itervalues() if f in family_names]
if len(fam) == len(_GNOME_FONT):
font_families = _GNOME_FONT
log.debug('Using Gnome fonts: %s' % font_families)

View File

@ -180,9 +180,9 @@ class DetAncestorReport(Report):
for family_handle in person.get_family_handle_list():
family = self.database.get_family_from_handle(family_handle)
mother_handle = family.get_mother_handle()
if mother_handle is None or \
mother_handle not in self.map.values() or \
person.get_gender() == gen.lib.Person.FEMALE:
if (mother_handle is None or
mother_handle not in self.map.itervalues() or
person.get_gender() == gen.lib.Person.FEMALE):
# The second test above also covers the 1. person's
# mate, which is not an ancestor and as such is not
# included in the self.map dictionary

View File

@ -92,11 +92,12 @@ class NumberOfAncestorsReport(Report):
while thisgensize > 0:
thisgensize = 0
if thisgen != {}:
thisgensize = len(thisgen.values())
thisgensize = len(thisgen)
gen += 1
theoretical = math.pow(2, ( gen - 1 ) )
total_theoretical += theoretical
percent = '(%s%%)' % locale.format('%3.2f', ((sum(thisgen.values()) / theoretical ) * 100))
percent = '(%s%%)' % locale.format('%3.2f',
((sum(thisgen.itervalues()) / theoretical ) * 100))
# TC # English return something like:
# Generation 3 has 2 individuals. (50.00%)
@ -132,7 +133,8 @@ class NumberOfAncestorsReport(Report):
person_data
if( total_theoretical != 1 ):
percent = '(%3.2f%%)' % (( sum(all_people.values()) / (total_theoretical-1) ) * 100)
percent = '(%3.2f%%)' % (( sum(all_people.itervalues())
/ (total_theoretical-1) ) * 100)
else:
percent = 0