From b4d9840ed097fc50b62751493a32c37e535d6913 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Tue, 29 Jan 2019 13:41:35 +0000 Subject: [PATCH] Added set_properties context manager --- openflexure_microscope/utilities.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/openflexure_microscope/utilities.py b/openflexure_microscope/utilities.py index ecf38203..3a52ece9 100644 --- a/openflexure_microscope/utilities.py +++ b/openflexure_microscope/utilities.py @@ -1,6 +1,28 @@ import copy import operator from functools import reduce +from contextlib import contextmanager + +@contextmanager +def set_properties(obj, **kwargs): + """A context manager to set, then reset, certain properties of an object. + + The first argument is the object, subsequent keyword arguments are properties + of said object, which are set initially, then reset to their previous values. + """ + saved_properties = {} + for k in kwargs.keys(): + try: + saved_properties[k] = getattr(obj, k) + except AttributeError: + print("Warning: could not get {} on {}. This property will not be restored!".format(k, obj)) + for k, v in kwargs.items(): + setattr(obj, k, v) + try: + yield + finally: + for k, v in saved_properties.items(): + setattr(obj, k, v) def axes_to_array(coordinate_dictionary, axis_keys=['x', 'y', 'z'], base_array=None): """Takes key-value pairs of a JSON value, and maps onto an array"""