-
Notifications
You must be signed in to change notification settings - Fork 8
Code Techniques
Tom Willemsen edited this page Mar 22, 2018
·
3 revisions
Common and useful ways of coding scripts.
Sometime you will want to create a high level routine, like taking a measurement, where the technique is general across multiple instruments but the detail are different on each instrument, e.g. turn on the right number of detectors. The general strategy for this is to get the instrument name (see https://github.com/ISISComputingGroup/IBEX/issues/3052) and then do something sensible either:
instrument = g.get_instrument_py_name()
if instrument == "inst1":
do x
elif instrument == "inst2":
do y
else
print "Error"
or
import importlib
try:
module_name = "{}.measurement".format(g.get_instrument_py_name())
my_measurement = importlib.import_module(module_name)
except ImportError:
print("Can not import measurement for the instrument {}".format(g.get_instrument_py_name()))
def measurement():
return my_measurement.turn_on_detector() # This will fail when called if the module can not be imported