diff --git a/src/PluginUtils/_PluginDialogs.py b/src/PluginUtils/_PluginDialogs.py index 65d6c5ecc..f1200e9fe 100644 --- a/src/PluginUtils/_PluginDialogs.py +++ b/src/PluginUtils/_PluginDialogs.py @@ -33,6 +33,7 @@ import gtk # #------------------------------------------------------------------------- from gettext import gettext as _ +from collections import defaultdict #------------------------------------------------------------------------- # @@ -186,16 +187,13 @@ class PluginDialog(ManagedWindow.ManagedWindow): self.store.clear() # build the tree items and group together based on the category name - item_hash = {} + item_hash = defaultdict() for plugin in reg_list: if not plugin.supported: category = _UNSUPPORTED else: category = categories[plugin.category] - if category in item_hash: - item_hash[category].append(plugin) - else: - item_hash[category] = [plugin] + item_hash[category].append(plugin) # add a submenu for each category, and populate it with the # GtkTreeItems that are associated with it. diff --git a/src/gui/viewmanager.py b/src/gui/viewmanager.py index 9907d69e9..79a1008ae 100644 --- a/src/gui/viewmanager.py +++ b/src/gui/viewmanager.py @@ -35,6 +35,7 @@ import os from gettext import gettext as _ from cStringIO import StringIO +from collections import defaultdict #------------------------------------------------------------------------- # @@ -1489,22 +1490,19 @@ class ViewManager(CLIManager): menu = gtk.Menu() menu.show() - - hash_data = {} + + hash_data = defaultdict(list) for pdata in item_list: if not pdata.supported: category = _UNSUPPORTED else: category = categories[pdata.category] - if category in hash_data: - hash_data[category].append(pdata) - else: - hash_data[category] = [pdata] - + hash_data[category].append(pdata) + # Sort categories, skipping the unsupported - catlist = [item for item in hash_data - if item != _UNSUPPORTED] - catlist.sort() + catlist = sorted(item for item in hash_data + if item != _UNSUPPORTED) + for key in catlist: new_key = key.replace(' ', '-') ofile.write('' % new_key) diff --git a/src/plugins/lib/libgedcom.py b/src/plugins/lib/libgedcom.py index 463c75089..c3ce80a3c 100644 --- a/src/plugins/lib/libgedcom.py +++ b/src/plugins/lib/libgedcom.py @@ -93,6 +93,7 @@ import time import codecs from gettext import gettext as _ from xml.parsers.expat import ParserCreate +from collections import defaultdict #------------------------------------------------------------------------ # @@ -5477,21 +5478,10 @@ class GedcomStageOne(object): """ return value and value[0] == '@' - @staticmethod - def __add_to_list(table, key, value): - """ - Add the value to the table entry associated with key. If the entry - does not exist, it is added. - """ - if key in table: - table[key].append(value) - else: - table[key] = [value] - def __init__(self, ifile): self.ifile = ifile - self.famc = {} - self.fams = {} + self.famc = defaultdict(list) + self.fams = defaultdict(list) self.enc = "" self.pcnt = 0 self.lcnt = 0 @@ -5551,9 +5541,9 @@ class GedcomStageOne(object): self.pcnt += 1 elif key in ("HUSB", "HUSBAND", "WIFE") and \ self.__is_xref_value(value): - self.__add_to_list(self.fams, value[1:-1], current_family_id) + self.fams[value[1:-1]].append(current_family_id) elif key in ("CHIL", "CHILD") and self.__is_xref_value(value): - self.__add_to_list(self.famc, value[1:-1], current_family_id) + self.famc[value[1:-1]].append(current_family_id) elif key == 'CHAR' and not self.enc: assert(isinstance(value, basestring)) self.enc = value diff --git a/src/plugins/textreport/IndivComplete.py b/src/plugins/textreport/IndivComplete.py index 360971c7a..6fd5cf205 100644 --- a/src/plugins/textreport/IndivComplete.py +++ b/src/plugins/textreport/IndivComplete.py @@ -30,6 +30,7 @@ #------------------------------------------------------------------------ import os from gettext import gettext as _ +from collections import defaultdict #------------------------------------------------------------------------ # @@ -477,7 +478,7 @@ class IndivCompleteReport(Report): contained within a group it is moved from the Individual Facts section into its own section. """ - event_dict = {} + event_dict = defaultdict(list) event_ref_list = self.person.get_event_ref_list() for event_ref in event_ref_list: if event_ref: @@ -485,10 +486,7 @@ class IndivCompleteReport(Report): group = TYPE2GROUP[event.get_type().value] if group not in self.section_list: group = FACTS - if group in event_dict: - event_dict[group].append(event_ref) - else: - event_dict[group] = [event_ref] + event_dict[group].append(event_ref) # Write separate event group sections for group in SECTION_LIST: diff --git a/src/plugins/tool/EventCmp.py b/src/plugins/tool/EventCmp.py index b8f55e550..971c6e764 100644 --- a/src/plugins/tool/EventCmp.py +++ b/src/plugins/tool/EventCmp.py @@ -29,6 +29,7 @@ # #------------------------------------------------------------------------ import os +from collections import defaultdict #------------------------------------------------------------------------ # @@ -128,7 +129,7 @@ class EventComparison(Tool.Tool,ManagedWindow.ManagedWindow): "on_editor_clicked" : self.filter_editor_clicked, "on_filters_delete_event": self.close, "on_help_clicked" : self.on_help_clicked, - "destroy_passed_object" : self.close + "destroy_passed_object" : self.close, "on_delete_event" : self.close, }) @@ -296,14 +297,11 @@ class DisplayChart(ManagedWindow.ManagedWindow): name = individual.get_primary_name().get_name() gid = individual.get_gramps_id() - the_map = {} + the_map = defaultdict(list) for ievent_ref in individual.get_event_ref_list(): ievent = self.db.get_event_from_handle(ievent_ref.ref) event_name = str(ievent.get_type()) - if event_name in the_map: - the_map[event_name].append(ievent_ref.ref) - else: - the_map[event_name] = [ievent_ref.ref] + the_map[event_name].append(ievent_ref.ref) first = True done = False @@ -346,7 +344,7 @@ class DisplayChart(ManagedWindow.ManagedWindow): name, birth, and death. This should be the column titles of the report. """ - the_map = {} + the_map = defaultdict(int) for individual_id in self.my_list: individual = self.db.get_person_from_handle(individual_id) for event_ref in individual.get_event_ref_list(): @@ -354,10 +352,7 @@ class DisplayChart(ManagedWindow.ManagedWindow): name = str(event.get_type()) if not name: break - if name in the_map: - the_map[name] += 1 - else: - the_map[name] = 1 + the_map[name] += 1 unsort_list = sorted([(d, k) for k,d in the_map.iteritems()],by_value) diff --git a/src/plugins/webreport/NarrativeWeb.py b/src/plugins/webreport/NarrativeWeb.py index 655c13223..50d74e362 100644 --- a/src/plugins/webreport/NarrativeWeb.py +++ b/src/plugins/webreport/NarrativeWeb.py @@ -64,6 +64,7 @@ from TransUtils import sgettext as _ from cStringIO import StringIO from textwrap import TextWrapper from unicodedata import normalize +from collections import defaultdict #------------------------------------------------------------------------ # @@ -5958,7 +5959,7 @@ class NavWebOptions(MenuReportOptions): # FIXME. Why do we need our own sorting? Why not use Sort.Sort? def sort_people(db, handle_list): - sname_sub = {} + sname_sub = defaultdict(list) sortnames = {} for person_handle in handle_list: @@ -5971,11 +5972,7 @@ def sort_people(db, handle_list): surname = db.get_name_group_mapping(primary_name.surname) sortnames[person_handle] = _nd.sort_string(primary_name) - - if surname in sname_sub: - sname_sub[surname].append(person_handle) - else: - sname_sub[surname] = [person_handle] + sname_sub[surname].append(person_handle) sorted_lists = [] temp_list = sorted(sname_sub, key=locale.strxfrm) @@ -6079,7 +6076,7 @@ def alphabet_navigation(menu_set, alphakey): @param: menu_set -- a dictionary of either letters or words @param: alphakey -- either Person, Place, or AlphaEvent """ - sorted_set = {} + sorted_set = defaultdict(int) # The comment below from the glibc locale sv_SE in # localedata/locales/sv_SE : # @@ -6096,10 +6093,7 @@ def alphabet_navigation(menu_set, alphakey): ltr = get_first_letters for menu_item in menu_set: - if menu_item in sorted_set: - sorted_set[menu_item] += 1 - else: - sorted_set[menu_item] = 1 + sorted_set[menu_item] += 1 # remove the number of each occurance of each letter sorted_alpha_index = sorted(sorted_set, key = locale.strxfrm) diff --git a/src/web/sortheaders.py b/src/web/sortheaders.py index f3e645e94..25ee58d9a 100644 --- a/src/web/sortheaders.py +++ b/src/web/sortheaders.py @@ -2,6 +2,7 @@ # Site: http://www.djangosnippets.org/snippets/308/ from django.core.paginator import InvalidPage, EmptyPage +from collections import defaultdict from unicodedata import normalize import locale @@ -137,7 +138,7 @@ class NamePaginator(object): self.pages = [] # chunk up the objects so we don't need to iterate over the whole list for each letter - chunks = {} + chunks = defaultdict(list) for obj in self.object_list: if on: @@ -146,10 +147,6 @@ class NamePaginator(object): obj_str = unicode(obj) letter = first_letter(obj_str[0]) - - if letter not in chunks: - chunks[letter] = [] - chunks[letter].append(obj) # the process for assigning objects to each page