1
1
import os
2
2
from decimal import Decimal
3
- import simplejson as json
3
+
4
4
import requests
5
+ import simplejson as json
5
6
6
7
7
8
class RatesNotAvailableError (Exception ):
@@ -45,7 +46,7 @@ def _get_decoded_rate(
45
46
self , response , dest_cur , use_decimal = False , date_str = None ):
46
47
return self ._decode_rates (
47
48
response , use_decimal = use_decimal , date_str = date_str ).get (
48
- dest_cur , None )
49
+ dest_cur , None )
49
50
50
51
51
52
class CurrencyRates (Common ):
@@ -102,7 +103,8 @@ def convert(self, base_cur, dest_cur, amount, date_obj=None):
102
103
converted_amount = rate * amount
103
104
return converted_amount
104
105
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" )
106
108
raise RatesNotAvailableError ("Currency Rates Source Not Ready" )
107
109
108
110
@@ -116,20 +118,22 @@ def convert(self, base_cur, dest_cur, amount, date_obj=None):
116
118
class CurrencyCodes :
117
119
118
120
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
120
130
121
131
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 )
126
133
return currency_dict
127
134
128
135
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 )
133
137
return currency_dict
134
138
135
139
def get_symbol (self , currency_code ):
@@ -153,7 +157,6 @@ def get_currency_code_from_symbol(self, symbol):
153
157
154
158
_CURRENCY_CODES = CurrencyCodes ()
155
159
156
-
157
160
get_symbol = _CURRENCY_CODES .get_symbol
158
161
get_currency_name = _CURRENCY_CODES .get_currency_name
159
162
get_currency_code_from_symbol = _CURRENCY_CODES .get_currency_code_from_symbol
0 commit comments