Skip to content

Commit

Permalink
Merge pull request #106 from Robbie-Palmer/lazy-load-currency-data
Browse files Browse the repository at this point in the history
Lazy load currency data
  • Loading branch information
abhijeet-1110 authored Dec 3, 2021
2 parents 7a55314 + 7e0bb80 commit 1f131f3
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions forex_python/converter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
from decimal import Decimal
import simplejson as json

import requests
import simplejson as json


class RatesNotAvailableError(Exception):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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")


Expand All @@ -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):
Expand All @@ -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

0 comments on commit 1f131f3

Please sign in to comment.