64
edits
Changes
→Defining the Report class: A code description is aligned with the new code
===Defining the ReportOptions class===
In this exampletutorial, no special the report has two options . The first, called <code>what_types</code>, allows a user to choose whether the counts of persons whose gender is Male, Female, Unknown or Other are requiredshown in the report. This makes The second is the options class very simple. All standard Gramps option that is necessary is allows the user to define choose the default styleslocale for the report different from UI locale. It is added by calling <precode>from grampsstdoptions.genadd_localization_option()</code> method.plug.report import Reportfrom gramps.gen.plug.report import ReportOptionsfrom gramps.gen.lib import Personfrom gramps.gen.plug import docgenfrom gramps.gen.const import GRAMPS_LOCALE as glocale_ = glocale.translationThese two options are added in the overridden <code>add_menu_options()</code> method.gettext
To define two unique styles for the report: DBS-Title and DBS-Normal the method <code>make_default_style()</code> is overridden too. Both styles are added by calling <code>default_style.add_paragraph_style()</code> method.
So create now file '''report.py''' and copy there the above code.
===Defining the Report class===
The actual implementation of the <code>DbSummaryReport</code> is rather simple. No additional work needs to be done to To initialize the class, so the parent <code>Report.__init__()</code> routine is called. All and then values of the work is done in options entered by the user are extracted from <code>write_report()options_class</code> method. It uses a [https://www.gramps-project.org/docs/gen/gen_db.html#gramps.gen.db.generic.DbGeneric.iter_people parameter which is of class <code>self.database.iter_people()DbSummaryOptions</code>] to iterate through [https://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.person An instance variable <code>Personself.what_types</code>] objects and gathers some simple statistics. The only thing of any complication is the determination assigned a list of the most common surname. A python dictionary is used to store the number names of times each surname is used. Each time a surname is encountered, the value in the dictionary is incremented. The results are then loaded into a list and sorted, allowing us to find the most common name check boxes selected by looking at the last entry in the listuser.Then <precode>class DbSummaryReport(Report): def __init__(self, database, options_class, user): Report.__init__set_locale(self, database, options_class, user) def write_report(self): males = 0 females = 0 total = 0 surname_map = {} for person in self.database.iter_people(): if person.get_gender() == Person.MALE: males += 1 if person.get_gender() == Person.FEMALE: females += 1 total += 1 surname = person.get_primary_name().get_surname() if surname in surname_map: surname_map[surname] += 1 else: surname_map[surname] = 1 slist = [] for key in surname_map.keys(): slist.append((surname_map[key], key)) slist.sort() self.doc.start_paragraph("DBS-Title") self.doc.write_text(_("Database Summary")) self.doc.end_paragraph() self.doc.start_paragraph('DBS-Normal') self.doc.write_text(_('Number </code> method is called with the value of males : %d') % males) selfthe locale to be used to run the report.doc.end_paragraph() self.doc.start_paragraph('DBS-Normal') self.doc.write_text(_('Number of females : %d') % females) self.doc.end_paragraph()
Append the above code to the '''report.py''' file