mirror of
https://github.com/freqtrade/freqtrade.git
synced 2025-11-29 00:23:07 +00:00
chore: load zip reusability
This commit is contained in:
@@ -6,7 +6,7 @@ import logging
|
|||||||
import zipfile
|
import zipfile
|
||||||
from copy import copy
|
from copy import copy
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from io import StringIO
|
from io import BytesIO, StringIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Literal
|
from typing import Any, Literal
|
||||||
|
|
||||||
@@ -169,18 +169,11 @@ def load_backtest_stats(filename: Path | str) -> BacktestResultType:
|
|||||||
logger.info(f"Loading backtest result from {filename}")
|
logger.info(f"Loading backtest result from {filename}")
|
||||||
|
|
||||||
if filename.suffix == ".zip":
|
if filename.suffix == ".zip":
|
||||||
try:
|
data = json_load(
|
||||||
with zipfile.ZipFile(filename) as zipf:
|
StringIO(
|
||||||
json_filename = filename.with_suffix(".json")
|
load_file_from_zip(filename, filename.with_suffix(".json").name).decode("utf-8")
|
||||||
try:
|
)
|
||||||
with zipf.open(json_filename.name) as json_file:
|
)
|
||||||
# Need to convert to StringIO since json_load expects a text stream
|
|
||||||
data = json_load(StringIO(json_file.read().decode("utf-8")))
|
|
||||||
except KeyError:
|
|
||||||
# File not found in zip
|
|
||||||
raise ValueError(f"Could not find {json_filename.name} in {filename}")
|
|
||||||
except zipfile.BadZipFile:
|
|
||||||
raise ValueError(f"Bad zip file: {filename}")
|
|
||||||
else:
|
else:
|
||||||
with filename.open() as file:
|
with filename.open() as file:
|
||||||
data = json_load(file)
|
data = json_load(file)
|
||||||
@@ -407,6 +400,22 @@ def load_backtest_data(filename: Path | str, strategy: str | None = None) -> pd.
|
|||||||
return df
|
return df
|
||||||
|
|
||||||
|
|
||||||
|
def load_file_from_zip(zip_path: Path, filename: str) -> bytes | None:
|
||||||
|
"""
|
||||||
|
Load a file from a zip file
|
||||||
|
:param zip_path: Path to the zip file
|
||||||
|
:param filename: Name of the file to load
|
||||||
|
:return: Bytes of the file
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
with zipfile.ZipFile(zip_path) as zipf:
|
||||||
|
with zipf.open(filename) as file:
|
||||||
|
return file.read()
|
||||||
|
except zipfile.BadZipFile:
|
||||||
|
logger.exception(f"Bad zip file: {zip_path}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def load_backtest_analysis_data(backtest_dir: Path, name: str):
|
def load_backtest_analysis_data(backtest_dir: Path, name: str):
|
||||||
"""
|
"""
|
||||||
Load backtest analysis data either from a pickle file or from within a zip file
|
Load backtest analysis data either from a pickle file or from within a zip file
|
||||||
@@ -424,23 +433,15 @@ def load_backtest_analysis_data(backtest_dir: Path, name: str):
|
|||||||
|
|
||||||
if zip_path.suffix == ".zip":
|
if zip_path.suffix == ".zip":
|
||||||
# Load from zip file
|
# Load from zip file
|
||||||
try:
|
analysis_name = f"{zip_path.stem}_{name}.pkl"
|
||||||
with zipfile.ZipFile(zip_path) as zipf:
|
data = load_file_from_zip(zip_path, analysis_name)
|
||||||
# Files in zip are stored with just their base names
|
if not data:
|
||||||
analysis_name = f"{zip_path.stem}_{name}.pkl"
|
|
||||||
try:
|
|
||||||
with zipf.open(analysis_name) as analysis_file:
|
|
||||||
loaded_data = joblib.load(analysis_file)
|
|
||||||
logger.info(
|
|
||||||
f"Loaded {name} candles from zip: {str(zip_path)}:{analysis_name}"
|
|
||||||
)
|
|
||||||
return loaded_data
|
|
||||||
except KeyError:
|
|
||||||
logger.exception(f"Cannot find {analysis_name} in {zip_path}")
|
|
||||||
return None
|
|
||||||
except zipfile.BadZipFile:
|
|
||||||
logger.exception(f"Bad zip file: {zip_path}")
|
|
||||||
return None
|
return None
|
||||||
|
loaded_data = joblib.load(BytesIO(data))
|
||||||
|
|
||||||
|
logger.info(f"Loaded {name} candles from zip: {str(zip_path)}:{analysis_name}")
|
||||||
|
return loaded_data
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Load from separate pickle file
|
# Load from separate pickle file
|
||||||
if backtest_dir.is_dir():
|
if backtest_dir.is_dir():
|
||||||
|
|||||||
Reference in New Issue
Block a user