Skip to content

Commit

Permalink
Convert CryptoToFiatConverter into a Singleton
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
glonlas committed Jan 22, 2018
1 parent 408f120 commit 28b1ecb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
19 changes: 13 additions & 6 deletions freqtrade/fiat_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ def is_expired(self) -> bool:
return self._expiration - time.time() <= 0


class CryptoToFiatConverter():
class CryptoToFiatConverter(object):
__instance = None
_coinmarketcap = None

# Constants
SUPPORTED_FIAT = [
"AUD", "BRL", "CAD", "CHF", "CLP", "CNY", "CZK", "DKK",
Expand All @@ -57,12 +60,16 @@ class CryptoToFiatConverter():
"RUB", "SEK", "SGD", "THB", "TRY", "TWD", "ZAR", "USD"
]

def __init__(self) -> None:
try:
self._coinmarketcap = Pymarketcap()
except BaseException:
self._coinmarketcap = None
def __new__(cls):
if CryptoToFiatConverter.__instance is None:
CryptoToFiatConverter.__instance = object.__new__(cls)
try:
CryptoToFiatConverter._coinmarketcap = Pymarketcap()
except BaseException:
CryptoToFiatConverter._coinmarketcap = None
return CryptoToFiatConverter.__instance

def __init__(self) -> None:
self._pairs = []

def convert_amount(self, crypto_amount: float, crypto_symbol: str, fiat_symbol: str) -> float:
Expand Down
12 changes: 3 additions & 9 deletions freqtrade/tests/rpc/test_rpc_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ def test_forcesell_handle(default_conf, update, ticker, ticker_sell_up, mocker):
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker)
mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap',
ticker=MagicMock(return_value={'price_usd': 15000.0}),
_cache_symbols=MagicMock(return_value={'BTC': 1}))
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
init(default_conf, create_engine('sqlite://'))

# Create some test data
Expand Down Expand Up @@ -256,9 +254,7 @@ def test_forcesell_down_handle(default_conf, update, ticker, ticker_sell_down, m
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker)
mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap',
ticker=MagicMock(return_value={'price_usd': 15000.0}),
_cache_symbols=MagicMock(return_value={'BTC': 1}))
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
init(default_conf, create_engine('sqlite://'))

# Create some test data
Expand Down Expand Up @@ -317,9 +313,7 @@ def test_forcesell_all_handle(default_conf, update, ticker, mocker):
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker)
mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap',
ticker=MagicMock(return_value={'price_usd': 15000.0}),
_cache_symbols=MagicMock(return_value={'BTC': 1}))
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
init(default_conf, create_engine('sqlite://'))

# Create some test data
Expand Down
6 changes: 3 additions & 3 deletions freqtrade/tests/test_fiat_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ def test_fiat_convert_get_price(mocker):
assert fiat_convert._pairs[0]._expiration is not expiration


def test_fiat_convert_without_network(mocker):
pymarketcap = MagicMock(side_effect=ImportError('Oh boy, you have no network!'))
mocker.patch('freqtrade.fiat_convert.Pymarketcap', pymarketcap)
def test_fiat_convert_without_network():
# Because CryptoToFiatConverter is a Singleton we reset the value of _coinmarketcap
CryptoToFiatConverter._coinmarketcap = None

fiat_convert = CryptoToFiatConverter()
assert fiat_convert._coinmarketcap is None
Expand Down
8 changes: 2 additions & 6 deletions freqtrade/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,7 @@ def test_execute_sell_up(default_conf, ticker, ticker_sell_up, mocker):
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker)
mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap',
ticker=MagicMock(return_value={'price_usd': 15000.0}),
_cache_symbols=MagicMock(return_value={'BTC': 1}))
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
init(default_conf, create_engine('sqlite://'))

# Create some test data
Expand Down Expand Up @@ -562,9 +560,7 @@ def test_execute_sell_down(default_conf, ticker, ticker_sell_down, mocker):
mocker.patch.multiple('freqtrade.main.exchange',
validate_pairs=MagicMock(),
get_ticker=ticker)
mocker.patch.multiple('freqtrade.fiat_convert.Pymarketcap',
ticker=MagicMock(return_value={'price_usd': 15000.0}),
_cache_symbols=MagicMock(return_value={'BTC': 1}))
mocker.patch('freqtrade.fiat_convert.CryptoToFiatConverter._find_price', return_value=15000.0)
init(default_conf, create_engine('sqlite://'))

# Create some test data
Expand Down

0 comments on commit 28b1ecb

Please sign in to comment.