Skip to content

Commit

Permalink
flake
Browse files Browse the repository at this point in the history
  • Loading branch information
redstreet committed Feb 5, 2021
1 parent b874459 commit 52d6576
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
"""See accompanying README.md"""

import collections
import re
import time

from beancount.core.amount import ZERO
from beancount.core import data
from beancount.core import account
from beancount.core import getters
from beancount.core import position
from beancount.core import flags
from beancount.ops import holdings
from beancount.parser import options
from beancount.parser import printer
from ast import literal_eval

DEBUG = 0

__plugins__ = ('capital_gains_classifier',)


def pretty_print_transaction(t):
print(t.date)
for p in t.postings:
Expand Down Expand Up @@ -48,7 +41,7 @@ def capital_gains_classifier(entries, options_map, config):
"Income.*Capital-Gains:Long:.*", ":Long:", ":Long:Losses:", ":Long:Gains:"
"Income.*Capital-Gains:Short:.*, ":Short:, ":Short:Losses:, ":Short:Gains:"
Returns:
A tuple of entries and errors. """

Expand All @@ -57,7 +50,7 @@ def capital_gains_classifier(entries, options_map, config):
new_accounts = []
errors = []

rewrites = literal_eval(config) #TODO: error check
rewrites = literal_eval(config) # TODO: error check

for entry in entries:
if isinstance(entry, data.Transaction):
Expand All @@ -69,9 +62,9 @@ def capital_gains_classifier(entries, options_map, config):
# matched = True
for r in rewrites:
if posting.units.number < 0:
account = account.replace(rewrites[r][0], rewrites[r][1]) #losses
account = account.replace(rewrites[r][0], rewrites[r][1]) # losses
else:
account = account.replace(rewrites[r][0], rewrites[r][2]) #gains
account = account.replace(rewrites[r][0], rewrites[r][2]) # gains
rewrite_count += 1
if account not in new_accounts:
new_accounts.append(account)
Expand All @@ -87,7 +80,6 @@ def capital_gains_classifier(entries, options_map, config):
return(new_open_entries + entries, errors)



def create_open_directives(new_accounts, entries):
meta = data.new_metadata('<zerosum>', 0)
# Ensure that the accounts we're going to use to book the postings exist, by
Expand Down
51 changes: 25 additions & 26 deletions beancount_reds_plugins/effective_date/effective_date.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
"""Beancount plugin to implement per-posting effective dates. See README.md for more."""

import collections
from beancount.core.amount import ZERO
from beancount.core import data
from beancount.core import account
from beancount.core import getters
from beancount.core import flags
from beancount.ops import holdings
from beancount.parser import options
from beancount.parser import printer
from ast import literal_eval
import copy
import datetime
Expand All @@ -24,29 +17,32 @@

LINK_FORMAT = 'edate-{date}-{random}'


def has_valid_effective_date(posting):
return posting.meta is not None and \
'effective_date' in posting.meta and \
type(posting.meta['effective_date']) == datetime.date


def has_posting_with_valid_effective_date(entry):
for posting in entry.postings:
if has_valid_effective_date(posting):
return True
return False


def create_new_effective_date_entry(entry, date, hold_posting, original_posting):
def cleaned(p):
clean_meta = copy.deepcopy(p.meta)
clean_meta.pop('effective_date', None)
return p._replace(meta=clean_meta)
clean_meta = copy.deepcopy(p.meta)
clean_meta.pop('effective_date', None)
return p._replace(meta=clean_meta)

new_meta = {'original_date': entry.date}
effective_date_entry = entry._replace(date=date,
meta={**entry.meta, **new_meta},
postings=[cleaned(hold_posting), cleaned(original_posting)])
effective_date_entry = entry._replace(date=date, meta={**entry.meta, **new_meta},
postings=[cleaned(hold_posting), cleaned(original_posting)])
return effective_date_entry


def build_config(config):
holding_accts = {}
if config:
Expand All @@ -59,6 +55,7 @@ def build_config(config):
}
return holding_accts


def effective_date(entries, options_map, config):
"""Effective dates
Expand Down Expand Up @@ -90,7 +87,7 @@ def effective_date(entries, options_map, config):
# for e in interesting_entries:
# printer.print_entry(e)
# print("------")

# add a link to each effective date entry. this gets copied over to the newly created effective date
# entries, and thus links each set of effective date entries
interesting_entries_linked = []
Expand All @@ -104,7 +101,6 @@ def effective_date(entries, options_map, config):
new_entries = []
for entry in interesting_entries_linked:
modified_entry_postings = []
effective_date_entry_postings = []
for posting in entry.postings:
if not has_valid_effective_date(posting):
modified_entry_postings += [posting]
Expand All @@ -126,7 +122,8 @@ def effective_date(entries, options_map, config):

# Create new entry at effective_date
hold_posting = new_posting._replace(units=-posting.units)
new_entry = create_new_effective_date_entry(entry, posting.meta['effective_date'], hold_posting, posting)
new_entry = create_new_effective_date_entry(entry, posting.meta['effective_date'],
hold_posting, posting)
new_entries.append(new_entry)
modified_entry = entry._replace(postings=modified_entry_postings)
new_entries.append(modified_entry)
Expand All @@ -144,6 +141,7 @@ def effective_date(entries, options_map, config):
retval = new_open_entries + new_entries + filtered_entries
return(retval, errors)


def effective_date_transaction(entries, options_map, config):
"""Effective dates
Expand Down Expand Up @@ -207,7 +205,7 @@ def effective_date_transaction(entries, options_map, config):
new_accounts = []
for entry in entries:
outlist = (interesting_entries
if (isinstance(entry, data.Transaction) and
if (isinstance(entry, data.Transaction) and
'effective_date' in entry.meta and
type(entry.meta['effective_date']) == datetime.date)
else filtered_entries)
Expand Down Expand Up @@ -247,9 +245,9 @@ def effective_date_transaction(entries, options_map, config):
if new_posting.account not in new_accounts:
new_accounts.append(new_posting.account)
modified_entry_postings += [new_posting]
effective_date_entry_postings += [posting]
effective_date_entry_postings += [posting]

effective_date_entry_postings += [posting._replace(
effective_date_entry_postings += [posting._replace(
account=posting.account.replace(found_acct, holding_account),
units=-posting.units)]

Expand All @@ -265,7 +263,7 @@ def effective_date_transaction(entries, options_map, config):
# modified_entry = data.entry_replace(entry, postings=modified_entry_postings,
# links=(entry.links or set()) | set([link]))
modified_entry = entry._replace(postings=modified_entry_postings,
links=(entry.links or set()) | set([link]))
links=(entry.links or set()) | set([link]))

effective_date_entry_narration = entry.narration + " (originally: {})".format(str(entry.date))
# effective_date_entry = data.entry_replace(entry, date=entry.meta['effective_date'],
Expand All @@ -274,10 +272,10 @@ def effective_date_transaction(entries, options_map, config):
# links=(entry.links or set()) | set([link]))
new_meta = {'original_date': entry.date}
effective_date_entry = entry._replace(date=entry.meta['effective_date'],
meta={**entry.meta, **new_meta},
postings=effective_date_entry_postings,
narration = effective_date_entry_narration,
links=(entry.links or set()) | set([link]))
meta={**entry.meta, **new_meta},
postings=effective_date_entry_postings,
narration=effective_date_entry_narration,
links=(entry.links or set()) | set([link]))
new_entries += [modified_entry, effective_date_entry]
else:
new_entries += [entry]
Expand All @@ -294,6 +292,7 @@ def effective_date_transaction(entries, options_map, config):
retval = new_open_entries + new_entries + filtered_entries
return(retval, errors)


def create_open_directives(new_accounts, entries):
if not entries:
return []
Expand All @@ -315,9 +314,9 @@ def create_open_directives(new_accounts, entries):
# -----------------------------------------------------------------------------------------------------------
# Bug:
# below will fail because expense account was opened too late in the source:
#2014-01-01 open Expenses:Taxes:Federal
# 2014-01-01 open Expenses:Taxes:Federal
#
#2014-02-01 * "Estimated taxes for 2013"
# 2014-02-01 * "Estimated taxes for 2013"
# Liabilities:Mastercard -2000 USD
# Expenses:Taxes:Federal 2000 USD
# effective_date: 2013-12-31
16 changes: 2 additions & 14 deletions beancount_reds_plugins/effective_date/test_effective_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@

# from plugins.beancount_plugins_redstreet import effective_date
import effective_date
from beancount.core.number import D
from beancount.core.number import ZERO
from beancount.core import data
from beancount.parser import options
from beancount.ops import validation
from beancount import loader
import datetime

Expand All @@ -37,7 +34,6 @@ def test_empty_entries(self):
entries, _ = effective_date.effective_date([], options.OPTIONS_DEFAULTS.copy(), None)
self.assertEqual([], entries)


@loader.load_doc()
def test_no_effective_dates(self, entries, _, options_map):
"""
Expand Down Expand Up @@ -76,8 +72,8 @@ def test_expense_earlier(self, entries, _, options_map):
self.assertEqual(5, len(new_entries))

results = get_entries_with_narration(new_entries, "Estimated taxes")
self.assertEqual(datetime.date(2013,12,31), results[0].date)
self.assertEqual(datetime.date(2014,2,1), results[1].date)
self.assertEqual(datetime.date(2013, 12, 31), results[0].date)
self.assertEqual(datetime.date(2014, 2, 1), results[1].date)

# self.assertEqual('Assets:Account1', results.postings[0].account)
# self.assertEqual('Income:Account1', results.postings[1].account)
Expand All @@ -86,16 +82,12 @@ def test_expense_earlier(self, entries, _, options_map):
# self.assertEqual(2, len(mansion.postings))
# self.assertEqual(D('-100'), mansion.postings[0].units.number)


# entry = get_entries_with_narration(unreal_entries, '3 units')[0]
# self.assertEqual("Equity:Account1:Gains", entry.postings[0].account)
# self.assertEqual("Income:Account1:Gains", entry.postings[1].account)
# self.assertEqual(D("24.00"), entry.postings[0].units.number)
# self.assertEqual(D("-24.00"), entry.postings[1].units.number)




# def test_expense_later(self, entries, _, options_map):
# """
# 2014-01-01 open Liabilities:Mastercard
Expand All @@ -116,8 +108,6 @@ def test_expense_earlier(self, entries, _, options_map):
# # Liabilities:Hold:Rent -2000 USD
# # Expenses:Rent 2000 USD



@loader.load_doc()
def test_expense_later_multiple(self, entries, _, options_map):
"""
Expand Down Expand Up @@ -153,5 +143,3 @@ def test_expense_later_multiple(self, entries, _, options_map):

new_entries, _ = effective_date.effective_date(entries, options_map, None)
self.assertEqual(7, len(new_entries))


1 change: 1 addition & 0 deletions beancount_reds_plugins/zerosum/test_zerosum.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'tolerance' : 0.0098,
}"""


def get_entries_with_acc_regexp(entries, regexp):
print(regexp)
return [entry
Expand Down
11 changes: 5 additions & 6 deletions beancount_reds_plugins/zerosum/zerosum.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,9 @@
"""

import time
import collections
from ast import literal_eval
import datetime
from collections import defaultdict
import cProfile, pstats

from beancount.core import data
from beancount.core import flags
Expand All @@ -177,6 +175,7 @@

__plugins__ = ('zerosum', 'flag_unmatched',)


# replace the account on a given posting with a new account
def account_replace(txn, posting, new_account):
# create a new posting with the new account, then remove old and add new
Expand All @@ -185,6 +184,7 @@ def account_replace(txn, posting, new_account):
txn.postings.remove(posting)
txn.postings.append(new_posting)


def zerosum(entries, options_map, config):
"""Insert entries for unmatched transactions in zero-sum accounts.
Expand Down Expand Up @@ -228,7 +228,7 @@ def find_match():
# Don't match with the same exact posting.
continue
if (abs(p.units.number + posting.units.number) < tolerance
and p.account == zs_account):
and p.account == zs_account):
return (p, t)
return None

Expand All @@ -237,7 +237,7 @@ def find_match():
# pr.enable()
start_time = time.time()

config_obj = literal_eval(config) #TODO: error check
config_obj = literal_eval(config) # TODO: error check
zs_accounts_list = config_obj.pop('zerosum_accounts', {})
(account_name_from, account_name_to) = config_obj.pop('account_name_replace', ('', ''))
tolerance = config_obj.pop('tolerance', DEFAULT_TOLERANCE)
Expand Down Expand Up @@ -266,7 +266,7 @@ def find_match():
for i in range(len(zerosum_txns)):
txn = zerosum_txns[i]
reprocess = True
while reprocess: # necessary since this entry's postings changes under us when we find a match
while reprocess: # necessary since this entry's postings changes under us when we find a match
for posting in txn.postings:
reprocess = False
if posting.account == zs_account:
Expand Down Expand Up @@ -330,4 +330,3 @@ def create_open_directives(new_accounts, entries):
open_entry = data.Open(meta, earliest_date, account_, None, None)
new_open_entries.append(open_entry)
return(new_open_entries)

0 comments on commit 52d6576

Please sign in to comment.