Changeset - 2d753c31aacd
[Not reviewed]
0 2 1
Brett Smith - 4 years ago 2020-05-17 17:32:32
brettcsmith@brettcsmith.org
historical: Add support for from_date arg.
3 files changed with 146 insertions and 62 deletions:
0 comments (0 inline, 0 general)
oxrlib/commands/historical.py
Show inline comments
...
 
@@ -14,5 +14,7 @@ except ImportError:
 
class Formatter:
 
    def __init__(self, rate, signed_currencies=(), base_fmt='#,##0.###',
 
    def __init__(self, cost_rates, price_rates=None,
 
                 signed_currencies=(), base_fmt='#,##0.###',
 
                 rate_precision=5, denomination=None):
 
        self.rate = rate
 
        self.cost_rates = cost_rates
 
        self.price_rates = price_rates
 
        self.base_fmt = base_fmt
...
 
@@ -50,3 +52,3 @@ class Formatter:
 
        from_amt = 1
 
        to_amt = self.rate.convert(from_amt, from_curr, to_curr)
 
        to_amt = self.cost_rates.convert(from_amt, from_curr, to_curr)
 
        return "{} {} = {} {}".format(
...
 
@@ -64,3 +66,3 @@ class Formatter:
 
    def format_conversion(self, from_amt, from_curr, to_curr):
 
        to_amt = self.rate.convert(from_amt, from_curr, to_curr)
 
        to_amt = self.cost_rates.convert(from_amt, from_curr, to_curr)
 
        return "{} = {}".format(
...
 
@@ -72,2 +74,12 @@ class Formatter:
 
class LedgerFormatter(Formatter):
 
    COST_FMT = '{{={}}}'
 
    PRICE_FMT = ' @ {}'
 

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

	
 
    def can_sign_currency(self, code):
...
 
@@ -89,11 +101,23 @@ class LedgerFormatter(Formatter):
 

	
 
    def format_rate(self, rate):
 
        return str(self.normalize_rate(rate))
 

	
 
    def format_ledger_rate_raw(self, rate, curr):
 
        rate_s = self.format_currency(rate, curr, currency_digits=False)
 
        return "{{={0}}} @ {0}".format(rate_s)
 
    def normalize_enough(self, rate, curr, from_amt, to_amt, prec=None):
 
        if prec is None:
 
            prec = self.rate_prec
 
        # Starting from prec, find the least amount of precision to
 
        # make sure from_amt converts exactly to to_amt.
 
        for try_prec in itertools.count(prec):
 
            try_rate = self.normalize_rate(rate, try_prec)
 
            got_amt = self.currency_decimal(from_amt * try_rate, curr)
 
            # If got_amt == to_amt, this is enough precision to do the
 
            # conversion exactly, so we're done.
 
            # If try_rate == rate, there's no more precision available, so stop.
 
            if (got_amt == to_amt) or (try_rate == rate):
 
                break
 
        return try_rate
 

	
 
    def format_ledger_rate(self, rate, curr):
 
        return self.format_ledger_rate_raw(self.normalize_rate(rate), curr)
 
    def _pretty_rate(self, fmt, rate, curr, from_amt=None, to_amt=None):
 
        if to_amt is None:
 
            rate = self.normalize_rate(rate)
 
        else:
 
            rate = self.normalize_enough(rate, curr, from_amt, to_amt)
 
        return fmt.format(self.format_currency(rate, curr, currency_digits=False))
 

	
...
 
@@ -101,5 +125,14 @@ class LedgerFormatter(Formatter):
 
        from_amt = 1
 
        to_amt = self.rate.convert(from_amt, from_curr, to_curr)
 
        return "{} {} {}".format(
 
            from_amt, from_curr, self.format_ledger_rate(to_amt, to_curr))
 
        cost = self.cost_rates.convert(from_amt, from_curr, to_curr)
 
        price = self.price_rate(from_amt, from_curr, to_curr)
 
        if price is None:
 
            price_s = ''
 
        else:
 
            price_s = self._pretty_rate(self.PRICE_FMT, price, to_curr)
 
        return "{} {} {}{}".format(
 
            from_amt,
 
            from_curr,
 
            self._pretty_rate(self.COST_FMT, cost, to_curr),
 
            price_s,
 
        )
 

	
...
 
@@ -118,18 +151,19 @@ class LedgerFormatter(Formatter):
 
            return amt_s
 
        full_rate = self.rate.convert(1, currency, denomination)
 
        # Starting from self.rate_prec, find the least amount of precision to
 
        # make sure the `from` amount converts exactly to the `to` amount.
 
        to_amt = self.currency_decimal(amount * full_rate, denomination)
 
        for prec in itertools.count(self.rate_prec):
 
            rate = self.normalize_rate(full_rate, prec)
 
            got_amt = self.currency_decimal(amount * rate, denomination)
 
            # If got_amt == to_amt, this is enough precision to do the
 
            # conversion exactly, so we're done.
 
            # If rate == full_rate, there's no more precision available, so stop.
 
            if (got_amt == to_amt) or (rate == full_rate):
 
                break
 
        return "{} {}".format(amt_s, self.format_ledger_rate_raw(rate, denomination))
 
        cost = self.cost_rates.convert(1, currency, denomination)
 
        price = self.price_rate(1, currency, denomination)
 
        to_amt = self.currency_decimal(amount * cost, denomination)
 
        if price is None:
 
            price_s = ''
 
        else:
 
            price_s = self._pretty_rate(
 
                self.PRICE_FMT, price, denomination, amount, to_amt,
 
            )
 
        return "{} {}{}".format(
 
            amt_s,
 
            self._pretty_rate(self.COST_FMT, cost, denomination, amount, to_amt),
 
            price_s,
 
        )
 

	
 
    def format_conversion(self, from_amt, from_curr, to_curr):
 
        to_amt = self.rate.convert(from_amt, from_curr, to_curr)
 
        to_amt = self.cost_rates.convert(from_amt, from_curr, to_curr)
 
        return "{}\n{}".format(
...
 
@@ -149,10 +183,19 @@ class Formats(enum.Enum):
 

	
 
def load_rates(config, loaders, date):
 
    with loaders.historical(date, config.args.base) as rate_json:
 
        rates = oxrrate.Rate.from_json_file(rate_json)
 
    if loaders.should_cache():
 
        config.cache.save_rate(rates)
 
    return rates
 

	
 
def run(config, stdout, stderr):
 
    loaders = config.get_loaders()
 
    with loaders.historical(config.args.date, config.args.base) as rate_json:
 
        rate = oxrrate.Rate.from_json_file(rate_json)
 
    if loaders.should_cache():
 
        config.cache.save_rate(rate)
 
    cost_rates = load_rates(config, loaders, config.args.date)
 
    if config.args.from_date is None:
 
        price_rates = None
 
    else:
 
        price_rates = load_rates(config, loaders, config.args.from_date)
 
    formatter = config.args.output_format.value(
 
        rate,
 
        cost_rates,
 
        price_rates,
 
        config.args.signed_currencies,
...
 
@@ -161,3 +204,3 @@ def run(config, stdout, stderr):
 
    if not config.args.from_currency:
 
        for from_curr in sorted(rate.rates):
 
        for from_curr in sorted(cost_rates.rates):
 
            print(formatter.format_rate_pair_bidir(from_curr, config.args.to_currency),
tests/historical2.json
Show inline comments
 
new file 100644
 
{
 
    "disclaimer": "https://openexchangerates.org/terms/",
 
    "license": "https://openexchangerates.org/license/",
 
    "timestamp": 982256400,
 
    "base": "USD",
 
    "rates": {
 
        "AED": 3.76246,
 
        "ALL": 144.529739,
 
        "ANG": 1.97,
 
        "RUB": 57.0736,
 
        "USD": 1
 
    }
 
}
tests/test_historical.py
Show inline comments
...
 
@@ -3,2 +3,3 @@ import decimal
 
import io
 
import itertools
 
import json
...
 
@@ -13,7 +14,7 @@ import oxrlib.commands.historical as oxrhist
 
class FakeResponder:
 
    def __init__(self, response_path):
 
        self.response_path = response_path
 
    def __init__(self, *response_paths):
 
        self.paths = itertools.cycle(response_paths)
 

	
 
    def _respond(self, *args, **kwargs):
 
        return open(self.response_path)
 
        return next(self.paths).open()
 

	
...
 
@@ -40,5 +41,12 @@ output = pytest.fixture(lambda: io.StringIO())
 

	
 
@pytest.fixture(scope='module')
 
def single_responder():
 
    return FakeResponder(relpath('historical1.json'))
 

	
 
@pytest.fixture
 
def historical1_responder():
 
    return FakeResponder(relpath('historical1.json').as_posix())
 
def alternate_responder():
 
    return FakeResponder(
 
        relpath('historical1.json'),
 
        relpath('historical2.json'),
 
    )
 

	
...
 
@@ -50,2 +58,3 @@ def build_config(
 
        to_currency=None,
 
        from_date=None,
 
        ledger=False,
...
 
@@ -61,2 +70,3 @@ def build_config(
 
        'to_currency': base if to_currency is None else to_currency,
 
        'from_date': from_date,
 
        'output_format': oxrhist.Formats['LEDGER' if ledger else 'RAW'],
...
 
@@ -88,4 +98,4 @@ def check_fx_amount(config, lines, amount, cost, fx_code, fx_sign=None, price=No
 

	
 
def test_rate_list(historical1_responder, output, any_date):
 
    config = build_config(historical1_responder, any_date)
 
def test_rate_list(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date)
 
    lines = lines_from_run(config, output)
...
 
@@ -98,4 +108,4 @@ def test_rate_list(historical1_responder, output, any_date):
 

	
 
def test_one_rate(historical1_responder, output, any_date):
 
    config = build_config(historical1_responder, any_date, from_currency='ANG')
 
def test_one_rate(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date, from_currency='ANG')
 
    lines = lines_from_run(config, output)
...
 
@@ -105,4 +115,4 @@ def test_one_rate(historical1_responder, output, any_date):
 

	
 
def test_conversion(historical1_responder, output, any_date):
 
    config = build_config(historical1_responder, any_date, amount=10, from_currency='AED')
 
def test_conversion(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date, amount=10, from_currency='AED')
 
    lines = lines_from_run(config, output)
...
 
@@ -111,4 +121,4 @@ def test_conversion(historical1_responder, output, any_date):
 

	
 
def test_back_conversion(historical1_responder, output, any_date):
 
    config = build_config(historical1_responder, any_date,
 
def test_back_conversion(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date,
 
                          amount=2, from_currency='USD', to_currency='ALL')
...
 
@@ -118,4 +128,4 @@ def test_back_conversion(historical1_responder, output, any_date):
 

	
 
def test_ledger_rate(historical1_responder, output, any_date):
 
    config = build_config(historical1_responder, any_date,
 
def test_ledger_rate(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date,
 
                          from_currency='ANG', ledger=True)
...
 
@@ -126,4 +136,4 @@ def test_ledger_rate(historical1_responder, output, any_date):
 

	
 
def test_ledger_conversion(historical1_responder, output, any_date):
 
    config = build_config(historical1_responder, any_date,
 
def test_ledger_conversion(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date,
 
                          from_currency='ALL', amount=300, ledger=True)
...
 
@@ -134,4 +144,4 @@ def test_ledger_conversion(historical1_responder, output, any_date):
 

	
 
def test_signed_currencies(historical1_responder, output, any_date):
 
    config = build_config(historical1_responder, any_date,
 
def test_signed_currencies(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date,
 
                          from_currency='AED', ledger=True, signed_currencies=['EUR'])
...
 
@@ -142,4 +152,4 @@ def test_signed_currencies(historical1_responder, output, any_date):
 

	
 
def test_denomination(historical1_responder, output, any_date):
 
    config = build_config(historical1_responder, any_date,
 
def test_denomination(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date,
 
                          from_currency='ANG', to_currency='AED', amount=10,
...
 
@@ -151,4 +161,4 @@ def test_denomination(historical1_responder, output, any_date):
 

	
 
def test_redundant_denomination(historical1_responder, output, any_date):
 
    config = build_config(historical1_responder, any_date,
 
def test_redundant_denomination(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date,
 
                          from_currency='ANG', to_currency='USD', amount=10,
...
 
@@ -160,4 +170,4 @@ def test_redundant_denomination(historical1_responder, output, any_date):
 

	
 
def test_from_denomination(historical1_responder, output, any_date):
 
    config = build_config(historical1_responder, any_date,
 
def test_from_denomination(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date,
 
                          from_currency='USD', to_currency='ALL', amount=10,
...
 
@@ -169,4 +179,4 @@ def test_from_denomination(historical1_responder, output, any_date):
 

	
 
def test_rate_precision_added_as_needed(historical1_responder, output, any_date):
 
    config = build_config(historical1_responder, any_date,
 
def test_rate_precision_added_as_needed(single_responder, output, any_date):
 
    config = build_config(single_responder, any_date,
 
                          from_currency='RUB', to_currency='USD', amount=63805,
...
 
@@ -181 +191,19 @@ def test_rate_precision_added_as_needed(historical1_responder, output, any_date)
 
    assert next(lines, None) is None
 

	
 
def test_from_date_rates(alternate_responder, output, any_date):
 
    config = build_config(alternate_responder, any_date,
 
                          from_currency='ANG', to_currency='AED',
 
                          from_date=any_date, ledger=True, denomination='USD')
 
    lines = lines_from_run(config, output)
 
    check_fx_amount(config, lines, '1 ANG', '2.051', 'AED', None, '1.909')
 
    check_fx_amount(config, lines, '1 AED', '0.487', 'ANG', None, '0.523')
 
    assert next(lines, None) is None
 

	
 
def test_from_date_conversion(alternate_responder, output, any_date):
 
    config = build_config(alternate_responder, any_date,
 
                          from_currency='ANG', to_currency='AED', amount=10,
 
                          from_date=any_date, ledger=True, denomination='USD')
 
    lines = lines_from_run(config, output)
 
    check_fx_amount(config, lines, '10.00 ANG', '0.558', 'USD', '$', '0.507')
 
    check_fx_amount(config, lines, '20.52 AED', '0.272', 'USD', '$', '0.265')
 
    assert next(lines, None) is None
0 comments (0 inline, 0 general)