-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpense_tracker.py
More file actions
277 lines (211 loc) · 8.04 KB
/
Copy pathexpense_tracker.py
File metadata and controls
277 lines (211 loc) · 8.04 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
expense_tracker.py
Single-file CLI app: Personal Expense Tracker (JSON persistence)
What it does:
- Add expenses with amount, category, note, date
- List expenses (filter by category and/or date range)
- Summarize totals (overall + by category)
- Delete an expense by its ID
- Saves data to expenses.json (standard library only)
"""
from __future__ import annotations
import json
import os
from datetime import datetime, date
from typing import Any, Dict, List, Optional
DATA_FILE = "expenses.json"
DATE_FMT = "%Y-%m-%d"
def load_expenses(filepath: str) -> List[Dict[str, Any]]:
"""Load expenses from a JSON file. If missing/invalid, return an empty list."""
if not os.path.exists(filepath):
return []
try:
with open(filepath, "r", encoding="utf-8") as file:
data = json.load(file)
if not isinstance(data, list):
return []
return data
except (json.JSONDecodeError, OSError):
return []
def save_expenses(filepath: str, expenses: List[Dict[str, Any]]) -> None:
"""Save expenses to a JSON file safely (pretty-printed)."""
try:
with open(filepath, "w", encoding="utf-8") as file:
json.dump(expenses, file, indent=2)
except OSError:
print("Error: Could not save data. Check file permissions or disk space.")
def parse_date(date_str: str) -> Optional[date]:
"""Parse YYYY-MM-DD into a date object. Return None if invalid."""
try:
return datetime.strptime(date_str.strip(), DATE_FMT).date()
except ValueError:
return None
def prompt_nonempty(prompt: str) -> str:
"""Prompt until the user enters a non-empty string."""
while True:
value = input(prompt).strip()
if value:
return value
print("Please enter a value (cannot be blank).")
def prompt_float(prompt: str) -> float:
"""Prompt until the user enters a valid positive float."""
while True:
raw = input(prompt).strip()
try:
value = float(raw)
if value <= 0:
print("Amount must be greater than 0.")
continue
return value
except ValueError:
print("Please enter a valid number (example: 12.50).")
def prompt_date(prompt: str, allow_blank: bool = False) -> Optional[date]:
"""Prompt for YYYY-MM-DD; optionally allow blank to mean None."""
while True:
raw = input(prompt).strip()
if allow_blank and raw == "":
return None
parsed = parse_date(raw)
if parsed is not None:
return parsed
print("Invalid date. Use YYYY-MM-DD (example: 2026-01-08).")
def next_id(expenses: List[Dict[str, Any]]) -> int:
"""Return the next integer ID based on current expenses."""
if not expenses:
return 1
existing_ids = [e.get("id", 0) for e in expenses if isinstance(e.get("id"), int)]
return (max(existing_ids) if existing_ids else 0) + 1
def add_expense(expenses: List[Dict[str, Any]]) -> None:
"""Collect input, validate it, append a new expense, and save."""
amount = prompt_float("Amount (e.g., 12.50): ")
category = prompt_nonempty("Category (e.g., food, gas, rent): ").lower()
note = input("Note (optional): ").strip()
when = prompt_date("Date (YYYY-MM-DD) [blank = today]: ", allow_blank=True)
if when is None:
when = date.today()
new_item = {
"id": next_id(expenses),
"amount": round(amount, 2),
"category": category,
"note": note,
"date": when.strftime(DATE_FMT),
}
expenses.append(new_item)
save_expenses(DATA_FILE, expenses)
print(f"Added expense #{new_item['id']} ✅")
def matches_filters(
item: Dict[str, Any],
category: Optional[str],
start: Optional[date],
end: Optional[date],
) -> bool:
"""Return True if an expense matches the chosen filters."""
if category is not None and item.get("category") != category:
return False
item_date = parse_date(str(item.get("date", "")))
if item_date is None:
return False
if start is not None and item_date < start:
return False
if end is not None and item_date > end:
return False
return True
def list_expenses(expenses: List[Dict[str, Any]]) -> None:
"""Print expenses, optionally filtered and sorted by date descending."""
if not expenses:
print("No expenses yet.")
return
category = input("Filter by category (blank = no filter): ").strip().lower() or None
start = prompt_date("Start date YYYY-MM-DD (blank = none): ", allow_blank=True)
end = prompt_date("End date YYYY-MM-DD (blank = none): ", allow_blank=True)
filtered = [e for e in expenses if matches_filters(e, category, start, end)]
def sort_key(e: Dict[str, Any]):
d = parse_date(str(e.get("date", ""))) or date.min
return (d, e.get("id", 0))
filtered.sort(key=sort_key, reverse=True)
if not filtered:
print("No expenses matched your filters.")
return
print("\nID Date Amount Category Note")
print("-- ---------- ------- ---------- -------------------------")
for e in filtered:
eid = str(e.get("id", "")).rjust(2)
d = str(e.get("date", "")).ljust(10)
amt = f"{float(e.get('amount', 0)):7.2f}"
cat = str(e.get("category", "")).ljust(10)
note = str(e.get("note", ""))[:25]
print(f"{eid} {d} {amt} {cat} {note}")
print(f"\nShown: {len(filtered)} expense(s)\n")
def summarize(expenses: List[Dict[str, Any]]) -> None:
"""Print total spending and totals per category (with optional date filtering)."""
if not expenses:
print("No expenses to summarize.")
return
start = prompt_date("Start date YYYY-MM-DD (blank = none): ", allow_blank=True)
end = prompt_date("End date YYYY-MM-DD (blank = none): ", allow_blank=True)
filtered = [e for e in expenses if matches_filters(e, None, start, end)]
total = 0.0
by_category: Dict[str, float] = {}
for e in filtered:
amount = float(e.get("amount", 0))
total += amount
cat = str(e.get("category", "uncategorized"))
by_category[cat] = by_category.get(cat, 0.0) + amount
print("\nSummary")
print("-------")
print(f"Total spent: ${total:.2f}")
if not by_category:
print("No category totals available.")
return
sorted_cats = sorted(by_category.items(), key=lambda x: x[1], reverse=True)
print("\nBy category:")
for cat, amt in sorted_cats:
print(f"- {cat}: ${amt:.2f}")
print("")
def delete_expense(expenses: List[Dict[str, Any]]) -> None:
"""Delete an expense by its ID."""
if not expenses:
print("No expenses to delete.")
return
raw = input("Enter expense ID to delete: ").strip()
if not raw.isdigit():
print("Please enter a valid integer ID.")
return
target_id = int(raw)
for i, e in enumerate(expenses):
if e.get("id") == target_id:
removed = expenses.pop(i)
save_expenses(DATA_FILE, expenses)
print(f"Deleted expense #{removed.get('id')} ✅")
return
print(f"No expense found with ID {target_id}.")
def print_menu() -> None:
"""Display the main menu."""
print("Personal Expense Tracker")
print("------------------------")
print("1) Add expense")
print("2) List expenses")
print("3) Summary")
print("4) Delete expense")
print("5) Exit")
def main() -> None:
"""Program entry point."""
expenses = load_expenses(DATA_FILE)
while True:
print_menu()
choice = input("Choose an option (1-5): ").strip()
if choice == "1":
add_expense(expenses)
elif choice == "2":
list_expenses(expenses)
elif choice == "3":
summarize(expenses)
elif choice == "4":
delete_expense(expenses)
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid choice. Please choose 1-5.\n")
if __name__ == "__main__":
main()