I have an application that has been written in Python and it is used for generating various MIS reports for a specified month. It is now written without using the OOPs concepts or any kind of design patterns. I would like to refactor the application so that it will be maintainable and extensible. At this point, I have three reports and it will be increased in the future. There is a plan to move all the reports (about 25) to this platform so that the reporting system will act like an independent microservice.
Now the code is written in a procedural way as follows:
app.py
conn = get_db_connection()
cur = conn.cursor()
workbook = excel.create_workbook(filename)
excel.write_report_details(workbook, filename, year, month)
for current_month in range(int(month), 0, -1):
cur.execute('SELECT get_data(%s,%s,%s); ', (str(current_month), str(year)))
data = json.loads(json.dumps(cur.fetchall()))
row = data[0][0]
data_report1 = row['data_report1']
data_report2 = row['data_report2']
data_report3 = row['data_report3']
data_report4 = row['data_report4']
data_report5 = row['data_report5']
excel.write_report1(data_report1,workbook. year, month, logger) -> creates excel sheet in workbook and writes data
excel.write_report2(data_report2, workbook, year, month, logger, type='one_type') -> creates excel sheet and writes data based on the type
excel.write_report2(data_report2, workbook, year, month, logger, type='another_type') -> creates excel sheet and writes data based on the type
workbook.save(filename)
conn.close()
here the excel.py
is another python file from which the functions and imported to the main app.py
.
This looks simple, but as the number of reports increases, it will be difficult to manage the code as every time I need to add a method and all of these methods are in a single file. I have done some refactoring and moved some functions from excel.py
to another file and imported them to the excel
file. Also, there are multiple configurations like the report file names, report column headers, etc, which are specific to each of the reports.
Now my idea is to create a common Report
and extend this class for each of the reports. The base class will contain a method generate_report()
and this will be overridden in each of the sub-classes. As I need to generate all these reports, I have confused about whether I need to create individual objects for all these subclasses or if is there any alternative method for doing this. Also, if there is any better architectural pattern, please suggest one. I have checked the Factory Pattern, but not sure it is the correct pattern that I need to follow here.