replace os.path with pathlib.Path

This commit is contained in:
Matthias 2018-12-15 13:54:35 +01:00
parent a34c2cf64b
commit f261911285
3 changed files with 18 additions and 22 deletions

View File

@ -9,7 +9,7 @@ except ImportError:
import json # type: ignore
_UJSON = False
import logging
import os
from pathlib import Path
from typing import Optional, List, Dict, Tuple, Any
import arrow
@ -64,7 +64,7 @@ def trim_tickerlist(tickerlist: List[Dict], timerange: TimeRange) -> List[Dict]:
def load_tickerdata_file(
datadir: str, pair: str,
datadir: Path, pair: str,
ticker_interval: str,
timerange: Optional[TimeRange] = None) -> Optional[List[Dict]]:
"""
@ -73,16 +73,16 @@ def load_tickerdata_file(
"""
path = make_testdata_path(datadir)
pair_s = pair.replace('/', '_')
file = os.path.join(path, f'{pair_s}-{ticker_interval}.json')
gzipfile = file + '.gz'
file = path.joinpath(f'{pair_s}-{ticker_interval}.json')
gzipfile = file.with_suffix('.gz')
# If the file does not exist we download it when None is returned.
# If file exists, read the file, load the json
if os.path.isfile(gzipfile):
if gzipfile.is_file():
logger.debug('Loading ticker data from file %s', gzipfile)
with gzip.open(gzipfile) as tickerdata:
pairdata = json.load(tickerdata)
elif os.path.isfile(file):
elif file.is_file():
logger.debug('Loading ticker data from file %s', file)
with open(file) as tickerdata:
pairdata = json.load(tickerdata)
@ -94,7 +94,7 @@ def load_tickerdata_file(
return pairdata
def load_data(datadir: str,
def load_data(datadir: Path,
ticker_interval: str,
pairs: List[str],
refresh_pairs: Optional[bool] = False,
@ -137,13 +137,9 @@ def load_data(datadir: str,
return result
def make_testdata_path(datadir: str) -> str:
def make_testdata_path(datadir: Optional[Path]) -> Path:
"""Return the path where testdata files are stored"""
return datadir or os.path.abspath(
os.path.join(
os.path.dirname(__file__), '..', 'tests', 'testdata'
)
)
return datadir or (Path(__file__).parent.parent / "tests" / "testdata").resolve()
def download_pairs(datadir, exchange: Exchange, pairs: List[str],
@ -167,7 +163,7 @@ def download_pairs(datadir, exchange: Exchange, pairs: List[str],
return True
def load_cached_data_for_updating(filename: str,
def load_cached_data_for_updating(filename: Path,
tick_interval: str,
timerange: Optional[TimeRange]) -> Tuple[
List[Any],
@ -187,7 +183,7 @@ def load_cached_data_for_updating(filename: str,
since_ms = arrow.utcnow().shift(minutes=num_minutes).timestamp * 1000
# read the cached file
if os.path.isfile(filename):
if filename.is_file():
with open(filename, "rt") as file:
data = json_load(file)
# remove the last item, because we are not sure if it is correct
@ -210,7 +206,7 @@ def load_cached_data_for_updating(filename: str,
return (data, since_ms)
def download_backtesting_testdata(datadir: str,
def download_backtesting_testdata(datadir: Path,
exchange: Exchange,
pair: str,
tick_interval: str = '5m',
@ -230,7 +226,7 @@ def download_backtesting_testdata(datadir: str,
"""
path = make_testdata_path(datadir)
filepair = pair.replace("/", "_")
filename = os.path.join(path, f'{filepair}-{tick_interval}.json')
filename = path.joinpath(f'{filepair}-{tick_interval}.json')
logger.info(
'Download the pair: "%s", Interval: %s',

View File

@ -142,7 +142,7 @@ def plot_analyzed_dataframe(args: Namespace) -> None:
tickers[pair] = exchange.klines(pair)
else:
tickers = history.load_data(
datadir=_CONF.get("datadir"),
datadir=Path(_CONF.get("datadir")),
pairs=[pair],
ticker_interval=tick_interval,
refresh_pairs=_CONF.get('refresh_pairs', False),

View File

@ -13,10 +13,10 @@ Optional Cli parameters
--export-filename: Specify where the backtest export is located.
"""
import logging
import os
import sys
import json
from argparse import Namespace
from pathlib import Path
from typing import List, Optional
import numpy as np
@ -27,7 +27,7 @@ import plotly.graph_objs as go
from freqtrade.arguments import Arguments
from freqtrade.configuration import Configuration
from freqtrade import constants
from freqtrade.data as history
from freqtrade.data import history
from freqtrade.resolvers import StrategyResolver
import freqtrade.misc as misc
@ -121,7 +121,7 @@ def plot_profit(args: Namespace) -> None:
logger.info('Filter, keep pairs %s' % pairs)
tickers = history.load_data(
datadir=config.get('datadir'),
datadir=Path(config.get('datadir')),
pairs=pairs,
ticker_interval=tick_interval,
refresh_pairs=False,
@ -187,7 +187,7 @@ def plot_profit(args: Namespace) -> None:
)
fig.append_trace(pair_profit, 3, 1)
plot(fig, filename=os.path.join('user_data', 'freqtrade-profit-plot.html'))
plot(fig, filename=str(Path('user_data').joinpath('freqtrade-profit-plot.html')))
def define_index(min_date: int, max_date: int, interval: str) -> int: