budget/pfbudget/utils/converters.py
Luís Murta ad3fe02e4f
Adds new JSON parser and moves init to Manager
Adds a new abstract `Input` interface for the manager to use different
input handlers with the same methods. The Nordigen class and a new JSON
parser descend from it. The previous csv parser will also eventually.
New converter for list input also added.

Issue #19
2022-10-06 22:22:55 +01:00

29 lines
606 B
Python

from functools import singledispatch
from pfbudget.core.transactions import Transaction, TransactionError, Transactions
@singledispatch
def convert(t):
pass
@convert.register
def _(t: Transaction) -> list:
return (t.date, t.description, t.bank, t.value, t.category)
@convert.register
def _(t: list) -> Transaction:
try:
return Transaction(t)
except TransactionError:
print(f"{t} is in the wrong format")
def convert_transactions(transactions) -> list[list]:
return [convert(c) for c in transactions]
convert.register(type(Transactions), convert_transactions)