Macro parameter examples

This chapter consists of a series of examples demonstrating how to declare macros which receive parameter(s).

  1##############################################################################
  2##
  3# This file is part of Sardana
  4##
  5# http://www.sardana-controls.org/
  6##
  7# Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
  8##
  9# Sardana is free software: you can redistribute it and/or modify
 10# it under the terms of the GNU Lesser General Public License as published by
 11# the Free Software Foundation, either version 3 of the License, or
 12# (at your option) any later version.
 13##
 14# Sardana is distributed in the hope that it will be useful,
 15# but WITHOUT ANY WARRANTY; without even the implied warranty of
 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17# GNU Lesser General Public License for more details.
 18##
 19# You should have received a copy of the GNU Lesser General Public License
 20# along with Sardana.  If not, see <http://www.gnu.org/licenses/>.
 21##
 22##############################################################################
 23
 24"""This module contains macros that demonstrate the usage of macro parameters"""
 25
 26from sardana.macroserver.macro import Macro, Type
 27
 28__all__ = [
 29    "pt0",
 30    "pt1",
 31    "pt2",
 32    "pt3",
 33    "pt3d",
 34    "pt4",
 35    "pt5",
 36    "pt6",
 37    "pt7",
 38    "pt7d1",
 39    "pt7d2",
 40    "pt8",
 41    "pt9",
 42    "pt10",
 43    "pt11",
 44    "pt12",
 45    "pt13",
 46    "pt14",
 47    "pt14d",
 48    "twice",
 49]
 50
 51
 52class pt0(Macro):
 53    """Macro without parameters. Pretty dull.
 54    Usage from Spock, ex.:
 55    pt0
 56    """
 57
 58    param_def = []
 59
 60    def run(self):
 61        pass
 62
 63
 64class pt1(Macro):
 65    """Macro with one float parameter: Each parameter is described in the
 66    param_def sequence as being a sequence of four elements: name, type,
 67    default value and description.
 68    Usage from Spock, ex.:
 69    pt1 1
 70    """
 71
 72    param_def = [["value", Type.Float, None, "some bloody float"]]
 73
 74    def run(self, f):
 75        pass
 76
 77
 78class pt1d(Macro):
 79    """Macro with one float parameter with default value..
 80    Usage from Spock, ex.:
 81    pt1d 1
 82    pt1d
 83    """
 84
 85    param_def = [["value", Type.Float, None, "some bloody float"]]
 86
 87    def run(self, f):
 88        pass
 89
 90
 91class pt2(Macro):
 92    """Macro with one Motor parameter: Each parameter is described in the
 93    param_def sequence as being a sequence of four elements: name, type,
 94    default value and description.
 95    Usage from Spock, ex.
 96    pt2 mot1
 97    """
 98
 99    param_def = [["motor", Type.Motor, None, "some bloody motor"]]
100
101    def run(self, m):
102        pass
103
104
105class pt3(Macro):
106    """Macro with a list of numbers as parameter: the type is a sequence of
107    parameter types which is repeated. In this case it is a repetition of a
108    float so only one parameter is defined.
109    By default the repetition as a semantics of 'at least one'
110    Usages from Spock, ex.:
111    pt3 [1 34 15]
112    pt3 1 34 15
113    """
114
115    param_def = [
116        ["numb_list", [["pos", Type.Float, None, "value"]], None, "List of values"],
117    ]
118
119    def run(self, *args, **kwargs):
120        pass
121
122
123class pt3d(Macro):
124    """Macro with a list of numbers as parameter: the type is a sequence of
125    parameter types which is repeated. In this case it is a repetition of a
126    float so only one parameter is defined. The parameter has a default value.
127    By default the repetition as a semantics of 'at least one'
128    Usages from Spock, ex.:
129    pt3d [1 34 15]
130    pt3d 1 34 15
131    Usage taken the default value, ex.:
132    pt3d [1 [] 15]
133    """
134
135    param_def = [
136        ["numb_list", [["pos", Type.Float, 21, "value"]], None, "List of values"],
137    ]
138
139    def run(self, *args, **kwargs):
140        pass
141
142
143class pt4(Macro):
144    """Macro with a list of motors as parameter: the type is a sequence of
145    parameter types which is repeated. In this case it is a repetition of a
146    motor so only one parameter is defined.
147    By default the repetition as a semantics of 'at least one'.
148    Usages from Spock, ex.:
149    pt4 [mot1 mot2 mot3]
150    pt4 mot1 mot2 mot3
151    """
152
153    param_def = [
154        [
155            "motor_list",
156            [["motor", Type.Motor, None, "motor name"]],
157            None,
158            "List of motors",
159        ],
160    ]
161
162    def run(self, *args, **kwargs):
163        pass
164
165
166class pt5(Macro):
167    """Macro with a motor parameter followed by a list of numbers.
168    Usages from Spock, ex.:
169    pt5 mot1 [1 3]
170    pt5 mot1 1 3
171    """
172
173    param_def = [
174        ["motor", Type.Motor, None, "Motor to move"],
175        ["numb_list", [["pos", Type.Float, None, "value"]], None, "List of values"],
176    ]
177
178    def run(self, *args, **kwargs):
179        pass
180
181
182class pt6(Macro):
183    """Macro with a motor parameter followed by a list of numbers. The list as
184    explicitly stated an optional last element which is a dictionary that defines the
185    min and max values for repetitions.
186    Usages from Spock, ex.:
187    pt6 mot1 [1 34 1]
188    pt6 mot1 1 34 1
189    """
190
191    param_def = [
192        ["motor", Type.Motor, None, "Motor to move"],
193        [
194            "numb_list",
195            [["pos", Type.Float, None, "value"], {"min": 1, "max": None}],
196            None,
197            "List of values",
198        ],
199    ]
200
201    def run(self, *args, **kwargs):
202        pass
203
204
205class pt7(Macro):
206    """Macro with a list of pair Motor,Float.
207    Usages from Spock, ex.:
208    pt7 [[mot1 1] [mot2 3]]
209    pt7 mot1 1 mot2 3
210    """
211
212    param_def = [
213        [
214            "m_p_pair",
215            [
216                ["motor", Type.Motor, None, "Motor to move"],
217                ["pos", Type.Float, None, "Position to move to"],
218            ],
219            None,
220            "List of motor/position pairs",
221        ]
222    ]
223
224    def run(self, *args, **kwargs):
225        pass
226
227
228class pt7d1(Macro):
229    """Macro with a list of pair Motor,Float. Default value for last
230    repeat parameter element.
231    Usages from Spock, ex.:
232    pt7d1 [[mot1 1] [mot2 3]]
233    pt7d1 mot1 1 mot2 3
234    Using default value, ex.:
235    pt7d1 [[mot1] [mot2 3]] # at any repetition
236    pt7d1 mot1 # if only one repetition
237
238    """
239
240    param_def = [
241        [
242            "m_p_pair",
243            [
244                ["motor", Type.Motor, None, "Motor to move"],
245                ["pos", Type.Float, 2, "Position to move to"],
246            ],
247            None,
248            "List of motor/position pairs",
249        ]
250    ]
251
252    def run(self, *args, **kwargs):
253        pass
254
255
256class pt7d2(Macro):
257    """Macro with a list of pair Motor,Float. Default value for both
258    repeat parameters elements.
259    Usages from Spock, ex.:
260    pt7d2 [[mot1 1] [mot2 3]]
261    pt7d2 mot1 1 mot2 3
262    Using both default values, ex.:
263    pt7d2 [[] [mot2 3] []] # at any repetition
264    """
265
266    param_def = [
267        [
268            "m_p_pair",
269            [
270                ["motor", Type.Motor, "mot1", "Motor to move"],
271                ["pos", Type.Float, 2, "Position to move to"],
272            ],
273            None,
274            "List of motor/position pairs",
275        ]
276    ]
277
278    def run(self, *args, **kwargs):
279        pass
280
281
282class pt8(Macro):
283    """Macro with a list of pair Motor,Float. The min and max elements have been
284    explicitly stated.
285    Usages from Spock, ex.:
286    pt8 [[mot1 1] [mot2 3]]
287    pt8 mot1 1 mot2 3
288    """
289
290    param_def = [
291        [
292            "m_p_pair",
293            [
294                ["motor", Type.Motor, None, "Motor to move"],
295                ["pos", Type.Float, None, "Position to move to"],
296                {"min": 1, "max": 2},
297            ],
298            None,
299            "List of motor/position pairs",
300        ]
301    ]
302
303    def run(self, *args, **kwargs):
304        pass
305
306
307class pt9(Macro):
308    """Same as macro pt7 but with min and max number of repetitions of the
309    repeat parameter.
310    Usages from Spock, ex.:
311    pt9 [[mot1 1][mot2 3]]
312    pt9 mot1 1 mot2 3
313    """
314
315    param_def = [
316        [
317            "m_p_pair",
318            [
319                ["motor", Type.Motor, None, "Motor to move"],
320                ["pos", Type.Float, None, "Position to move to"],
321                {"min": 1, "max": 2},
322            ],
323            None,
324            "List of motor/position pairs",
325        ],
326    ]
327
328    def run(self, *args, **kwargs):
329        pass
330
331
332class pt10(Macro):
333    """Macro with list of numbers followed by a motor parameter. The repeat
334    parameter may be defined as first one.
335    Usage from Spock, ex.:
336    pt10 [1 3] mot1
337    pt10 1 mot1 # if only one repetition
338    """
339
340    param_def = [
341        ["numb_list", [["pos", Type.Float, None, "value"]], None, "List of values"],
342        ["motor", Type.Motor, None, "Motor to move"],
343    ]
344
345    def run(self, *args, **kwargs):
346        pass
347
348
349class pt11(Macro):
350    """Macro with counter parameter followed by a list of numbers, followed by
351    a motor parameter. The repeat parameter may be defined in between other
352    parameters.
353    Usages from Spock, ex.:
354    pt11 ct1 [1 3] mot1
355    pt11 ct1 1 mot1 # if only one repetition
356    """
357
358    param_def = [
359        ["counter", Type.ExpChannel, None, "Counter to count"],
360        ["numb_list", [["pos", Type.Float, None, "value"]], None, "List of values"],
361        ["motor", Type.Motor, None, "Motor to move"],
362    ]
363
364    def run(self, *args, **kwargs):
365        pass
366
367
368class pt12(Macro):
369    """Macro with list of motors followed by list of numbers. Two repeat
370    parameters may defined.
371    Usage from Spock, ex.:
372    pt12 [1 3 4] [mot1 mot2]
373    pt12 1 mot1 # if only one repetition for each repeat parameter
374    """
375
376    param_def = [
377        ["numb_list", [["pos", Type.Float, None, "value"]], None, "List of values"],
378        [
379            "motor_list",
380            [["motor", Type.Motor, None, "Motor to move"]],
381            None,
382            "List of motors",
383        ],
384    ]
385
386    def run(self, *args, **kwargs):
387        pass
388
389
390class pt13(Macro):
391    """Macro with list of motors groups, where each motor group is a list of
392    motors. Repeat parameters may be defined as nested.
393    Usage from Spock, ex.:
394    pt13 [[mot1 mot2] [mot3 mot4]]
395    """
396
397    param_def = [
398        [
399            "motor_group_list",
400            [
401                [
402                    "motor_list",
403                    [["motor", Type.Motor, None, "Motor to move"]],
404                    None,
405                    "List of motors",
406                ]
407            ],
408            None,
409            "Motor groups",
410        ]
411    ]
412
413    def run(self, *args, **kwargs):
414        pass
415
416
417class pt14(Macro):
418    """Macro with list of motors groups, where each motor group is a list of
419    motors and a float. Repeat parameters may be defined as nested.
420    Usage from Spock, ex.:
421    pt14 [[[mot1 mot2] 3] [[mot3] 5]]
422    """
423
424    param_def = [
425        [
426            "motor_group_list",
427            [
428                [
429                    "motor_list",
430                    [["motor", Type.Motor, None, "Motor to move"]],
431                    None,
432                    "List of motors",
433                ],
434                ["float", Type.Float, None, "Number"],
435            ],
436            None,
437            "Motor groups",
438        ]
439    ]
440
441    def run(self, *args, **kwargs):
442        pass
443
444
445class pt14d(Macro):
446    """Macro with list of motors groups, where each motor group is a list of
447    motors and a float. Repeat parameters may be defined as nested.
448    Default values can be used.
449    Usages taken default values, ex.:
450    pt14d [[[mot1 mot2] 3] [[mot3] []]]
451    pt14d [[[mot1 []] 3] [[mot3] []]]
452    pt14d [[[[]] 3] [[mot3] []]]
453    """
454
455    param_def = [
456        [
457            "motor_group_list",
458            [
459                [
460                    "motor_list",
461                    [["motor", Type.Motor, "mot1", "Motor to move"]],
462                    None,
463                    "List of motors",
464                ],
465                ["float", Type.Float, 33, "Number"],
466            ],
467            None,
468            "Motor groups",
469        ]
470    ]
471
472    def run(self, *args, **kwargs):
473        pass
474
475
476class twice(Macro):
477    """A macro that returns a float that is twice its input. It also sets its
478    data to be a dictionary with 'in','out' as keys and value,result
479    as values, respectively"""
480
481    # uncomment the following lines as necessary. Otherwise you may delete them
482    param_def = [["value", Type.Float, 23, "value to be doubled"]]
483    result_def = [["result", Type.Float, None, "the double of the given value"]]
484    # hints = {}
485    # env = (,)
486
487    # uncomment the following lines if need prepare. Otherwise you may delete them
488    # def prepare(self):
489    #    pass
490
491    def run(self, n):
492        ret = 2 * n
493        self.setData({"in": n, "out": ret})
494        return ret