Skip to content

Commit

Permalink
Upgarde for beancount3/beangulp fixes #135
Browse files Browse the repository at this point in the history
  • Loading branch information
tarioch committed Dec 30, 2024
1 parent f692d96 commit 09dba77
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Changelog
=========
v0.6 (2024-12-27)
-----------------

Upgrade to beancount 3 and beangulp.


v0.5 (2024-01-21)
-----------------
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ packages = find:
setup_requires =
setuptools_scm
install_requires =
beancount>=2.3.5,<3.0.0
beancount>=3
beangulp
scikit-learn>=1.0
numpy>=1.18.0

Expand Down
2 changes: 1 addition & 1 deletion smart_importer/detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging

from beancount.ingest import similar
from beangulp import similar

from smart_importer.hooks import ImporterHook

Expand Down
8 changes: 3 additions & 5 deletions smart_importer/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ def apply_hooks(importer, hooks):
unpatched_extract = importer.extract

@wraps(unpatched_extract)
def patched_extract_method(file, existing_entries=None):
def patched_extract_method(filepath, existing=None):
logger.debug("Calling the importer's extract method.")
imported_entries = unpatched_extract(
file, existing_entries=existing_entries
)
imported_entries = unpatched_extract(filepath, existing=existing)

for hook in hooks:
imported_entries = hook(
importer, file, imported_entries, existing_entries
importer, filepath, imported_entries, existing
)

return imported_entries
Expand Down
2 changes: 1 addition & 1 deletion smart_importer/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __call__(self, importer, file, imported_entries, existing_entries):
A list of entries, modified by this predictor.
"""
logging.debug("Running %s for file %s", self.__class__.__name__, file)
self.account = importer.file_account(file)
self.account = importer.account(file)
self.load_training_data(existing_entries)
with self.lock:
self.define_pipeline()
Expand Down
8 changes: 4 additions & 4 deletions tests/data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import pytest
from beancount.core.compare import stable_hash_namedtuple
from beancount.ingest.importer import ImporterProtocol
from beancount.parser import parser
from beangulp import Importer

from smart_importer import PredictPostings, apply_hooks

Expand Down Expand Up @@ -51,14 +51,14 @@ def test_testset(testset, string_tokenizer):
# pylint: disable=unbalanced-tuple-unpacking
imported, training_data, expected = _load_testset(testset)

class DummyImporter(ImporterProtocol):
def extract(self, file, existing_entries=None):
class DummyImporter(Importer):
def extract(self, filepath, existing=None):
return imported

importer = DummyImporter()
apply_hooks(importer, [PredictPostings(string_tokenizer=string_tokenizer)])
imported_transactions = importer.extract(
"dummy-data", existing_entries=training_data
"dummy-data", existing=training_data
)

for txn1, txn2 in zip(imported_transactions, expected):
Expand Down
26 changes: 12 additions & 14 deletions tests/predictors_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Tests for the `PredictPayees` and the `PredictPostings` decorator"""

# pylint: disable=missing-docstring
from beancount.ingest.importer import ImporterProtocol
from beancount.parser import parser
from beangulp import Importer

from smart_importer import PredictPayees, PredictPostings
from smart_importer.hooks import apply_hooks
Expand Down Expand Up @@ -132,16 +132,16 @@
DENYLISTED_ACCOUNTS = ["Expenses:Denylisted"]


class BasicTestImporter(ImporterProtocol):
def extract(self, file, existing_entries=None):
if file == "dummy-data":
class BasicTestImporter(Importer):
def extract(self, filepath, existing=None):
if filepath == "dummy-data":
return TEST_DATA
if file == "empty":
if filepath == "empty":
return []
assert False
return []

def file_account(self, file):
def account(self, filepath):
return "Assets:US:BofA:Checking"


Expand All @@ -166,8 +166,8 @@ def test_no_transactions():
"""
POSTING_IMPORTER.extract("empty")
PAYEE_IMPORTER.extract("empty")
POSTING_IMPORTER.extract("empty", existing_entries=TRAINING_DATA)
PAYEE_IMPORTER.extract("empty", existing_entries=TRAINING_DATA)
POSTING_IMPORTER.extract("empty", existing=TRAINING_DATA)
PAYEE_IMPORTER.extract("empty", existing=TRAINING_DATA)


def test_unchanged_narrations():
Expand All @@ -178,7 +178,7 @@ def test_unchanged_narrations():
extracted_narrations = [
transaction.narration
for transaction in PAYEE_IMPORTER.extract(
"dummy-data", existing_entries=TRAINING_DATA
"dummy-data", existing=TRAINING_DATA
)
]
assert extracted_narrations == correct_narrations
Expand All @@ -194,7 +194,7 @@ def test_unchanged_first_posting():
extracted_first_postings = [
transaction.postings[0]
for transaction in PAYEE_IMPORTER.extract(
"dummy-data", existing_entries=TRAINING_DATA
"dummy-data", existing=TRAINING_DATA
)
]
assert extracted_first_postings == correct_first_postings
Expand All @@ -204,9 +204,7 @@ def test_payee_predictions():
"""
Verifies that the decorator adds predicted postings.
"""
transactions = PAYEE_IMPORTER.extract(
"dummy-data", existing_entries=TRAINING_DATA
)
transactions = PAYEE_IMPORTER.extract("dummy-data", existing=TRAINING_DATA)
predicted_payees = [transaction.payee for transaction in transactions]
assert predicted_payees == PAYEE_PREDICTIONS

Expand All @@ -218,7 +216,7 @@ def test_account_predictions():
predicted_accounts = [
entry.postings[-1].account
for entry in POSTING_IMPORTER.extract(
"dummy-data", existing_entries=TRAINING_DATA
"dummy-data", existing=TRAINING_DATA
)
]
assert predicted_accounts == ACCOUNT_PREDICTIONS

0 comments on commit 09dba77

Please sign in to comment.