historical: Format amounts according to currency convention.

This commit is contained in:
Brett Smith 2017-05-17 17:38:45 -04:00
parent 84bc0c7e84
commit ec3b9e83f8
3 changed files with 21 additions and 12 deletions

View file

@ -1,12 +1,22 @@
from babel.numbers import format_currency
from .. import rate as oxrrate from .. import rate as oxrrate
def format_one_rate(rate, from_amt, from_curr, to_curr): CURRENCY_FMT = '#,##0.### ¤¤'
return "{:g} {} = {:g} {}".format( RATE_FMT = '{from_amt:g} {from_curr} = {to_amt:g} {to_curr}'
from_amt, from_curr, rate.convert(from_amt, from_curr, to_curr), 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): def format_rate_pair(rate, from_curr, to_curr):
yield format_one_rate(rate, 1, from_curr, to_curr) amt = rate.convert(1, from_curr, to_curr)
yield format_one_rate(rate, 1, to_curr, from_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)
def run(config, stdout, stderr): def run(config, stdout, stderr):
loaders = config.get_loaders() loaders = config.get_loaders()
@ -22,6 +32,6 @@ def run(config, stdout, stderr):
print(*format_rate_pair(rate, config.args.from_currency, config.args.to_currency), print(*format_rate_pair(rate, config.args.from_currency, config.args.to_currency),
sep='\n', file=stdout) sep='\n', file=stdout)
else: else:
print(format_one_rate(rate, config.args.amount, print(format_conversion(rate, config.args.amount,
config.args.from_currency, config.args.to_currency), config.args.from_currency, config.args.to_currency),
file=stdout) file=stdout)

View file

@ -5,11 +5,12 @@ from setuptools import setup
setup( setup(
name='oxrlib', name='oxrlib',
description="Library to query the Open Exchange Rates (OXR) API", description="Library to query the Open Exchange Rates (OXR) API",
version='1.0', version='1.1',
author='Brett Smith', author='Brett Smith',
author_email='brettcsmith@brettcsmith.org', author_email='brettcsmith@brettcsmith.org',
license='GNU AGPLv3+', license='GNU AGPLv3+',
install_requires=['babel'],
setup_requires=['pytest-runner'], setup_requires=['pytest-runner'],
tests_require=['pytest'], tests_require=['pytest'],

View file

@ -82,14 +82,12 @@ def test_one_rate(historical1_responder, output):
def test_conversion(historical1_responder, output): def test_conversion(historical1_responder, output):
config = build_config(historical1_responder, amount=10, from_currency='AED') config = build_config(historical1_responder, amount=10, from_currency='AED')
lines = lines_from_run(config, output) lines = lines_from_run(config, output)
# FIXME: Assertion probably changes after we deal with precision right. assert next(lines) == '10.00 AED = 2.72 USD\n'
assert next(lines).startswith('10 AED = 2.72297')
assert next(lines, None) is None assert next(lines, None) is None
def test_back_conversion(historical1_responder, output): def test_back_conversion(historical1_responder, output):
config = build_config(historical1_responder, config = build_config(historical1_responder,
amount=2, from_currency='USD', to_currency='ALL') amount=2, from_currency='USD', to_currency='ALL')
lines = lines_from_run(config, output) lines = lines_from_run(config, output)
# FIXME: Assertion probably changes after we deal with precision right. assert next(lines) == '2.00 USD = 289 ALL\n'
assert next(lines) == '2 USD = 289.059586 ALL\n'
assert next(lines, None) is None assert next(lines, None) is None