Use defaultdict type where possible for minor performance gain and source code simplification
svn: r14011
This commit is contained in:
parent
4c7365dbcf
commit
2b12f3df07
@ -33,6 +33,7 @@ import gtk
|
|||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
@ -186,16 +187,13 @@ class PluginDialog(ManagedWindow.ManagedWindow):
|
|||||||
self.store.clear()
|
self.store.clear()
|
||||||
|
|
||||||
# build the tree items and group together based on the category name
|
# build the tree items and group together based on the category name
|
||||||
item_hash = {}
|
item_hash = defaultdict()
|
||||||
for plugin in reg_list:
|
for plugin in reg_list:
|
||||||
if not plugin.supported:
|
if not plugin.supported:
|
||||||
category = _UNSUPPORTED
|
category = _UNSUPPORTED
|
||||||
else:
|
else:
|
||||||
category = categories[plugin.category]
|
category = categories[plugin.category]
|
||||||
if category in item_hash:
|
item_hash[category].append(plugin)
|
||||||
item_hash[category].append(plugin)
|
|
||||||
else:
|
|
||||||
item_hash[category] = [plugin]
|
|
||||||
|
|
||||||
# add a submenu for each category, and populate it with the
|
# add a submenu for each category, and populate it with the
|
||||||
# GtkTreeItems that are associated with it.
|
# GtkTreeItems that are associated with it.
|
||||||
|
@ -35,6 +35,7 @@ import os
|
|||||||
|
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
@ -1489,22 +1490,19 @@ class ViewManager(CLIManager):
|
|||||||
|
|
||||||
menu = gtk.Menu()
|
menu = gtk.Menu()
|
||||||
menu.show()
|
menu.show()
|
||||||
|
|
||||||
hash_data = {}
|
hash_data = defaultdict(list)
|
||||||
for pdata in item_list:
|
for pdata in item_list:
|
||||||
if not pdata.supported:
|
if not pdata.supported:
|
||||||
category = _UNSUPPORTED
|
category = _UNSUPPORTED
|
||||||
else:
|
else:
|
||||||
category = categories[pdata.category]
|
category = categories[pdata.category]
|
||||||
if category in hash_data:
|
hash_data[category].append(pdata)
|
||||||
hash_data[category].append(pdata)
|
|
||||||
else:
|
|
||||||
hash_data[category] = [pdata]
|
|
||||||
|
|
||||||
# Sort categories, skipping the unsupported
|
# Sort categories, skipping the unsupported
|
||||||
catlist = [item for item in hash_data
|
catlist = sorted(item for item in hash_data
|
||||||
if item != _UNSUPPORTED]
|
if item != _UNSUPPORTED)
|
||||||
catlist.sort()
|
|
||||||
for key in catlist:
|
for key in catlist:
|
||||||
new_key = key.replace(' ', '-')
|
new_key = key.replace(' ', '-')
|
||||||
ofile.write('<menu action="%s">' % new_key)
|
ofile.write('<menu action="%s">' % new_key)
|
||||||
|
@ -93,6 +93,7 @@ import time
|
|||||||
import codecs
|
import codecs
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
from xml.parsers.expat import ParserCreate
|
from xml.parsers.expat import ParserCreate
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
#------------------------------------------------------------------------
|
#------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
@ -5477,21 +5478,10 @@ class GedcomStageOne(object):
|
|||||||
"""
|
"""
|
||||||
return value and value[0] == '@'
|
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):
|
def __init__(self, ifile):
|
||||||
self.ifile = ifile
|
self.ifile = ifile
|
||||||
self.famc = {}
|
self.famc = defaultdict(list)
|
||||||
self.fams = {}
|
self.fams = defaultdict(list)
|
||||||
self.enc = ""
|
self.enc = ""
|
||||||
self.pcnt = 0
|
self.pcnt = 0
|
||||||
self.lcnt = 0
|
self.lcnt = 0
|
||||||
@ -5551,9 +5541,9 @@ class GedcomStageOne(object):
|
|||||||
self.pcnt += 1
|
self.pcnt += 1
|
||||||
elif key in ("HUSB", "HUSBAND", "WIFE") and \
|
elif key in ("HUSB", "HUSBAND", "WIFE") and \
|
||||||
self.__is_xref_value(value):
|
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):
|
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:
|
elif key == 'CHAR' and not self.enc:
|
||||||
assert(isinstance(value, basestring))
|
assert(isinstance(value, basestring))
|
||||||
self.enc = value
|
self.enc = value
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
#------------------------------------------------------------------------
|
#------------------------------------------------------------------------
|
||||||
import os
|
import os
|
||||||
from gettext import gettext as _
|
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
|
contained within a group it is moved from the Individual Facts
|
||||||
section into its own section.
|
section into its own section.
|
||||||
"""
|
"""
|
||||||
event_dict = {}
|
event_dict = defaultdict(list)
|
||||||
event_ref_list = self.person.get_event_ref_list()
|
event_ref_list = self.person.get_event_ref_list()
|
||||||
for event_ref in event_ref_list:
|
for event_ref in event_ref_list:
|
||||||
if event_ref:
|
if event_ref:
|
||||||
@ -485,10 +486,7 @@ class IndivCompleteReport(Report):
|
|||||||
group = TYPE2GROUP[event.get_type().value]
|
group = TYPE2GROUP[event.get_type().value]
|
||||||
if group not in self.section_list:
|
if group not in self.section_list:
|
||||||
group = FACTS
|
group = FACTS
|
||||||
if group in event_dict:
|
event_dict[group].append(event_ref)
|
||||||
event_dict[group].append(event_ref)
|
|
||||||
else:
|
|
||||||
event_dict[group] = [event_ref]
|
|
||||||
|
|
||||||
# Write separate event group sections
|
# Write separate event group sections
|
||||||
for group in SECTION_LIST:
|
for group in SECTION_LIST:
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
#
|
#
|
||||||
#------------------------------------------------------------------------
|
#------------------------------------------------------------------------
|
||||||
import os
|
import os
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
#------------------------------------------------------------------------
|
#------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
@ -128,7 +129,7 @@ class EventComparison(Tool.Tool,ManagedWindow.ManagedWindow):
|
|||||||
"on_editor_clicked" : self.filter_editor_clicked,
|
"on_editor_clicked" : self.filter_editor_clicked,
|
||||||
"on_filters_delete_event": self.close,
|
"on_filters_delete_event": self.close,
|
||||||
"on_help_clicked" : self.on_help_clicked,
|
"on_help_clicked" : self.on_help_clicked,
|
||||||
"destroy_passed_object" : self.close
|
"destroy_passed_object" : self.close,
|
||||||
"on_delete_event" : self.close,
|
"on_delete_event" : self.close,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -296,14 +297,11 @@ class DisplayChart(ManagedWindow.ManagedWindow):
|
|||||||
name = individual.get_primary_name().get_name()
|
name = individual.get_primary_name().get_name()
|
||||||
gid = individual.get_gramps_id()
|
gid = individual.get_gramps_id()
|
||||||
|
|
||||||
the_map = {}
|
the_map = defaultdict(list)
|
||||||
for ievent_ref in individual.get_event_ref_list():
|
for ievent_ref in individual.get_event_ref_list():
|
||||||
ievent = self.db.get_event_from_handle(ievent_ref.ref)
|
ievent = self.db.get_event_from_handle(ievent_ref.ref)
|
||||||
event_name = str(ievent.get_type())
|
event_name = str(ievent.get_type())
|
||||||
if event_name in the_map:
|
the_map[event_name].append(ievent_ref.ref)
|
||||||
the_map[event_name].append(ievent_ref.ref)
|
|
||||||
else:
|
|
||||||
the_map[event_name] = [ievent_ref.ref]
|
|
||||||
|
|
||||||
first = True
|
first = True
|
||||||
done = False
|
done = False
|
||||||
@ -346,7 +344,7 @@ class DisplayChart(ManagedWindow.ManagedWindow):
|
|||||||
name, birth, and death.
|
name, birth, and death.
|
||||||
This should be the column titles of the report.
|
This should be the column titles of the report.
|
||||||
"""
|
"""
|
||||||
the_map = {}
|
the_map = defaultdict(int)
|
||||||
for individual_id in self.my_list:
|
for individual_id in self.my_list:
|
||||||
individual = self.db.get_person_from_handle(individual_id)
|
individual = self.db.get_person_from_handle(individual_id)
|
||||||
for event_ref in individual.get_event_ref_list():
|
for event_ref in individual.get_event_ref_list():
|
||||||
@ -354,10 +352,7 @@ class DisplayChart(ManagedWindow.ManagedWindow):
|
|||||||
name = str(event.get_type())
|
name = str(event.get_type())
|
||||||
if not name:
|
if not name:
|
||||||
break
|
break
|
||||||
if name in the_map:
|
the_map[name] += 1
|
||||||
the_map[name] += 1
|
|
||||||
else:
|
|
||||||
the_map[name] = 1
|
|
||||||
|
|
||||||
unsort_list = sorted([(d, k) for k,d in the_map.iteritems()],by_value)
|
unsort_list = sorted([(d, k) for k,d in the_map.iteritems()],by_value)
|
||||||
|
|
||||||
|
@ -64,6 +64,7 @@ from TransUtils import sgettext as _
|
|||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
from textwrap import TextWrapper
|
from textwrap import TextWrapper
|
||||||
from unicodedata import normalize
|
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?
|
# FIXME. Why do we need our own sorting? Why not use Sort.Sort?
|
||||||
def sort_people(db, handle_list):
|
def sort_people(db, handle_list):
|
||||||
sname_sub = {}
|
sname_sub = defaultdict(list)
|
||||||
sortnames = {}
|
sortnames = {}
|
||||||
|
|
||||||
for person_handle in handle_list:
|
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)
|
surname = db.get_name_group_mapping(primary_name.surname)
|
||||||
|
|
||||||
sortnames[person_handle] = _nd.sort_string(primary_name)
|
sortnames[person_handle] = _nd.sort_string(primary_name)
|
||||||
|
sname_sub[surname].append(person_handle)
|
||||||
if surname in sname_sub:
|
|
||||||
sname_sub[surname].append(person_handle)
|
|
||||||
else:
|
|
||||||
sname_sub[surname] = [person_handle]
|
|
||||||
|
|
||||||
sorted_lists = []
|
sorted_lists = []
|
||||||
temp_list = sorted(sname_sub, key=locale.strxfrm)
|
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: menu_set -- a dictionary of either letters or words
|
||||||
@param: alphakey -- either Person, Place, or AlphaEvent
|
@param: alphakey -- either Person, Place, or AlphaEvent
|
||||||
"""
|
"""
|
||||||
sorted_set = {}
|
sorted_set = defaultdict(int)
|
||||||
# The comment below from the glibc locale sv_SE in
|
# The comment below from the glibc locale sv_SE in
|
||||||
# localedata/locales/sv_SE :
|
# localedata/locales/sv_SE :
|
||||||
#
|
#
|
||||||
@ -6096,10 +6093,7 @@ def alphabet_navigation(menu_set, alphakey):
|
|||||||
ltr = get_first_letters
|
ltr = get_first_letters
|
||||||
|
|
||||||
for menu_item in menu_set:
|
for menu_item in menu_set:
|
||||||
if menu_item in sorted_set:
|
sorted_set[menu_item] += 1
|
||||||
sorted_set[menu_item] += 1
|
|
||||||
else:
|
|
||||||
sorted_set[menu_item] = 1
|
|
||||||
|
|
||||||
# remove the number of each occurance of each letter
|
# remove the number of each occurance of each letter
|
||||||
sorted_alpha_index = sorted(sorted_set, key = locale.strxfrm)
|
sorted_alpha_index = sorted(sorted_set, key = locale.strxfrm)
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
# Site: http://www.djangosnippets.org/snippets/308/
|
# Site: http://www.djangosnippets.org/snippets/308/
|
||||||
|
|
||||||
from django.core.paginator import InvalidPage, EmptyPage
|
from django.core.paginator import InvalidPage, EmptyPage
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
from unicodedata import normalize
|
from unicodedata import normalize
|
||||||
import locale
|
import locale
|
||||||
@ -137,7 +138,7 @@ class NamePaginator(object):
|
|||||||
self.pages = []
|
self.pages = []
|
||||||
|
|
||||||
# chunk up the objects so we don't need to iterate over the whole list for each letter
|
# 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:
|
for obj in self.object_list:
|
||||||
if on:
|
if on:
|
||||||
@ -146,10 +147,6 @@ class NamePaginator(object):
|
|||||||
obj_str = unicode(obj)
|
obj_str = unicode(obj)
|
||||||
|
|
||||||
letter = first_letter(obj_str[0])
|
letter = first_letter(obj_str[0])
|
||||||
|
|
||||||
if letter not in chunks:
|
|
||||||
chunks[letter] = []
|
|
||||||
|
|
||||||
chunks[letter].append(obj)
|
chunks[letter].append(obj)
|
||||||
|
|
||||||
# the process for assigning objects to each page
|
# the process for assigning objects to each page
|
||||||
|
Loading…
Reference in New Issue
Block a user