use Path.open() instead of open

This commit is contained in:
Matthias
2023-02-25 17:08:02 +01:00
parent c8a4a773ee
commit d014e4590e
10 changed files with 17 additions and 17 deletions

View File

@@ -58,7 +58,7 @@ def load_config_file(path: str) -> Dict[str, Any]:
"""
try:
# Read config from stdin if requested in the options
with open(path) if path != '-' else sys.stdin as file:
with Path(path).open() if path != '-' else sys.stdin as file:
config = rapidjson.load(file, parse_mode=CONFIG_PARSE_MODE)
except FileNotFoundError:
raise OperationalException(

View File

@@ -24,9 +24,9 @@ def _load_signal_candles(backtest_dir: Path):
scpf = Path(backtest_dir.parent / f"{backtest_dir.stem}_signals.pkl")
try:
scp = open(scpf, "rb")
signal_candles = joblib.load(scp)
logger.info(f"Loaded signal candles: {str(scpf)}")
with scpf.open("rb") as scp:
signal_candles = joblib.load(scp)
logger.info(f"Loaded signal candles: {str(scpf)}")
except Exception as e:
logger.error("Cannot load signal candles from pickled results: ", e)

View File

@@ -195,7 +195,7 @@ class Binance(Exchange):
leverage_tiers_path = (
Path(__file__).parent / 'binance_leverage_tiers.json'
)
with open(leverage_tiers_path) as json_file:
with leverage_tiers_path.open() as json_file:
return json_load(json_file)
else:
try:

View File

@@ -81,7 +81,7 @@ def file_dump_json(filename: Path, data: Any, is_zip: bool = False, log: bool =
else:
if log:
logger.info(f'dumping json to "{filename}"')
with open(filename, 'w') as fp:
with filename.open('w') as fp:
rapidjson.dump(data, fp, default=str, number_mode=rapidjson.NM_NATIVE)
logger.debug(f'done json to "{filename}"')
@@ -98,7 +98,7 @@ def file_dump_joblib(filename: Path, data: Any, log: bool = True) -> None:
if log:
logger.info(f'dumping joblib to "{filename}"')
with open(filename, 'wb') as fp:
with filename.open('wb') as fp:
joblib.dump(data, fp)
logger.debug(f'done joblib dump to "{filename}"')
@@ -112,7 +112,7 @@ def json_load(datafile: IO) -> Any:
return rapidjson.load(datafile, number_mode=rapidjson.NM_NATIVE)
def file_load_json(file):
def file_load_json(file: Path):
if file.suffix != ".gz":
gzipfile = file.with_suffix(file.suffix + '.gz')
@@ -125,7 +125,7 @@ def file_load_json(file):
pairdata = json_load(datafile)
elif file.is_file():
logger.debug(f"Loading historical data from file {file}")
with open(file) as datafile:
with file.open() as datafile:
pairdata = json_load(datafile)
else:
return None

View File

@@ -29,7 +29,7 @@ def get_strategy_run_id(strategy) -> str:
# Include _ft_params_from_file - so changing parameter files cause cache eviction
digest.update(rapidjson.dumps(
strategy._ft_params_from_file, default=str, number_mode=rapidjson.NM_NAN).encode('utf-8'))
with open(strategy.__file__, 'rb') as fp:
with Path(strategy.__file__).open('rb') as fp:
digest.update(fp.read())
return digest.hexdigest().lower()

View File

@@ -157,7 +157,7 @@ class RemotePairList(IPairList):
file_path = Path(filename)
if file_path.exists():
with open(filename) as json_file:
with file_path.open() as json_file:
# Load the JSON data into a dictionary
jsonparse = json.load(json_file)