Skip to content

Commit

Permalink
exchange CLI finished and functions made more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
gidden committed Aug 2, 2017
1 parent 4f06461 commit e3fb670
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
46 changes: 34 additions & 12 deletions salamanca/cli.py
Expand Up @@ -41,21 +41,40 @@ def download_wb(log=False, overwrite=False, **kwargs):
#

def exchange_cli(parser):
amt = 'quantity of currency'
parser.add_argument('amt', help=amt, default=1.0)
amt = 'quantity of currency (default: 1.0)'
parser.add_argument('-x', '--amt', help=amt, default=1.0)
units = "units in which to do conversion [MER or PPP] (default: MER)"
parser.add_argument('-u', '--units', help=units, default='MER')
meth = "method to use to do conversion [deflator or cpi] (default: deflator)"
parser.add_argument('-m', '--meth', help=meth, default='deflator')

required = parser.add_argument_group('required arguments')
_from = '(iso, year) a 3-letter ISO code for the origin country and origin year'
required.add_argument('-f', '--from', help=_from, nargs=2, required=True)
_to = '(iso, year) a 3-letter ISO code for the destination country and destination year'
required.add_argument('-t', '--to', help=_to, nargs=2, required=True)
_from = """
ISO: 3-letter ISO code for the origin country, YEAR: origin year
"""
required.add_argument('-f', '--from', help=_from,
nargs=2, metavar=('ISO', 'YEAR'), required=True)
_to = """
ISO: 3-letter ISO code for the destination country, YEAR: destination year
"""
required.add_argument('-t', '--to', help=_to,
nargs=2, metavar=('ISO', 'YEAR'), required=True)


def exchange(*args, **kwargs):
kwargs.pop('command')
amt = kwargs.pop('amt', 1)
xlator = currency.Translator()
return xlator.exchange(amt, **kwargs)
def exchange(**kwargs):
amt = kwargs['amt']
fromiso, fromyr = kwargs['from']
toiso, toyr = kwargs['to']
units = kwargs['units']
inflation_method = kwargs['meth']

xlator = currency.Translator()
ret = xlator.exchange(amt,
fromiso=fromiso, fromyr=fromyr,
toiso=toiso, toyr=toyr,
units=units, inflation_method=inflation_method)
print(ret)
return ret

COMMANDS['exchange'] = (
"""Exchange currency from one country/year to another.""",
Expand All @@ -77,7 +96,10 @@ def main():
for cmd in COMMANDS:
cli_help = COMMANDS[cmd][0]
cli_func = COMMANDS[cmd][1]
subparser = subparsers.add_parser(cmd, help=cli_help)
subparser = subparsers.add_parser(
cmd,
help=cli_help,
)
cli_func(subparser)

args = parser.parse_args()
Expand Down
12 changes: 9 additions & 3 deletions salamanca/currency.py
Expand Up @@ -24,7 +24,7 @@ def __init__(self):
self._xr = wb.to_wide(wb.exchange_rate()).set_index('country')
self._ppp_to_mer = wb.to_wide(wb.ppp_to_mer()).set_index('country')

def inflation(self, iso, fromyr, toyr, method=None):
def inflation(self, iso, fromyr, toyr, method='deflator'):
"""Calculate inflation for a country
Parameters
Expand All @@ -38,7 +38,7 @@ def inflation(self, iso, fromyr, toyr, method=None):
method: str
one of: cpi, deflator (default: deflator)
"""
method = method or 'deflator'
method = method.lower()
df = self._cpi if method == 'cpi' else self._gdp_deflator
x = df.loc[iso][toyr] / df.loc[iso][fromyr]
if np.isnan(x):
Expand All @@ -51,7 +51,7 @@ def inflation(self, iso, fromyr, toyr, method=None):
def exchange(self, x, iso=None, yr=None, units='MER',
fromiso=None, fromyr=None,
toiso=None, toyr=None,
inusd=False, inflation_method=None):
inusd=False, inflation_method='deflator'):
"""Exchange currency from one country/year to another.
Parameters
Expand Down Expand Up @@ -79,10 +79,16 @@ def exchange(self, x, iso=None, yr=None, units='MER',
the argument to provide to `inflation()`
"""
fromiso = fromiso or iso
fromiso = fromiso.upper()
fromyr = fromyr or yr
fromyr = int(fromyr)

toiso = toiso or 'USA'
toiso = toiso.upper()
toyr = toyr or yr
toyr = int(toyr)

units = units.upper()

# is using special inusd option, do full calculation and return
if inusd:
Expand Down
15 changes: 10 additions & 5 deletions tests/test_currency.py
Expand Up @@ -150,11 +150,16 @@ def test_aut_2005_2010_usd_ppp():


def test_cli():
obs = cli.exchange(command='exchange', amt=20, iso='AUT', fromyr=2005,
toyr=2010, inusd=True, units='PPP')
exp = 20 * (xr_aut_2005 * ppp_to_mer_aut_2005) * \
(100. / gdef_aut_2005) / \
(xr_aut_2010 * ppp_to_mer_aut_2010)
kwargs = {
'amt': 20,
'from': ('CAN', '2005'),
'to': ('AUT', '2010'),
'units': 'PPP',
'meth': 'deflator',
}
obs = cli.exchange(**kwargs)
exp = 20 * (xr_aut_2005 / xr_can_2005) / (gdef_aut_2005 / 100.)
exp *= ppp_to_mer_aut_2005 / ppp_to_mer_can_2005
assert_almost_equal(obs, exp)


Expand Down

0 comments on commit e3fb670

Please sign in to comment.