Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 2b80705

Browse files
author
Jaquier Aurélien Tristan
committed
use with context manager when reading/writing files
1 parent f03ed3a commit 2b80705

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

bluepyopt/deapext/algorithms.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def eaAlphaMuPlusLambdaCheckpoint(
123123

124124
if continue_cp:
125125
# A file name has been given, then load the data from the file
126-
cp = pickle.load(open(cp_filename, "rb"))
126+
with open(cp_filename, "rb") as f:
127+
cp = pickle.load(f)
127128
population = cp["population"]
128129
parents = cp["parents"]
129130
start_gen = cp["generation"]
@@ -185,7 +186,8 @@ def eaAlphaMuPlusLambdaCheckpoint(
185186
logbook=logbook,
186187
rndstate=random.getstate(),
187188
param_names=param_names)
188-
pickle.dump(cp, open(cp_filename_tmp, "wb"))
189+
with open(cp_filename_tmp, "wb") as f:
190+
pickle.dump(cp, f)
189191
if os.path.isfile(cp_filename_tmp):
190192
shutil.copy(cp_filename_tmp, cp_filename)
191193
logger.debug('Wrote checkpoint to %s', cp_filename)

bluepyopt/deapext/optimisationsCMA.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ def run(
265265
if continue_cp:
266266

267267
# A file name has been given, then load the data from the file
268-
cp = pickle.load(open(cp_filename, "rb"))
268+
with open(cp_filename, "rb") as f:
269+
cp = pickle.load(f)
269270
gen = cp["generation"]
270271
self.hof = cp["halloffame"]
271272
logbook = cp["logbook"]
@@ -361,7 +362,8 @@ def run(
361362
CMA_es=CMA_es,
362363
param_names=param_names,
363364
)
364-
pickle.dump(cp, open(cp_filename_tmp, "wb"))
365+
with open(cp_filename_tmp, "wb") as f:
366+
pickle.dump(cp, f)
365367
if os.path.isfile(cp_filename_tmp):
366368
shutil.copy(cp_filename_tmp, cp_filename)
367369
logger.debug("Wrote checkpoint to %s", cp_filename)

0 commit comments

Comments
 (0)