Skip to content

Commit

Permalink
Use try/finally in context manager (#1383)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrapin authored Mar 14, 2022
1 parent c403b08 commit 527c727
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions nevergrad/parametrization/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,13 @@ def deterministic_sampling(parameter: core.Parameter) -> tp.Iterator[None]:
deterministic = [lay.deterministic for lay in int_layers]
for lay in int_layers:
lay.deterministic = True
yield
# sample and reset the previous behavior
parameter.value # pylint: disable=pointless-statement
for lay, det in zip(int_layers, deterministic):
lay.deterministic = det
try:
yield
finally:
# sample and reset the previous behavior
parameter.value # pylint: disable=pointless-statement
for lay, det in zip(int_layers, deterministic):
lay.deterministic = det


def _fully_bounded_layers(data: pdata.Data) -> tp.List[_datalayers.BoundLayer]:
Expand Down

0 comments on commit 527c727

Please sign in to comment.