generated from felixm/defaultpy
Implement fuzzy category input
This commit is contained in:
26
src/predict.py
Normal file
26
src/predict.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from src.models import Transaction, UNKNOWN_CATEGORY
|
||||
from src.fzf import iterfzf
|
||||
from typing import List
|
||||
|
||||
|
||||
def get_categories(transactions: List[Transaction]) -> List[str]:
|
||||
categories = set([t.account2 for t in transactions])
|
||||
categories.discard(UNKNOWN_CATEGORY)
|
||||
return list(categories)
|
||||
|
||||
|
||||
def add_account2(transactions: List[Transaction]):
|
||||
categories = get_categories(transactions)
|
||||
unmapped_transactions = filter(lambda t: t.account2 == UNKNOWN_CATEGORY, transactions)
|
||||
for t in unmapped_transactions:
|
||||
add_account2_interactive(t, categories)
|
||||
|
||||
|
||||
def add_account2_interactive(transaction: Transaction, categories: List[str]):
|
||||
t = transaction
|
||||
account2 = None
|
||||
prompt = f"{t.account1} {t.date} {t.description} {t.debit} > "
|
||||
while account2 is None:
|
||||
account2 = iterfzf(categories, prompt=prompt)
|
||||
transaction.account2 = account2
|
||||
print(f"Assigned category '{account2}'.")
|
||||
Reference in New Issue
Block a user