# Recipes Some recipes on how to write sardana macros and general python code. Aimed at helping MSPD scientists. ## Fast shutter There are a set of macros which can be used from spock: * `fsenable`: enables the usage of fast shutter (ie, sets environment: `_fsShutter = True`) * `fdisable`: disables the usage of fast shutter (ie, sets environment: `_fsShutter = False`) * `fsstatus`: prints fast shutter status * `fsopen`: opens the fast shutter (warning: it doesn't respect the shutter enabled state, ie, always opens the shutter) * `fsclose`: closes the fast shutter (warning: it doesn't respect the shutter enabled state, ie, always closes the shutter) ### How to use fast shutter in macros The `fs_opened` and `fs_closed` helpers allow you work with the shutter opened or the shutter closed respectively. Better explained with an example: ```python import time from sardana.macroserver.macro import Macro from mspd.sardana.macro.util.fast_shutter import fs_opened class super_example(Macro): def run(self): with fs_opened(self): # opens fast shutter (if enabled) # the code indented here will all be executed # while the shutter is opened. for i in range(5): time.sleep(0.5) self.output('While shutter is open ( #%d/5 )...' % i+1) # you can do as many instructions as you want. After this line # the shutter is ensured to be closed even if Ctrl-C happens self.output("Finished!") ``` Executing the macro should output something like: ``` Door [1]: super_example Opening fs (was close)... [DONE] While shutter is open ( 1/5 )... While shutter is open ( 2/5 )... While shutter is open ( 3/5 )... While shutter is open ( 4/5 )... While shutter is open ( 5/5 )... Closing fs (was open) Finished! Door [2]: ``` *Option 1: control output verbosity* If you don't want the open and close messages to show just call `fs_opened` with `verbose=False`. Like this: ```python with fs_opened(self, verbose=False): ... ``` *Option 2: strict mode* By default the `fs_opened` and `fs_closed` only do their work if the shutter is enabled. If the shutter is disabled these functions have no effect. If, instead, you want your macro to fail if the shutter is not enabled, you can pass `strict=True`. Like this: ```python with fs_opened(self, strict=True): ... ```