Skip to content

Commit

Permalink
Merge pull request #455 from AurelienJaquier/withopen
Browse files Browse the repository at this point in the history
use with context manager when reading/writing files
  • Loading branch information
AurelienJaquier authored May 3, 2023
2 parents f03ed3a + be841c4 commit 9567299
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
6 changes: 4 additions & 2 deletions bluepyopt/deapext/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def eaAlphaMuPlusLambdaCheckpoint(

if continue_cp:
# A file name has been given, then load the data from the file
cp = pickle.load(open(cp_filename, "rb"))
with open(cp_filename, "rb") as f:
cp = pickle.load(f)
population = cp["population"]
parents = cp["parents"]
start_gen = cp["generation"]
Expand Down Expand Up @@ -185,7 +186,8 @@ def eaAlphaMuPlusLambdaCheckpoint(
logbook=logbook,
rndstate=random.getstate(),
param_names=param_names)
pickle.dump(cp, open(cp_filename_tmp, "wb"))
with open(cp_filename_tmp, "wb") as f:
pickle.dump(cp, f)
if os.path.isfile(cp_filename_tmp):
shutil.copy(cp_filename_tmp, cp_filename)
logger.debug('Wrote checkpoint to %s', cp_filename)
Expand Down
6 changes: 4 additions & 2 deletions bluepyopt/deapext/optimisationsCMA.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ def run(
if continue_cp:

# A file name has been given, then load the data from the file
cp = pickle.load(open(cp_filename, "rb"))
with open(cp_filename, "rb") as f:
cp = pickle.load(f)
gen = cp["generation"]
self.hof = cp["halloffame"]
logbook = cp["logbook"]
Expand Down Expand Up @@ -361,7 +362,8 @@ def run(
CMA_es=CMA_es,
param_names=param_names,
)
pickle.dump(cp, open(cp_filename_tmp, "wb"))
with open(cp_filename_tmp, "wb") as f:
pickle.dump(cp, f)
if os.path.isfile(cp_filename_tmp):
shutil.copy(cp_filename_tmp, cp_filename)
logger.debug("Wrote checkpoint to %s", cp_filename)
Expand Down
6 changes: 3 additions & 3 deletions bluepyopt/tests/test_deapext/test_algorithms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""bluepyopt.optimisations tests"""

import numpy
import mock
from unittest import mock

import deap.creator
import deap.benchmarks
Expand Down Expand Up @@ -75,7 +75,7 @@ def test_eaAlphaMuPlusLambdaCheckpoint_with_checkpoint():

with mock.patch('pickle.dump'):
with mock.patch('bluepyopt.deapext.algorithms.open',
return_value=None):
mock.mock_open()):
population, hof, logbook, history = \
bluepyopt.deapext.algorithms.eaAlphaMuPlusLambdaCheckpoint(
population=population,
Expand All @@ -99,7 +99,7 @@ def test_eaAlphaMuPlusLambdaCheckpoint_with_checkpoint():
'rndstate': random.getstate(),
'generation': 1}):
with mock.patch('bluepyopt.deapext.algorithms.open',
return_value=None):
mock.mock_open()):
new_population, hof, logbook, history = \
bluepyopt.deapext.algorithms.eaAlphaMuPlusLambdaCheckpoint(
population=population,
Expand Down
2 changes: 1 addition & 1 deletion bluepyopt/tests/test_ephys/test_simulators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import pytest
import numpy

import mock
from unittest import mock
import numpy

import bluepyopt.ephys as ephys
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ extras = tests
deps =
coverage
flake8
mock
neuron-nightly
sh
pytest-cov
Expand Down

0 comments on commit 9567299

Please sign in to comment.