From 81a08484902efba2873063ba508b0ba4cf8a4fa7 Mon Sep 17 00:00:00 2001 From: Gerald Britton Date: Wed, 27 May 2009 17:33:45 +0000 Subject: [PATCH] Bug 3018: remove calls to keys() and values() dictionary method where possible svn: r12579 --- src/AutoComp.py | 2 +- src/BaseDoc.py | 13 +++---- src/DataViews/GrampletView.py | 19 ++++++----- src/plugins/drawreport/AncestorTree.py | 3 +- src/plugins/drawreport/StatisticsChart.py | 10 ++---- src/plugins/gramplet/AgeStats.py | 34 ++++++++++++++----- src/plugins/gramplet/GivenNameGramplet.py | 3 +- src/plugins/gramplet/SurnameCloudGramplet.py | 3 +- src/plugins/lib/libcairodoc.py | 8 ++--- src/plugins/textreport/DetAncestralReport.py | 6 ++-- .../textreport/NumberOfAncestorsReport.py | 8 +++-- 11 files changed, 60 insertions(+), 49 deletions(-) diff --git a/src/AutoComp.py b/src/AutoComp.py index 2b03899d0..1ecdb742b 100644 --- a/src/AutoComp.py +++ b/src/AutoComp.py @@ -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 diff --git a/src/BaseDoc.py b/src/BaseDoc.py index a4e5493b6..a419a96aa 100644 --- a/src/BaseDoc.py +++ b/src/BaseDoc.py @@ -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 diff --git a/src/DataViews/GrampletView.py b/src/DataViews/GrampletView.py index eb5c13b30..9d87e7bc9 100644 --- a/src/DataViews/GrampletView.py +++ b/src/DataViews/GrampletView.py @@ -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: diff --git a/src/plugins/drawreport/AncestorTree.py b/src/plugins/drawreport/AncestorTree.py index 36acbf478..059e82490 100644 --- a/src/plugins/drawreport/AncestorTree.py +++ b/src/plugins/drawreport/AncestorTree.py @@ -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 diff --git a/src/plugins/drawreport/StatisticsChart.py b/src/plugins/drawreport/StatisticsChart.py index 3d95f367a..c5846ad58 100644 --- a/src/plugins/drawreport/StatisticsChart.py +++ b/src/plugins/drawreport/StatisticsChart.py @@ -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 diff --git a/src/plugins/gramplet/AgeStats.py b/src/plugins/gramplet/AgeStats.py index c399c408c..203ffe4fa 100644 --- a/src/plugins/gramplet/AgeStats.py +++ b/src/plugins/gramplet/AgeStats.py @@ -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") diff --git a/src/plugins/gramplet/GivenNameGramplet.py b/src/plugins/gramplet/GivenNameGramplet.py index b50f6077e..76ee1eda8 100644 --- a/src/plugins/gramplet/GivenNameGramplet.py +++ b/src/plugins/gramplet/GivenNameGramplet.py @@ -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: diff --git a/src/plugins/gramplet/SurnameCloudGramplet.py b/src/plugins/gramplet/SurnameCloudGramplet.py index 107fee90b..51ba38c56 100644 --- a/src/plugins/gramplet/SurnameCloudGramplet.py +++ b/src/plugins/gramplet/SurnameCloudGramplet.py @@ -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: diff --git a/src/plugins/lib/libcairodoc.py b/src/plugins/lib/libcairodoc.py index a3a42d859..6486ca4d7 100644 --- a/src/plugins/lib/libcairodoc.py +++ b/src/plugins/lib/libcairodoc.py @@ -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) @@ -1584,4 +1584,4 @@ def register_plugin(): ) ) -register_plugin() \ No newline at end of file +register_plugin() diff --git a/src/plugins/textreport/DetAncestralReport.py b/src/plugins/textreport/DetAncestralReport.py index 014cce2ac..b79752265 100644 --- a/src/plugins/textreport/DetAncestralReport.py +++ b/src/plugins/textreport/DetAncestralReport.py @@ -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 diff --git a/src/plugins/textreport/NumberOfAncestorsReport.py b/src/plugins/textreport/NumberOfAncestorsReport.py index 9e8e885c7..ab20cb883 100644 --- a/src/plugins/textreport/NumberOfAncestorsReport.py +++ b/src/plugins/textreport/NumberOfAncestorsReport.py @@ -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