Remove os.path from hyperopt

This commit is contained in:
Matthias 2019-07-21 15:56:44 +02:00
parent ae0e001187
commit 0a253d66d0
2 changed files with 14 additions and 11 deletions

View File

@ -5,7 +5,6 @@ This module contains the hyperopt logic
""" """
import logging import logging
import os
import sys import sys
from operator import itemgetter from operator import itemgetter
@ -31,9 +30,9 @@ logger = logging.getLogger(__name__)
INITIAL_POINTS = 30 INITIAL_POINTS = 30
MAX_LOSS = 100000 # just a big enough number to be bad result in loss optimization 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') TICKERDATA_PICKLE = Path.cwd() / 'user_data' / 'hyperopt_tickerdata.pkl'
TRIALSDATA_PICKLE = os.path.join('user_data', 'hyperopt_results.pickle') TRIALSDATA_PICKLE = Path.cwd() / 'user_data' / 'hyperopt_results.pickle'
HYPEROPT_LOCKFILE = os.path.join('user_data', 'hyperopt.lock') HYPEROPT_LOCKFILE = Path.cwd() / 'user_data' / 'hyperopt.lock'
class Hyperopt(Backtesting): class Hyperopt(Backtesting):
@ -115,7 +114,7 @@ class Hyperopt(Backtesting):
""" """
logger.info('Reading Trials from \'%s\'', self.trials_file) logger.info('Reading Trials from \'%s\'', self.trials_file)
trials = load(self.trials_file) trials = load(self.trials_file)
os.remove(self.trials_file) self.trials_file.unlink()
return trials return trials
def log_trials_result(self) -> None: def log_trials_result(self) -> None:
@ -269,7 +268,7 @@ class Hyperopt(Backtesting):
def load_previous_results(self): def load_previous_results(self):
""" read trials file if we have one """ """ 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() self.trials = self.read_trials()
logger.info( logger.info(
'Loaded %d previous evaluations from disk.', 'Loaded %d previous evaluations from disk.',

View File

@ -1,12 +1,13 @@
# pragma pylint: disable=missing-docstring,W0212,C0103 # pragma pylint: disable=missing-docstring,W0212,C0103
import os import os
from datetime import datetime from datetime import datetime
from unittest.mock import MagicMock from unittest.mock import MagicMock, PropertyMock
import pandas as pd import pandas as pd
import pytest import pytest
from arrow import Arrow from arrow import Arrow
from filelock import Timeout from filelock import Timeout
from pathlib import Path
from freqtrade import DependencyException from freqtrade import DependencyException
from freqtrade.data.converter import parse_ticker_dataframe 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 - we might have a pickle'd file so make sure that we return
false when looking for it 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.object(Path, "is_file", MagicMock(return_value=False))
mocker.patch('freqtrade.optimize.hyperopt.os.path.getsize', return_value=1) stat_mock = MagicMock()
mocker.patch('freqtrade.optimize.hyperopt.os.remove', return_value=True) 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) mocker.patch('freqtrade.optimize.hyperopt.dump', return_value=None)
return [{'loss': 1, 'result': 'foo', 'params': {}}] return [{'loss': 1, 'result': 'foo', 'params': {}}]