Changeset - ec3b9e83f865
[Not reviewed]
0 3 0
Brett Smith - 7 years ago 2017-05-17 21:38:45
brettcsmith@brettcsmith.org
historical: Format amounts according to currency convention.
3 files changed with 21 insertions and 12 deletions:
0 comments (0 inline, 0 general)
oxrlib/commands/historical.py
Show inline comments
 
from babel.numbers import format_currency
 

	
 
from .. import rate as oxrrate
 

	
 
def format_one_rate(rate, from_amt, from_curr, to_curr):
 
    return "{:g} {} = {:g} {}".format(
 
        from_amt, from_curr, rate.convert(from_amt, from_curr, to_curr), to_curr)
 
CURRENCY_FMT = '#,##0.### ¤¤'
 
RATE_FMT = '{from_amt:g} {from_curr} = {to_amt:g} {to_curr}'
 

	
 
def format_conversion(rate, from_amt, from_curr, to_curr):
 
    to_amt = rate.convert(from_amt, from_curr, to_curr)
 
    return "{} = {}".format(
 
        format_currency(from_amt, from_curr, CURRENCY_FMT),
 
        format_currency(to_amt, to_curr, CURRENCY_FMT),
 
    )
 

	
 
def format_rate_pair(rate, from_curr, to_curr):
 
    yield format_one_rate(rate, 1, from_curr, to_curr)
 
    yield format_one_rate(rate, 1, to_curr, from_curr)
 
    amt = rate.convert(1, from_curr, to_curr)
 
    yield RATE_FMT.format(from_amt=1, from_curr=from_curr, to_amt=amt, to_curr=to_curr)
 
    amt = rate.convert(1, to_curr, from_curr)
 
    yield RATE_FMT.format(from_amt=1, from_curr=to_curr, to_amt=amt, to_curr=from_curr)
 

	
...
 
@@ -24,4 +34,4 @@ def run(config, stdout, stderr):
 
    else:
 
        print(format_one_rate(rate, config.args.amount,
 
                              config.args.from_currency, config.args.to_currency),
 
        print(format_conversion(rate, config.args.amount,
 
                                config.args.from_currency, config.args.to_currency),
 
              file=stdout)
setup.py
Show inline comments
...
 
@@ -7,3 +7,3 @@ setup(
 
    description="Library to query the Open Exchange Rates (OXR) API",
 
    version='1.0',
 
    version='1.1',
 
    author='Brett Smith',
...
 
@@ -12,2 +12,3 @@ setup(
 

	
 
    install_requires=['babel'],
 
    setup_requires=['pytest-runner'],
tests/test_historical.py
Show inline comments
...
 
@@ -84,4 +84,3 @@ def test_conversion(historical1_responder, output):
 
    lines = lines_from_run(config, output)
 
    # FIXME: Assertion probably changes after we deal with precision right.
 
    assert next(lines).startswith('10 AED = 2.72297')
 
    assert next(lines) == '10.00 AED = 2.72 USD\n'
 
    assert next(lines, None) is None
...
 
@@ -92,4 +91,3 @@ def test_back_conversion(historical1_responder, output):
 
    lines = lines_from_run(config, output)
 
    # FIXME: Assertion probably changes after we deal with precision right.
 
    assert next(lines) == '2 USD = 289.059586 ALL\n'
 
    assert next(lines) == '2.00 USD = 289 ALL\n'
 
    assert next(lines, None) is None
0 comments (0 inline, 0 general)