chore: improve zip file loading interface

This commit is contained in:
Matthias
2024-12-26 15:14:57 +01:00
parent 0682d12fe3
commit 7a619594f6
2 changed files with 5 additions and 4 deletions
+3 -2
View File
@@ -404,12 +404,13 @@ 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: def load_file_from_zip(zip_path: Path, filename: str) -> bytes:
""" """
Load a file from a zip file Load a file from a zip file
:param zip_path: Path to the zip file :param zip_path: Path to the zip file
:param filename: Name of the file to load :param filename: Name of the file to load
:return: Bytes of the file :return: Bytes of the file
:raises: ValueError if loading goes wrong.
""" """
try: try:
with zipfile.ZipFile(zip_path) as zipf: with zipfile.ZipFile(zip_path) as zipf:
@@ -417,7 +418,7 @@ def load_file_from_zip(zip_path: Path, filename: str) -> bytes | None:
return file.read() return file.read()
except zipfile.BadZipFile: except zipfile.BadZipFile:
logger.exception(f"Bad zip file: {zip_path}") logger.exception(f"Bad zip file: {zip_path}")
return None raise ValueError(f"Bad zip file: {zip_path}") from None
def load_backtest_analysis_data(backtest_dir: Path, name: str): def load_backtest_analysis_data(backtest_dir: Path, name: str):
@@ -1,7 +1,7 @@
import logging import logging
from io import BytesIO, StringIO from io import BytesIO, StringIO
from pathlib import Path from pathlib import Path
from typing import Any, TextIO from typing import Any
from zipfile import ZIP_DEFLATED, ZipFile from zipfile import ZIP_DEFLATED, ZipFile
from pandas import DataFrame from pandas import DataFrame
@@ -16,7 +16,7 @@ from freqtrade.optimize.backtest_caching import get_backtest_metadata_filename
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def file_dump_joblib(file_obj: TextIO, data: Any, log: bool = True) -> None: def file_dump_joblib(file_obj: BytesIO, data: Any, log: bool = True) -> None:
""" """
Dump object data into a file Dump object data into a file
:param filename: file to create :param filename: file to create