From ad9bc4a7c4aa27a1c82d1a7508acf3da59d9d225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Murta?= Date: Wed, 9 Sep 2020 21:03:50 +0100 Subject: [PATCH] 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. --- main.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/main.py b/main.py index 7c43a06..3067300 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,7 @@ +from datetime import date from pathlib import Path import logging +import matplotlib.pyplot as plt import pickle import sys @@ -128,3 +130,90 @@ if __name__ == "__main__": # Tr.write_transactions(Path("data") / f, file_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) + ) + )