From 4e826b341b6d3f53e5b2e9b61722bc2781fef987 Mon Sep 17 00:00:00 2001 From: Robbie-Palmer <8760191+Robbie-Palmer@users.noreply.github.com> Date: Sat, 6 Nov 2021 21:37:38 +0000 Subject: [PATCH 1/2] Format converter.py --- forex_python/converter.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/forex_python/converter.py b/forex_python/converter.py index bf465d0..c652a2c 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") @@ -120,7 +122,7 @@ def __init__(self): 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: + 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) return currency_dict @@ -153,7 +155,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 From 7e0bb8094729b12e2d8b353d1b7cf3d1ea054471 Mon Sep 17 00:00:00 2001 From: Robbie-Palmer <8760191+Robbie-Palmer@users.noreply.github.com> Date: Sat, 6 Nov 2021 21:43:00 +0000 Subject: [PATCH 2/2] Lazy load currency data from json file Removing duplicate code, and preventing redundant disk reads --- forex_python/converter.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/forex_python/converter.py b/forex_python/converter.py index c652a2c..390a11a 100644 --- a/forex_python/converter.py +++ b/forex_python/converter.py @@ -118,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):