@@ -79,7 +79,7 @@ def get_stats(items) -> dict:
7979 dict: A dictionary containing general statistics for the list of items.
8080 """
8181 if len (items ) < 1 :
82- raise ValueError (' The list of items must contain at least one item' )
82+ raise ValueError (" The list of items must contain at least one item" )
8383
8484 return {
8585 "average" : statistics .mean (items ),
@@ -106,7 +106,9 @@ def get_stats_by_category(self) -> dict:
106106 category_names = self ._items_db .get_category_names ()
107107 out_dict = {}
108108 for category_name in category_names :
109- items = [item for item in self ._items_db .get_items_by_category (category_name )]
109+ items = [
110+ item for item in self ._items_db .get_items_by_category (category_name )
111+ ]
110112 out_dict [category_name ] = self .get_stats_expense_and_income (items )
111113
112114 return out_dict
@@ -122,14 +124,21 @@ def get_stats_by_category_with_subcategories(self) -> dict:
122124 stats_dict = {}
123125 for category_name in category_names :
124126 # Case: With Category and Without Subcategory
125- stats_dict [f'{ category_name } -NoSubcategory' ] = self .get_stats_expense_and_income (
126- self ._items_db .get_items_without_subcategory (category_name ))
127+ stats_dict [f"{ category_name } -NoSubcategory" ] = (
128+ self .get_stats_expense_and_income (
129+ self ._items_db .get_items_without_subcategory (category_name )
130+ )
131+ )
127132
128133 # Case: With Category and With Subcategory
129134 for subcategory in self ._items_db .get_subcategory_names (category_name ):
130- stats_dict [f' { category_name } -{ subcategory } ' ] = \
135+ stats_dict [f" { category_name } -{ subcategory } " ] = (
131136 self .get_stats_expense_and_income (
132- self ._items_db .get_items_by_category_and_subcategory (category_name , subcategory ))
137+ self ._items_db .get_items_by_category_and_subcategory (
138+ category_name , subcategory
139+ )
140+ )
141+ )
133142
134143 return stats_dict
135144
@@ -179,12 +188,12 @@ def to_dataframe(items) -> pd.DataFrame:
179188 rows = []
180189 for item in items :
181190 category = item .category
182- row = {k : v for k , v in item .__dict__ .items () if k != ' category' }
191+ row = {k : v for k , v in item .__dict__ .items () if k != " category" }
183192
184193 if category is not None :
185- row [' category' ] = category .name
194+ row [" category" ] = category .name
186195 if category .subcategory is not None :
187- row [' subcategory' ] = category .subcategory
196+ row [" subcategory" ] = category .subcategory
188197
189198 rows .append (row )
190199
@@ -201,35 +210,37 @@ def generate_report(self):
201210 items = self .items_db .get_all_items ()
202211 df = self .to_dataframe (items )
203212
204- with pd .ExcelWriter (self .file_path , engine = ' xlsxwriter' ) as writer :
213+ with pd .ExcelWriter (self .file_path , engine = " xlsxwriter" ) as writer :
205214 workbook = writer .book
206215
207216 # Raw Data Tab
208217 df .to_excel (
209218 writer ,
210- ' Data' , # worksheet name
211- index = False # index does not contain relevant information
219+ " Data" , # worksheet name
220+ index = False , # index does not contain relevant information
212221 )
213- summary_sheet = writer .sheets ['Data' ] # Assigning a variable to the sheet allows formatting
222+ summary_sheet = writer .sheets [
223+ "Data"
224+ ] # Assigning a variable to the sheet allows formatting
214225
215226 # Pivot Table Tab
216227 pivot_table = df .pivot_table (
217- values = ' amount' ,
218- index = [' category' , ' subcategory' ],
228+ values = " amount" ,
229+ index = [" category" , " subcategory" ],
219230 aggfunc = {
220- ' amount' : [' mean' , ' max' , ' min' ],
221- }
231+ " amount" : [" mean" , " max" , " min" ],
232+ },
222233 )
223234
224235 # Flatten the hierarchical column index
225- pivot_table .columns = [f' { agg } _amount' for agg in pivot_table .columns ]
236+ pivot_table .columns = [f" { agg } _amount" for agg in pivot_table .columns ]
226237
227238 pivot_table .to_excel (
228239 writer ,
229- ' Summary By Category' , # worksheet name
230- index = True # index does not contain relevant information
240+ " Summary By Category" , # worksheet name
241+ index = True , # index does not contain relevant information
231242 )
232- summary_sheet = writer .sheets [' Summary By Category' ]
243+ summary_sheet = writer .sheets [" Summary By Category" ]
233244
234245
235246class PdfReport (Report ):
@@ -247,11 +258,11 @@ def generate_report(self):
247258
248259 # Pivot Table Tab
249260 pivot_table = df .pivot_table (
250- values = ' amount' ,
251- index = [' category' , ' subcategory' ],
261+ values = " amount" ,
262+ index = [" category" , " subcategory" ],
252263 aggfunc = {
253- ' amount' : [' mean' , ' max' , ' min' ],
254- }
264+ " amount" : [" mean" , " max" , " min" ],
265+ },
255266 )
256267 pivot_table = pivot_table .reset_index ()
257268
@@ -267,13 +278,17 @@ def generate_report(self):
267278 table = Table (table_data )
268279
269280 # Style the table
270- style = TableStyle ([('BACKGROUND' , (0 , 0 ), (- 1 , 0 ), colors .gray ),
271- ('TEXTCOLOR' , (0 , 0 ), (- 1 , 0 ), colors .whitesmoke ),
272- ('ALIGN' , (0 , 0 ), (- 1 , - 1 ), 'CENTER' ),
273- ('FONTNAME' , (0 , 0 ), (- 1 , 0 ), 'Helvetica-Bold' ),
274- ('BOTTOMPADDING' , (0 , 0 ), (- 1 , 0 ), 12 ),
275- ('BACKGROUND' , (0 , 1 ), (- 1 , - 1 ), colors .beige ),
276- ('GRID' , (0 , 0 ), (- 1 , - 1 ), 1 , colors .black )])
281+ style = TableStyle (
282+ [
283+ ("BACKGROUND" , (0 , 0 ), (- 1 , 0 ), colors .gray ),
284+ ("TEXTCOLOR" , (0 , 0 ), (- 1 , 0 ), colors .whitesmoke ),
285+ ("ALIGN" , (0 , 0 ), (- 1 , - 1 ), "CENTER" ),
286+ ("FONTNAME" , (0 , 0 ), (- 1 , 0 ), "Helvetica-Bold" ),
287+ ("BOTTOMPADDING" , (0 , 0 ), (- 1 , 0 ), 12 ),
288+ ("BACKGROUND" , (0 , 1 ), (- 1 , - 1 ), colors .beige ),
289+ ("GRID" , (0 , 0 ), (- 1 , - 1 ), 1 , colors .black ),
290+ ]
291+ )
277292
278293 table .setStyle (style )
279294
0 commit comments