Changeset - c3fd55ec15b7
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-05-19 19:22:00
brettcsmith@brettcsmith.org
historical: Beancount can handle commas in amounts.

And having it looks nicer, is more consistent with our historical
books, is less code for me, and is no more trouble for the user.
2 files changed with 3 insertions and 4 deletions:
0 comments (0 inline, 0 general)
oxrlib/commands/historical.py
Show inline comments
...
 
@@ -167,31 +167,31 @@ class LedgerFormatter(Formatter):
 
    def format_conversion(self, from_amt, from_curr, to_curr):
 
        to_amt = self.cost_rates.convert(from_amt, from_curr, to_curr)
 
        return "{}\n{}".format(
 
            self.format_denominated_rate(from_amt, from_curr, to_curr),
 
            self.format_denominated_rate(to_amt, to_curr, None),
 
        )
 

	
 

	
 
class BeancountFormatter(LedgerFormatter):
 
    COST_FMT = '{{{}}}'
 

	
 
    def __init__(self, cost_rates, price_rates=None,
 
                 signed_currencies=(), base_fmt='###0.###',
 
                 signed_currencies=(), base_fmt='#,##0.###',
 
                 rate_precision=5, denomination=None):
 
        super().__init__(
 
            cost_rates,
 
            price_rates,
 
            (),
 
            base_fmt.replace(',', ''),
 
            base_fmt,
 
            rate_precision,
 
            denomination,
 
        )
 

	
 
    def price_rate(self, from_amt, from_curr, to_curr):
 
        if self.price_rates is None:
 
            return None
 
        else:
 
            return self.price_rates.convert(from_amt, from_curr, to_curr)
 

	
 

	
 
class Formats(enum.Enum):
tests/test_historical.py
Show inline comments
...
 
@@ -86,45 +86,44 @@ def lines_from_run(config, output):
 
def check_fx_amount(config, lines, amount, cost, fx_code, fx_sign=None, price=None):
 
    if price is None:
 
        price = cost
 
    rate_fmt = f'{{}} {re.escape(fx_code)}'
 
    cost = re.escape(cost) + r'\d*'
 
    price = re.escape(price) + r'\d*'
 
    if config.args.output_format is oxrhist.Formats.LEDGER:
 
        if fx_sign is not None and fx_code in config.args.signed_currencies:
 
            rate_fmt = f'{re.escape(fx_sign)}{{}}'
 
        cost_re = '{{={}}}'.format(rate_fmt.format(cost))
 
        price_re = ' @ {}'.format(rate_fmt.format(price))
 
    else:
 
        amount = amount.replace(',', '')
 
        cost_re = '{{{}}}'.format(rate_fmt.format(cost))
 
        if config.args.from_date is None:
 
            price_re = ''
 
        else:
 
            price_re = ' @ {}'.format(rate_fmt.format(price))
 
    pattern = r'^{} {}{}$'.format(re.escape(amount), cost_re, price_re)
 
    line = next(lines, "<EOF>")
 
    assert re.match(pattern, line)
 

	
 
def check_nonfx_amount(config, lines, amount, code=None, sign=None):
 
    if config.args.output_format is oxrhist.Formats.LEDGER:
 
        if code is None:
 
            code = 'USD'
 
            sign = '$'
 
        if code in config.args.signed_currencies and sign is not None:
 
            expected = f'{sign}{amount}\n'
 
        else:
 
            expected = f'{amount} {code}\n'
 
    else:
 
        expected = f'{amount.replace(",", "")} {code or "USD"}\n'
 
        expected = f'{amount} {code or "USD"}\n'
 
    assert next(lines, "<EOF>") == expected
 

	
 
def test_rate_list(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date)
 
    lines = lines_from_run(config, output)
 
    assert next(lines).startswith('1 AED = 0.27229')
 
    assert next(lines) == '1 USD = 3.67246 AED\n'
 
    assert next(lines).startswith('1 ALL = 0.0069189')
 
    assert next(lines) == '1 USD = 144.529793 ALL\n'
 
    assert next(lines).startswith('1 ANG = 0.55865')
 
    assert next(lines) == '1 USD = 1.79 ANG\n'
 

	
0 comments (0 inline, 0 general)