Skip to content

Commit

Permalink
avoid code dupl
Browse files Browse the repository at this point in the history
  • Loading branch information
marscher committed Jun 18, 2018
1 parent b7b9d23 commit 13aa194
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions pyemma/util/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,18 @@ def random_seed(seed=42):
def settings(**kwargs):
""" apply given PyEMMA config values temporarily within the given context."""
from pyemma import config
# validate:
valid_keys = config.keys()
for k in kwargs.keys():
if k not in valid_keys:
raise ValueError("not a valid settings: {key}".format(key=k))

old_settings = {}
for k, v in kwargs.items():
old_settings[k] = getattr(config, k)
setattr(config, k, v)

yield

# restore old settings
for k, v in old_settings.items():
setattr(config, k, v)
try:
# remember old setting, set new one. May raise ValueError, if invalid setting is given.
for k, v in kwargs.items():
old_settings[k] = getattr(config, k)
setattr(config, k, v)
yield
finally:
# restore old settings
for k, v in old_settings.items():
setattr(config, k, v)


@contextmanager
Expand Down

0 comments on commit 13aa194

Please sign in to comment.