Convert CryptoToFiatConverter into a Singleton
Result in a speed up of the unittest from 60s to 4s Because it cost time to load Pymarketcap() every time we create a CryptoToFiatConverter, it worth it to change it into a Singleton.
This commit is contained in:
parent
408f120612
commit
28b1ecb109
@ -48,7 +48,10 @@ class CryptoFiat():
|
|||||||
return self._expiration - time.time() <= 0
|
return self._expiration - time.time() <= 0
|
||||||
|
|
||||||
|
|
||||||
class CryptoToFiatConverter():
|
class CryptoToFiatConverter(object):
|
||||||
|
__instance = None
|
||||||
|
_coinmarketcap = None
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
SUPPORTED_FIAT = [
|
SUPPORTED_FIAT = [
|
||||||
"AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK", "DKK",
|
"AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK", "DKK",
|
||||||
@ -57,12 +60,16 @@ class CryptoToFiatConverter():
|
|||||||
"RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR", "USD"
|
"RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR", "USD"
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __new__(cls):
|
||||||
try:
|
if CryptoToFiatConverter.__instance is None:
|
||||||
self._coinmarketcap = Pymarketcap()
|
CryptoToFiatConverter.__instance = object.__new__(cls)
|
||||||
except BaseException:
|
try:
|
||||||
self._coinmarketcap = None
|
CryptoToFiatConverter._coinmarketcap = Pymarketcap()
|
||||||
|
except BaseException:
|
||||||
|
CryptoToFiatConverter._coinmarketcap = None
|
||||||
|
return CryptoToFiatConverter.__instance
|
||||||
|
|
||||||
|
def __init__(self) -> None:
|
||||||
self._pairs = []
|
self._pairs = []
|
||||||
|
|
||||||
def convert_amount(self, crypto_amount: float, crypto_symbol: str, fiat_symbol: str) -> float:
|
def convert_amount(self, crypto_amount: float, crypto_symbol: str, fiat_symbol: str) -> float:
|
||||||
|
@ -219,9 +219,7 @@ def test_forcesell_handle(default_conf, update, ticker, ticker_sell_up, mocker):
|
|||||||
mocker.patch.multiple('freqtrade.main.exchange',
|
mocker.patch.multiple('freqtrade.main.exchange',
|
||||||
validate_pairs=MagicMock(),
|
validate_pairs=MagicMock(),
|
||||||
get_ticker=ticker)
|
get_ticker=ticker)
|
||||||
mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap',
|
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
|
||||||
ticker=MagicMock(return_value={'price_usd': 15000.0}),
|
|
||||||
_cache_symbols=MagicMock(return_value={'BTC': 1}))
|
|
||||||
init(default_conf, create_engine('sqlite://'))
|
init(default_conf, create_engine('sqlite://'))
|
||||||
|
|
||||||
# Create some test data
|
# Create some test data
|
||||||
@ -256,9 +254,7 @@ def test_forcesell_down_handle(default_conf, update, ticker, ticker_sell_down, m
|
|||||||
mocker.patch.multiple('freqtrade.main.exchange',
|
mocker.patch.multiple('freqtrade.main.exchange',
|
||||||
validate_pairs=MagicMock(),
|
validate_pairs=MagicMock(),
|
||||||
get_ticker=ticker)
|
get_ticker=ticker)
|
||||||
mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap',
|
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
|
||||||
ticker=MagicMock(return_value={'price_usd': 15000.0}),
|
|
||||||
_cache_symbols=MagicMock(return_value={'BTC': 1}))
|
|
||||||
init(default_conf, create_engine('sqlite://'))
|
init(default_conf, create_engine('sqlite://'))
|
||||||
|
|
||||||
# Create some test data
|
# Create some test data
|
||||||
@ -317,9 +313,7 @@ def test_forcesell_all_handle(default_conf, update, ticker, mocker):
|
|||||||
mocker.patch.multiple('freqtrade.main.exchange',
|
mocker.patch.multiple('freqtrade.main.exchange',
|
||||||
validate_pairs=MagicMock(),
|
validate_pairs=MagicMock(),
|
||||||
get_ticker=ticker)
|
get_ticker=ticker)
|
||||||
mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap',
|
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
|
||||||
ticker=MagicMock(return_value={'price_usd': 15000.0}),
|
|
||||||
_cache_symbols=MagicMock(return_value={'BTC': 1}))
|
|
||||||
init(default_conf, create_engine('sqlite://'))
|
init(default_conf, create_engine('sqlite://'))
|
||||||
|
|
||||||
# Create some test data
|
# Create some test data
|
||||||
|
@ -116,9 +116,9 @@ def test_fiat_convert_get_price(mocker):
|
|||||||
assert fiat_convert._pairs[0]._expiration is not expiration
|
assert fiat_convert._pairs[0]._expiration is not expiration
|
||||||
|
|
||||||
|
|
||||||
def test_fiat_convert_without_network(mocker):
|
def test_fiat_convert_without_network():
|
||||||
pymarketcap = MagicMock(side_effect=ImportError('Oh boy, you have no network!'))
|
# Because CryptoToFiatConverter is a Singleton we reset the value of _coinmarketcap
|
||||||
mocker.patch('freqtrade.fiat_convert.Pymarketcap', pymarketcap)
|
CryptoToFiatConverter._coinmarketcap = None
|
||||||
|
|
||||||
fiat_convert = CryptoToFiatConverter()
|
fiat_convert = CryptoToFiatConverter()
|
||||||
assert fiat_convert._coinmarketcap is None
|
assert fiat_convert._coinmarketcap is None
|
||||||
|
@ -525,9 +525,7 @@ def test_execute_sell_up(default_conf, ticker, ticker_sell_up, mocker):
|
|||||||
mocker.patch.multiple('freqtrade.main.exchange',
|
mocker.patch.multiple('freqtrade.main.exchange',
|
||||||
validate_pairs=MagicMock(),
|
validate_pairs=MagicMock(),
|
||||||
get_ticker=ticker)
|
get_ticker=ticker)
|
||||||
mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap',
|
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
|
||||||
ticker=MagicMock(return_value={'price_usd': 15000.0}),
|
|
||||||
_cache_symbols=MagicMock(return_value={'BTC': 1}))
|
|
||||||
init(default_conf, create_engine('sqlite://'))
|
init(default_conf, create_engine('sqlite://'))
|
||||||
|
|
||||||
# Create some test data
|
# Create some test data
|
||||||
@ -562,9 +560,7 @@ def test_execute_sell_down(default_conf, ticker, ticker_sell_down, mocker):
|
|||||||
mocker.patch.multiple('freqtrade.main.exchange',
|
mocker.patch.multiple('freqtrade.main.exchange',
|
||||||
validate_pairs=MagicMock(),
|
validate_pairs=MagicMock(),
|
||||||
get_ticker=ticker)
|
get_ticker=ticker)
|
||||||
mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap',
|
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
|
||||||
ticker=MagicMock(return_value={'price_usd': 15000.0}),
|
|
||||||
_cache_symbols=MagicMock(return_value={'BTC': 1}))
|
|
||||||
init(default_conf, create_engine('sqlite://'))
|
init(default_conf, create_engine('sqlite://'))
|
||||||
|
|
||||||
# Create some test data
|
# Create some test data
|
||||||
|
Loading…
Reference in New Issue
Block a user