Skip to content

Code Techniques

Tom Willemsen edited this page Mar 22, 2018 · 3 revisions

Wiki > Code Techniques

Common and useful ways of coding scripts.

Creating scripts which work on multiple instruments but must be different

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
Clone this wiki locally