Skip to content

Commit 1f131f3

Browse files
Merge pull request #106 from Robbie-Palmer/lazy-load-currency-data
Lazy load currency data
2 parents 7a55314 + 7e0bb80 commit 1f131f3

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

forex_python/converter.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
22
from decimal import Decimal
3-
import simplejson as json
3+
44
import requests
5+
import simplejson as json
56

67

78
class RatesNotAvailableError(Exception):
@@ -45,7 +46,7 @@ def _get_decoded_rate(
4546
self, response, dest_cur, use_decimal=False, date_str=None):
4647
return self._decode_rates(
4748
response, use_decimal=use_decimal, date_str=date_str).get(
48-
dest_cur, None)
49+
dest_cur, None)
4950

5051

5152
class CurrencyRates(Common):
@@ -102,7 +103,8 @@ def convert(self, base_cur, dest_cur, amount, date_obj=None):
102103
converted_amount = rate * amount
103104
return converted_amount
104105
except TypeError:
105-
raise DecimalFloatMismatchError("convert requires amount parameter is of type Decimal when force_decimal=True")
106+
raise DecimalFloatMismatchError(
107+
"convert requires amount parameter is of type Decimal when force_decimal=True")
106108
raise RatesNotAvailableError("Currency Rates Source Not Ready")
107109

108110

@@ -116,20 +118,22 @@ def convert(self, base_cur, dest_cur, amount, date_obj=None):
116118
class CurrencyCodes:
117119

118120
def __init__(self):
119-
pass
121+
self.__currency_data = None
122+
123+
@property
124+
def _currency_data(self):
125+
if self.__currency_data is None:
126+
file_path = os.path.dirname(os.path.abspath(__file__))
127+
with open(file_path + '/raw_data/currencies.json') as f:
128+
self.__currency_data = json.loads(f.read())
129+
return self.__currency_data
120130

121131
def _get_data(self, currency_code):
122-
file_path = os.path.dirname(os.path.abspath(__file__))
123-
with open(file_path+'/raw_data/currencies.json') as f:
124-
currency_data = json.loads(f.read())
125-
currency_dict = next((item for item in currency_data if item["cc"] == currency_code), None)
132+
currency_dict = next((item for item in self._currency_data if item["cc"] == currency_code), None)
126133
return currency_dict
127134

128135
def _get_data_from_symbol(self, symbol):
129-
file_path = os.path.dirname(os.path.abspath(__file__))
130-
with open(file_path + '/raw_data/currencies.json') as f:
131-
currency_data = json.loads(f.read())
132-
currency_dict = next((item for item in currency_data if item["symbol"] == symbol), None)
136+
currency_dict = next((item for item in self._currency_data if item["symbol"] == symbol), None)
133137
return currency_dict
134138

135139
def get_symbol(self, currency_code):
@@ -153,7 +157,6 @@ def get_currency_code_from_symbol(self, symbol):
153157

154158
_CURRENCY_CODES = CurrencyCodes()
155159

156-
157160
get_symbol = _CURRENCY_CODES.get_symbol
158161
get_currency_name = _CURRENCY_CODES.get_currency_name
159162
get_currency_code_from_symbol = _CURRENCY_CODES.get_currency_code_from_symbol

0 commit comments

Comments
 (0)