Adds Transactions derived from list
New `Transactions` class derived from `list` to add auxilliary methods to a transaction list. New get_transactions_by_(year,month,category) methods return dict sorted by (year,month,category). Also adds sort_by_bank inplace.
This commit is contained in:
parent
2eecd2191a
commit
7a587213c9
22
main.py
22
main.py
@ -4,7 +4,7 @@ import pickle
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from categories import Categories
|
from categories import Categories
|
||||||
from transaction import Transaction as Tr, TransactionError
|
from transaction import Transaction as Tr, TransactionError, Transactions
|
||||||
from parsers import Parser
|
from parsers import Parser
|
||||||
|
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ def initialize(raw_dir, data_dir, restart=False):
|
|||||||
|
|
||||||
|
|
||||||
def manual_categorization(trs):
|
def manual_categorization(trs):
|
||||||
trs = Tr.sort_by_bank(trs)
|
trs.sort_by_bank()
|
||||||
for i, transaction in enumerate(trs):
|
for i, transaction in enumerate(trs):
|
||||||
if not transaction.category:
|
if not transaction.category:
|
||||||
category = input(f"{transaction} category: ")
|
category = input(f"{transaction} category: ")
|
||||||
@ -108,7 +108,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
datafiles = initialize(".raw", "data", restart=False)
|
datafiles = initialize(".raw", "data", restart=False)
|
||||||
|
|
||||||
transactions = list()
|
transactions = Transactions()
|
||||||
for file in datafiles.values():
|
for file in datafiles.values():
|
||||||
transactions.extend(file)
|
transactions.extend(file)
|
||||||
transactions.sort()
|
transactions.sort()
|
||||||
@ -119,12 +119,12 @@ if __name__ == "__main__":
|
|||||||
# if transaction.category in reprocess:
|
# if transaction.category in reprocess:
|
||||||
# transaction.category = ''
|
# transaction.category = ''
|
||||||
|
|
||||||
Categories.categorize(transactions)
|
# Categories.categorize(transactions)
|
||||||
|
#
|
||||||
manual_categorization(transactions)
|
# manual_categorization(transactions)
|
||||||
|
#
|
||||||
for f, file in datafiles.items():
|
# for f, file in datafiles.items():
|
||||||
file_transactions = [t for t in transactions if t in file]
|
# file_transactions = [t for t in transactions if t in file]
|
||||||
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)
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
from categories import Categories
|
||||||
from csv import reader, writer
|
from csv import reader, writer
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from decimal import Decimal, InvalidOperation
|
from decimal import Decimal, InvalidOperation
|
||||||
@ -89,4 +90,62 @@ class Transaction:
|
|||||||
return self.date >= other.date
|
return self.date >= other.date
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"{self.date} {self.description} {self.value}€ from {self.bank} ({self.category})"
|
return "{} {} {}€ ({})".format(
|
||||||
|
self.date.strftime("%d/%m/%y"), self.category, self.value, self.bank
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Transactions(list):
|
||||||
|
def sort_by_bank(self):
|
||||||
|
self.sort(key=lambda k: k.bank)
|
||||||
|
|
||||||
|
def get_transactions_by_year(self, start=None, end=None):
|
||||||
|
if not start:
|
||||||
|
start = self[0].date
|
||||||
|
if not end:
|
||||||
|
end = self[-1].date
|
||||||
|
|
||||||
|
years = dict()
|
||||||
|
for year in range(start.year, end.year + 1):
|
||||||
|
years[year] = Transactions(
|
||||||
|
t for t in self if start <= t.date <= end and t.date.year == year
|
||||||
|
)
|
||||||
|
|
||||||
|
return years
|
||||||
|
|
||||||
|
def get_transactions_by_month(self, start=None, end=None):
|
||||||
|
if not start:
|
||||||
|
start = self[0].date
|
||||||
|
if not end:
|
||||||
|
end = self[-1].date
|
||||||
|
|
||||||
|
months = dict()
|
||||||
|
for year, year_transactions in self.get_transactions_by_year(
|
||||||
|
start, end
|
||||||
|
).items():
|
||||||
|
for month in range(1, 13):
|
||||||
|
key = "_".join([str(year), str(month)])
|
||||||
|
months[key] = Transactions(
|
||||||
|
t for t in year_transactions if t.date.month == month
|
||||||
|
)
|
||||||
|
|
||||||
|
# trims last unused months
|
||||||
|
trim = 1
|
||||||
|
for transactions in reversed(months.values()):
|
||||||
|
if transactions:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
trim += 1
|
||||||
|
while trim := trim - 1:
|
||||||
|
months.popitem()
|
||||||
|
|
||||||
|
return months
|
||||||
|
|
||||||
|
def get_transactions_by_category(self):
|
||||||
|
categories = {cat: [] for cat in Categories.get_categories_names()}
|
||||||
|
for transaction in self:
|
||||||
|
try:
|
||||||
|
categories[transaction.category].append(transaction)
|
||||||
|
except AttributeError:
|
||||||
|
categories[transaction.category] = [transaction]
|
||||||
|
return categories
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user