diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py index 759ceffbe..a1ec43c14 100644 --- a/freqtrade/optimize/hyperopt.py +++ b/freqtrade/optimize/hyperopt.py @@ -5,7 +5,6 @@ This module contains the hyperopt logic """ import logging -import os import sys from operator import itemgetter @@ -31,9 +30,9 @@ logger = logging.getLogger(__name__) INITIAL_POINTS = 30 MAX_LOSS = 100000 # just a big enough number to be bad result in loss optimization -TICKERDATA_PICKLE = os.path.join('user_data', 'hyperopt_tickerdata.pkl') -TRIALSDATA_PICKLE = os.path.join('user_data', 'hyperopt_results.pickle') -HYPEROPT_LOCKFILE = os.path.join('user_data', 'hyperopt.lock') +TICKERDATA_PICKLE = Path.cwd() / 'user_data' / 'hyperopt_tickerdata.pkl' +TRIALSDATA_PICKLE = Path.cwd() / 'user_data' / 'hyperopt_results.pickle' +HYPEROPT_LOCKFILE = Path.cwd() / 'user_data' / 'hyperopt.lock' class Hyperopt(Backtesting): @@ -115,7 +114,7 @@ class Hyperopt(Backtesting): """ logger.info('Reading Trials from \'%s\'', self.trials_file) trials = load(self.trials_file) - os.remove(self.trials_file) + self.trials_file.unlink() return trials def log_trials_result(self) -> None: @@ -269,7 +268,7 @@ class Hyperopt(Backtesting): def load_previous_results(self): """ read trials file if we have one """ - if os.path.exists(self.trials_file) and os.path.getsize(self.trials_file) > 0: + if self.trials_file.is_file() and self.trials_file.stat().st_size > 0: self.trials = self.read_trials() logger.info( 'Loaded %d previous evaluations from disk.', diff --git a/freqtrade/tests/optimize/test_hyperopt.py b/freqtrade/tests/optimize/test_hyperopt.py index fad89e877..2370e145e 100644 --- a/freqtrade/tests/optimize/test_hyperopt.py +++ b/freqtrade/tests/optimize/test_hyperopt.py @@ -1,12 +1,13 @@ # pragma pylint: disable=missing-docstring,W0212,C0103 import os from datetime import datetime -from unittest.mock import MagicMock +from unittest.mock import MagicMock, PropertyMock import pandas as pd import pytest from arrow import Arrow from filelock import Timeout +from pathlib import Path from freqtrade import DependencyException from freqtrade.data.converter import parse_ticker_dataframe @@ -53,11 +54,14 @@ def create_trials(mocker, hyperopt) -> None: - we might have a pickle'd file so make sure that we return false when looking for it """ - hyperopt.trials_file = os.path.join('freqtrade', 'tests', 'optimize', 'ut_trials.pickle') + hyperopt.trials_file = Path('freqtrade/tests/optimize/ut_trials.pickle') - mocker.patch('freqtrade.optimize.hyperopt.os.path.exists', return_value=False) - mocker.patch('freqtrade.optimize.hyperopt.os.path.getsize', return_value=1) - mocker.patch('freqtrade.optimize.hyperopt.os.remove', return_value=True) + mocker.patch.object(Path, "is_file", MagicMock(return_value=False)) + stat_mock = MagicMock() + stat_mock.st_size = PropertyMock(return_value=1) + mocker.patch.object(Path, "stat", MagicMock(return_value=False)) + + mocker.patch.object(Path, "unlink", MagicMock(return_value=True)) mocker.patch('freqtrade.optimize.hyperopt.dump', return_value=None) return [{'loss': 1, 'result': 'foo', 'params': {}}]