Changes

From Gramps

Report-writing tutorial

814 bytes added, 10:38, 2 December 2025
Defining the Report class: A code description is aligned with the new 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 calledand then values of the options entered by the user are extracted from <code>options_class</code> parameter which is of class <code>DbSummaryOptions</code>. An instance variable <code>self.what_types</code> is assigned a list of names of check boxes selected by the user. Then <code>self.set_locale()</code> method is called with the value of the locale to be used to run the report. All the work is done in the <code>_count()</code> method. It uses a [https://www.gramps-project.org/docs/gen/gen_db.html#gramps.gen.db.generic.DbGeneric.iter_people <code>self.database.iter_people()</code>] to iterate through [https://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.person <code>Person</code>] objects and gathers some simple statistics. The only thing of any complication is the determination of the most common surnames. A python dictionary is used to store the number 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 in the reverse order <code>slist.sort(reverse=True)</code>, allowing us to find the most common names by looking at the first entries in the list. Finally <code>write_report()</code> method outputs the data collected in accordance with the options selected by the report user. Note that the strings being used in the report (such as "Report Options", "Males" etc) have already been used somewhere else in Gramps and have already been translated into various languages. Thus the content of our report as well as its parameters are already translated!
All the work is done in the <code>write_report()</code> method. It uses a [https://www.gramps-project.org/docs/gen/gen_db.html#gramps.gen.db.generic.DbGeneric.iter_people <code>self.database.iter_people()</code>] to iterate through [https://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.person <code>Person</code>] objects and gathers some simple statistics.
The only thing of any complication is the determination of the most common surname. A python dictionary is used to store the number 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 by looking at the last entry in the list.
{{RWT_Report.py}}
Append the above code to the '''report.py''' file
64
edits

Navigation menu