diff --git a/forex_python/converter.py b/forex_python/converter.py index bf465d0..390a11a 100644 --- a/forex_python/converter.py +++ b/forex_python/converter.py @@ -1,7 +1,8 @@ import os from decimal import Decimal -import simplejson as json + import requests +import simplejson as json class RatesNotAvailableError(Exception): @@ -45,7 +46,7 @@ def _get_decoded_rate( self, response, dest_cur, use_decimal=False, date_str=None): return self._decode_rates( response, use_decimal=use_decimal, date_str=date_str).get( - dest_cur, None) + dest_cur, None) class CurrencyRates(Common): @@ -102,7 +103,8 @@ def convert(self, base_cur, dest_cur, amount, date_obj=None): converted_amount = rate * amount return converted_amount except TypeError: - raise DecimalFloatMismatchError("convert requires amount parameter is of type Decimal when force_decimal=True") + raise DecimalFloatMismatchError( + "convert requires amount parameter is of type Decimal when force_decimal=True") raise RatesNotAvailableError("Currency Rates Source Not Ready") @@ -116,20 +118,22 @@ def convert(self, base_cur, dest_cur, amount, date_obj=None): class CurrencyCodes: def __init__(self): - pass + self.__currency_data = None + + @property + def _currency_data(self): + if self.__currency_data is None: + file_path = os.path.dirname(os.path.abspath(__file__)) + with open(file_path + '/raw_data/currencies.json') as f: + self.__currency_data = json.loads(f.read()) + return self.__currency_data def _get_data(self, currency_code): - file_path = os.path.dirname(os.path.abspath(__file__)) - with open(file_path+'/raw_data/currencies.json') as f: - currency_data = json.loads(f.read()) - currency_dict = next((item for item in currency_data if item["cc"] == currency_code), None) + currency_dict = next((item for item in self._currency_data if item["cc"] == currency_code), None) return currency_dict def _get_data_from_symbol(self, symbol): - file_path = os.path.dirname(os.path.abspath(__file__)) - with open(file_path + '/raw_data/currencies.json') as f: - currency_data = json.loads(f.read()) - currency_dict = next((item for item in currency_data if item["symbol"] == symbol), None) + currency_dict = next((item for item in self._currency_data if item["symbol"] == symbol), None) return currency_dict def get_symbol(self, currency_code): @@ -153,7 +157,6 @@ def get_currency_code_from_symbol(self, symbol): _CURRENCY_CODES = CurrencyCodes() - get_symbol = _CURRENCY_CODES.get_symbol get_currency_name = _CURRENCY_CODES.get_currency_name get_currency_code_from_symbol = _CURRENCY_CODES.get_currency_code_from_symbol