filters: remove_opening_balance_txn does replacement instead of del.

This commit is contained in:
Brett Smith 2020-06-04 09:49:39 -04:00
parent 5601ece2ac
commit 95ba1638d2
2 changed files with 13 additions and 2 deletions

View file

@ -87,5 +87,12 @@ def remove_opening_balance_txn(entries: Entries) -> Optional[Transaction]:
break break
else: else:
return None return None
del entries[index] # Deleting from the beginning of a list is O(n) slow in Python:
# <https://wiki.python.org/moin/TimeComplexity>
# So don't do that, and instead replace the transaction with a placeholder
# directive.
# The type:ignore is because of the funny way Beancount builds directives.
entries[index] = bc_data.Custom( # type:ignore[operator]
entry.meta, entry.date, "Removed opening balances", [],
)
return entry return entry

View file

@ -153,5 +153,9 @@ def test_remove_opening_balance_txn(opening_txn):
entries.insert(1, opening_txn) entries.insert(1, opening_txn)
actual = filters.remove_opening_balance_txn(entries) actual = filters.remove_opening_balance_txn(entries)
assert actual is opening_txn assert actual is opening_txn
assert len(entries) == 2
assert opening_txn not in entries assert opening_txn not in entries
assert not any(
post.account.startswith('Equity:')
for entry in entries
for post in getattr(entry, 'postings', ())
)