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

Commit 9567299

Browse files
Merge pull request #455 from AurelienJaquier/withopen
use with context manager when reading/writing files
2 parents f03ed3a + be841c4 commit 9567299

File tree

5 files changed

+12
-9
lines changed

5 files changed

+12
-9
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)

bluepyopt/tests/test_deapext/test_algorithms.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""bluepyopt.optimisations tests"""
22

33
import numpy
4-
import mock
4+
from unittest import mock
55

66
import deap.creator
77
import deap.benchmarks
@@ -75,7 +75,7 @@ def test_eaAlphaMuPlusLambdaCheckpoint_with_checkpoint():
7575

7676
with mock.patch('pickle.dump'):
7777
with mock.patch('bluepyopt.deapext.algorithms.open',
78-
return_value=None):
78+
mock.mock_open()):
7979
population, hof, logbook, history = \
8080
bluepyopt.deapext.algorithms.eaAlphaMuPlusLambdaCheckpoint(
8181
population=population,
@@ -99,7 +99,7 @@ def test_eaAlphaMuPlusLambdaCheckpoint_with_checkpoint():
9999
'rndstate': random.getstate(),
100100
'generation': 1}):
101101
with mock.patch('bluepyopt.deapext.algorithms.open',
102-
return_value=None):
102+
mock.mock_open()):
103103
new_population, hof, logbook, history = \
104104
bluepyopt.deapext.algorithms.eaAlphaMuPlusLambdaCheckpoint(
105105
population=population,

bluepyopt/tests/test_ephys/test_simulators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import pytest
2828
import numpy
2929

30-
import mock
30+
from unittest import mock
3131
import numpy
3232

3333
import bluepyopt.ephys as ephys

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ extras = tests
1818
deps =
1919
coverage
2020
flake8
21-
mock
2221
neuron-nightly
2322
sh
2423
pytest-cov

0 commit comments

Comments
 (0)