Skip to content

Commit

Permalink
Don't use bisecect.insort, instead lazily sort when necessary
Browse files Browse the repository at this point in the history
Also adds some type annotations
  • Loading branch information
xentac committed Oct 10, 2022
1 parent 3f0070d commit a709aba
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions beancount_import/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def __init__(self, fuzzy_match_days: int,
self.is_cleared = is_cleared
self._postings = {} # type: Dict[DatabaseDateKey, DatabaseValues]
self._date_currency = collections.defaultdict(list) # type: Dict[DateCurrencyKey, List[SearchPosting]]
self._date_currency_dirty = collections.defaultdict(bool)
self._keyed_postings = {
} # type: Dict[DatabaseMetadataKey, DatabaseValues]
self.metadata_keys = metadata_keys
Expand All @@ -254,11 +255,11 @@ def get_fuzzy_date_range(self, orig_date: datetime.date):
self.fuzzy_match_days + 1):
yield orig_date + datetime.timedelta(days=day_offset)

def fuzz_date_currency_key(self, key: Tuple):
def fuzz_date_currency_key(self, key: DateCurrencyKey) -> Iterable[DateCurrencyKey]:
for d in self.get_fuzzy_date_range(key[0]):
yield d, key[1]

def _date_currency_key(self, entry: Transaction, mp: MatchablePosting):
def _date_currency_key(self, entry: Transaction, mp: MatchablePosting) -> DateCurrencyKey:
return (_date_key(entry, mp), get_posting_weight(mp.posting).currency)

def _search_posting(self, entry: Transaction, mp: MatchablePosting):
Expand Down Expand Up @@ -288,9 +289,14 @@ def add_posting(self, entry: Transaction, mp: MatchablePosting):
return

for dc in self.fuzz_date_currency_key(self._date_currency_key(entry, mp)):
bisect.insort(
self._date_currency[dc],
self._search_posting(entry, mp))
self._date_currency[dc].append(self._search_posting(entry, mp))
self._date_currency_dirty[dc] = True

def get_date_currency_postings(self, key: DateCurrencyKey) -> List[SearchPosting]:
if self._date_currency_dirty[key]:
self._date_currency[key].sort()
self._date_currency_dirty[key] = False
return self._date_currency[key]

def search_postings(self,
entry: Transaction,
Expand All @@ -306,14 +312,14 @@ def search_postings(self,
continue
if negate:
weight = -weight
bisect.insort(
postings_date_currency[self._date_currency_key(entry, mp)],
(weight, id(mp), mp.posting))
postings_date_currency[self._date_currency_key(entry, mp)].append((weight, id(mp), mp.posting))
for dc, items in postings_date_currency.items():
items.sort()

keys = sorted(postings_date_currency.keys())

for key in keys:
db = self._date_currency[key]
db = self.get_date_currency_postings(key)
s = slice(0,1)
for weight, _, p in postings_date_currency[key]:
# Do meta lookup first
Expand Down

0 comments on commit a709aba

Please sign in to comment.