Skip to content

Commit

Permalink
refactor: remove unnecessary usage of pydantic dataclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-awd committed Sep 4, 2024
1 parent 22fe99e commit 62ba9f2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/monopoly/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import traceback
from concurrent.futures import ProcessPoolExecutor
from dataclasses import dataclass, field
from pathlib import Path
from typing import Collection, Iterable, Optional, TypedDict

import click
from pydantic.dataclasses import Field, dataclass
from tabulate import tabulate
from tqdm import tqdm

Expand Down Expand Up @@ -41,7 +41,7 @@ class Result:

source_file_name: str
target_file_name: Optional[str] = None
error_info: dict[str, str] = Field(default_factory=dict)
error_info: dict[str, str] = field(default_factory=dict)


@dataclass
Expand Down
4 changes: 1 addition & 3 deletions src/monopoly/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from dataclasses import field
from dataclasses import dataclass, field
from typing import Optional, Pattern

from pydantic.dataclasses import dataclass

from monopoly.constants import BankNames, EntryType, InternalBankNames
from monopoly.enums import RegexEnum

Expand Down
14 changes: 7 additions & 7 deletions src/monopoly/generic/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def is_transaction_date_first(self) -> bool:
return True

@lru_cache
def create_transaction_pattern(self) -> str:
def create_transaction_pattern(self) -> re.Pattern:
"""
Create a regex pattern that will be used for date parsing
by the generic statement handler.
Expand Down Expand Up @@ -100,16 +100,16 @@ def create_transaction_pattern(self) -> str:
if self.get_statement_type() == EntryType.CREDIT:
pattern += SharedPatterns.AMOUNT_EXTENDED

return pattern
return re.compile(pattern, re.IGNORECASE)

@lru_cache
def create_statement_date_pattern(self) -> str:
def create_statement_date_pattern(self) -> re.Pattern:
"""
Creates a regex pattern for the statement date based on the first statement
date.
"""
statement_date = self.matcher.get_statement_date_pattern()
return f"({statement_date})"
return re.compile(f"({statement_date})")

@lru_cache
def get_statement_type(self) -> str:
Expand Down Expand Up @@ -186,7 +186,7 @@ def check_if_multiline(self) -> bool:
return average_line_distance > 2

@lru_cache
def create_previous_balance_regex(self) -> str | None:
def create_previous_balance_regex(self) -> re.Pattern | None:
"""Helper function to check for a previous balance line items.
Makes the assumption that the previous balance line item, if it
exists, will be the line before the first line item with a date.
Expand Down Expand Up @@ -218,13 +218,13 @@ def create_previous_balance_regex(self) -> str | None:
+ SharedPatterns.AMOUNT
)
logger.debug("Found words, generated pattern %s", pattern)
return pattern
return re.compile(pattern)
return None

@lru_cache
def get_first_transaction_location(self):
# uses the transaction pattern to find the first transaction
pattern = re.compile(self.create_transaction_pattern(), re.IGNORECASE)
pattern = self.create_transaction_pattern()

for page_num, page in enumerate(self.pages):
for line_num, line in enumerate(page.lines):
Expand Down
7 changes: 6 additions & 1 deletion src/monopoly/identifiers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic.dataclasses import dataclass
from dataclasses import dataclass


@dataclass
Expand All @@ -16,6 +16,11 @@ class MetadataIdentifier(Identifier):
subject: str = ""
creator: str = ""
producer: str = ""
keywords: str = ""
creationDate: str = ""
modDate: str = ""
trapped: str = ""
encryption: dict = None


@dataclass
Expand Down
5 changes: 4 additions & 1 deletion src/monopoly/statements/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ def subtotal_pattern(self) -> re.Pattern:

@property
def pattern(self):
return self.config.transaction_pattern
pattern = self.config.transaction_pattern
if isinstance(pattern, str):
pattern = re.compile(pattern)
return pattern

@lru_cache
def get_transactions(self) -> list[Transaction] | None:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/generic/test_date_pattern_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def test_create_transaction_pattern_with_transaction_first(
date_pattern_analyzer.matches = matches_with_transaction_posting_dates

result = date_pattern_analyzer.create_transaction_pattern()
assert result == expected
assert result == re.compile(expected, re.IGNORECASE)


def test_create_transaction_pattern_with_posting_first(
Expand All @@ -305,7 +305,7 @@ def test_create_transaction_pattern_with_posting_first(
+ SharedPatterns.AMOUNT_EXTENDED
)
result = date_pattern_analyzer.create_transaction_pattern()
assert result == expected
assert result == re.compile(expected, re.IGNORECASE)


def test_get_statement_type_debit(date_pattern_analyzer: DatePatternAnalyzer):
Expand Down

0 comments on commit 62ba9f2

Please sign in to comment.