Adds plot with monthly income/spending
It separates the monthly transactions by categories and tracks income and spending along the defined time range. Now prints the total income/spending and the net result for the define period of time. Plot code is surrounded with an if False for future toggle on/off.
This commit is contained in:
parent
7a587213c9
commit
ad9bc4a7c4
89
main.py
89
main.py
@ -1,5 +1,7 @@
|
|||||||
|
from datetime import date
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import logging
|
import logging
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
import pickle
|
import pickle
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@ -128,3 +130,90 @@ if __name__ == "__main__":
|
|||||||
# Tr.write_transactions(Path("data") / f, file_transactions)
|
# Tr.write_transactions(Path("data") / f, file_transactions)
|
||||||
#
|
#
|
||||||
Tr.write_transactions("transactions.csv", transactions)
|
Tr.write_transactions("transactions.csv", transactions)
|
||||||
|
|
||||||
|
monthly_transactions = transactions.get_transactions_by_month(
|
||||||
|
start=date(2020, 1, 1), end=date(2020, 8, 31)
|
||||||
|
)
|
||||||
|
monthly_transactions_by_cat = []
|
||||||
|
for month_transactions in monthly_transactions.values():
|
||||||
|
cat = month_transactions.get_transactions_by_category()
|
||||||
|
monthly_transactions_by_cat.append(cat)
|
||||||
|
|
||||||
|
for month, month_transactions in zip(
|
||||||
|
monthly_transactions.keys(), monthly_transactions_by_cat
|
||||||
|
):
|
||||||
|
nulls = sum(t.value for t in month_transactions["Null"])
|
||||||
|
if nulls != 0:
|
||||||
|
print(f"{month} {nulls}")
|
||||||
|
|
||||||
|
expense_categories = [
|
||||||
|
*Categories.get_fixed_expenses(),
|
||||||
|
*Categories.get_variable_expenses(),
|
||||||
|
*Categories.get_discretionary_expenses(),
|
||||||
|
]
|
||||||
|
|
||||||
|
if False:
|
||||||
|
t = list(monthly_transactions.keys())
|
||||||
|
income = [
|
||||||
|
float(
|
||||||
|
sum(
|
||||||
|
t.value
|
||||||
|
for cat, transactions in months.items()
|
||||||
|
for t in transactions
|
||||||
|
if cat in Categories.get_income_categories()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
for months in monthly_transactions_by_cat
|
||||||
|
]
|
||||||
|
# income = []
|
||||||
|
# for months in monthly_transactions_by_cat:
|
||||||
|
# for cat, transactions in months.items():
|
||||||
|
# if cat in Categories.get_income_categories():
|
||||||
|
# income.append(sum(transactions))
|
||||||
|
|
||||||
|
expenses = []
|
||||||
|
for category in expense_categories:
|
||||||
|
expense_value = [
|
||||||
|
-float(sum(t.value for t in month[category]))
|
||||||
|
for month in monthly_transactions_by_cat
|
||||||
|
]
|
||||||
|
expenses.append(expense_value)
|
||||||
|
# expenses = [transactions for months in monthly_transactions_by_cat for cat, transactions in months.items()
|
||||||
|
# if cat not in Categories.get_income_categories() and transactions]
|
||||||
|
for expense in expenses:
|
||||||
|
for i, month in reversed(list(enumerate(t))):
|
||||||
|
if expense[i] < 0:
|
||||||
|
if i - 1 < 0:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
expense[i - 1] += expense[i]
|
||||||
|
expense[i] = 0
|
||||||
|
|
||||||
|
plt.plot(t, income, label="Income")
|
||||||
|
plt.stackplot(t, expenses, labels=expense_categories)
|
||||||
|
plt.legend(loc="upper left")
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
income = [
|
||||||
|
sum(
|
||||||
|
t.value
|
||||||
|
for cat, transactions in months.items()
|
||||||
|
for t in transactions
|
||||||
|
if cat in Categories.get_income_categories()
|
||||||
|
)
|
||||||
|
for months in monthly_transactions_by_cat
|
||||||
|
]
|
||||||
|
|
||||||
|
expenses = []
|
||||||
|
for category in expense_categories:
|
||||||
|
expense_value = [
|
||||||
|
-sum(t.value for t in month[category])
|
||||||
|
for month in monthly_transactions_by_cat
|
||||||
|
]
|
||||||
|
expenses.extend(expense_value)
|
||||||
|
|
||||||
|
print(
|
||||||
|
"Income: {}, Expenses: {}, Net = {}".format(
|
||||||
|
sum(income), sum(expenses), sum(income) - sum(expenses)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user