-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_expenses.py
More file actions
34 lines (29 loc) · 1.16 KB
/
sort_expenses.py
File metadata and controls
34 lines (29 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import csv
from datetime import datetime
import tkinter as tk
from tkinter import messagebox
def sort_expenses(name:str, column_to_sort: str, descending: bool = False, date_format: str = "%Y-%m-%d") -> list[dict[str, any]]:
# CSV file reading
with open(name, mode="r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
rows = list(reader)
# If the file is empty, it returns
if not rows or column_to_sort not in rows[0]:
messagebox.showinfo("Dataset vuoto", "Nessuna voce presente nel dataset")
return
# If the column is absent, it returns
if column_to_sort not in rows[0]:
messagebox.showinfo("Informazione mancante", "La colonna inserita non è presente come voce nel dataset")
return
# Prova ad ordinare per data
try:
rows.sort(
key=lambda row: datetime.strptime(row[column_to_sort], date_format),
reverse=descending
)
except (ValueError, KeyError) as e:
root = tk.Tk()
root.withdraw()
messagebox.showerror("Errore di ordinamento", f"Impossibile ordinare per la colonna '{column_to_sort}':\n{e}")
return rows
# TODO : Generalise the function to order with other filters as well