@@ -79,7 +79,7 @@ def get_stats(items) -> dict:
79
79
dict: A dictionary containing general statistics for the list of items.
80
80
"""
81
81
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" )
83
83
84
84
return {
85
85
"average" : statistics .mean (items ),
@@ -106,7 +106,9 @@ def get_stats_by_category(self) -> dict:
106
106
category_names = self ._items_db .get_category_names ()
107
107
out_dict = {}
108
108
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
+ ]
110
112
out_dict [category_name ] = self .get_stats_expense_and_income (items )
111
113
112
114
return out_dict
@@ -122,14 +124,21 @@ def get_stats_by_category_with_subcategories(self) -> dict:
122
124
stats_dict = {}
123
125
for category_name in category_names :
124
126
# 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
+ )
127
132
128
133
# Case: With Category and With Subcategory
129
134
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 } " ] = (
131
136
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
+ )
133
142
134
143
return stats_dict
135
144
@@ -179,12 +188,12 @@ def to_dataframe(items) -> pd.DataFrame:
179
188
rows = []
180
189
for item in items :
181
190
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" }
183
192
184
193
if category is not None :
185
- row [' category' ] = category .name
194
+ row [" category" ] = category .name
186
195
if category .subcategory is not None :
187
- row [' subcategory' ] = category .subcategory
196
+ row [" subcategory" ] = category .subcategory
188
197
189
198
rows .append (row )
190
199
@@ -201,35 +210,37 @@ def generate_report(self):
201
210
items = self .items_db .get_all_items ()
202
211
df = self .to_dataframe (items )
203
212
204
- with pd .ExcelWriter (self .file_path , engine = ' xlsxwriter' ) as writer :
213
+ with pd .ExcelWriter (self .file_path , engine = " xlsxwriter" ) as writer :
205
214
workbook = writer .book
206
215
207
216
# Raw Data Tab
208
217
df .to_excel (
209
218
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
212
221
)
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
214
225
215
226
# Pivot Table Tab
216
227
pivot_table = df .pivot_table (
217
- values = ' amount' ,
218
- index = [' category' , ' subcategory' ],
228
+ values = " amount" ,
229
+ index = [" category" , " subcategory" ],
219
230
aggfunc = {
220
- ' amount' : [' mean' , ' max' , ' min' ],
221
- }
231
+ " amount" : [" mean" , " max" , " min" ],
232
+ },
222
233
)
223
234
224
235
# 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 ]
226
237
227
238
pivot_table .to_excel (
228
239
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
231
242
)
232
- summary_sheet = writer .sheets [' Summary By Category' ]
243
+ summary_sheet = writer .sheets [" Summary By Category" ]
233
244
234
245
235
246
class PdfReport (Report ):
@@ -247,11 +258,11 @@ def generate_report(self):
247
258
248
259
# Pivot Table Tab
249
260
pivot_table = df .pivot_table (
250
- values = ' amount' ,
251
- index = [' category' , ' subcategory' ],
261
+ values = " amount" ,
262
+ index = [" category" , " subcategory" ],
252
263
aggfunc = {
253
- ' amount' : [' mean' , ' max' , ' min' ],
254
- }
264
+ " amount" : [" mean" , " max" , " min" ],
265
+ },
255
266
)
256
267
pivot_table = pivot_table .reset_index ()
257
268
@@ -267,13 +278,17 @@ def generate_report(self):
267
278
table = Table (table_data )
268
279
269
280
# 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
+ )
277
292
278
293
table .setStyle (style )
279
294
0 commit comments