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__ = ["pt0", "pt1", "pt2", "pt3", "pt3d", "pt4", "pt5", "pt6", "pt7",
 29           "pt7d1", "pt7d2", "pt8", "pt9", "pt10", "pt11", "pt12", "pt13",
 30           "pt14", "pt14d", "twice"]
 31
 32
 33class pt0(Macro):
 34    """Macro without parameters. Pretty dull.
 35       Usage from Spock, ex.:
 36       pt0
 37       """
 38
 39    param_def = []
 40
 41    def run(self):
 42        pass
 43
 44
 45class pt1(Macro):
 46    """Macro with one float parameter: Each parameter is described in the
 47    param_def sequence as being a sequence of four elements: name, type,
 48    default value and description.
 49    Usage from Spock, ex.:
 50    pt1 1
 51    """
 52
 53    param_def = [['value', Type.Float, None, 'some bloody float']]
 54
 55    def run(self, f):
 56        pass
 57
 58
 59class pt1d(Macro):
 60    """Macro with one float parameter with default value..
 61    Usage from Spock, ex.:
 62    pt1d 1
 63    pt1d
 64    """
 65
 66    param_def = [['value', Type.Float, None, 'some bloody float']]
 67
 68    def run(self, f):
 69        pass
 70
 71
 72class pt2(Macro):
 73    """Macro with one Motor parameter: Each parameter is described in the
 74    param_def sequence as being a sequence of four elements: name, type,
 75    default value and description.
 76    Usage from Spock, ex.
 77    pt2 mot1
 78    """
 79
 80    param_def = [['motor', Type.Motor, None, 'some bloody motor']]
 81
 82    def run(self, m):
 83        pass
 84
 85
 86class pt3(Macro):
 87    """Macro with a list of numbers as parameter: the type is a sequence of
 88    parameter types which is repeated. In this case it is a repetition of a
 89    float so only one parameter is defined.
 90    By default the repetition as a semantics of 'at least one'
 91    Usages from Spock, ex.:
 92    pt3 [1 34 15]
 93    pt3 1 34 15
 94    """
 95
 96    param_def = [
 97        ['numb_list', [['pos', Type.Float, None, 'value']], None, 'List of values'],
 98    ]
 99
100    def run(self, *args, **kwargs):
101        pass
102
103
104class pt3d(Macro):
105    """Macro with a list of numbers as parameter: the type is a sequence of
106    parameter types which is repeated. In this case it is a repetition of a
107    float so only one parameter is defined. The parameter has a default value.
108    By default the repetition as a semantics of 'at least one'
109    Usages from Spock, ex.:
110    pt3d [1 34 15]
111    pt3d 1 34 15
112    Usage taken the default value, ex.:
113    pt3d [1 [] 15]
114    """
115
116    param_def = [
117        ['numb_list', [['pos', Type.Float, 21, 'value']], None, 'List of values'],
118    ]
119
120    def run(self, *args, **kwargs):
121        pass
122
123
124class pt4(Macro):
125    """Macro with a list of motors as parameter: the type is a sequence of
126    parameter types which is repeated. In this case it is a repetition of a
127    motor so only one parameter is defined.
128    By default the repetition as a semantics of 'at least one'.
129    Usages from Spock, ex.:
130    pt4 [mot1 mot2 mot3]
131    pt4 mot1 mot2 mot3
132    """
133
134    param_def = [
135        ['motor_list', [['motor', Type.Motor, None, 'motor name']],
136            None, 'List of motors'],
137    ]
138
139    def run(self, *args, **kwargs):
140        pass
141
142
143class pt5(Macro):
144    """Macro with a motor parameter followed by a list of numbers.
145    Usages from Spock, ex.:
146    pt5 mot1 [1 3]
147    pt5 mot1 1 3
148    """
149
150    param_def = [
151        ['motor', Type.Motor, None, 'Motor to move'],
152        ['numb_list', [['pos', Type.Float, None, 'value']], None, 'List of values'],
153    ]
154
155    def run(self, *args, **kwargs):
156        pass
157
158
159class pt6(Macro):
160    """Macro with a motor parameter followed by a list of numbers. The list as
161    explicitly stated an optional last element which is a dictionary that defines the
162    min and max values for repetitions.
163    Usages from Spock, ex.:
164    pt6 mot1 [1 34 1]
165    pt6 mot1 1 34 1
166    """
167
168    param_def = [
169        ['motor', Type.Motor, None, 'Motor to move'],
170        ['numb_list', [['pos', Type.Float, None, 'value'], {
171            'min': 1, 'max': None}], None, 'List of values'],
172    ]
173
174    def run(self, *args, **kwargs):
175        pass
176
177
178class pt7(Macro):
179    """Macro with a list of pair Motor,Float.
180    Usages from Spock, ex.:
181    pt7 [[mot1 1] [mot2 3]]
182    pt7 mot1 1 mot2 3
183    """
184
185    param_def = [
186        ['m_p_pair', [['motor', Type.Motor, None, 'Motor to move'],
187                      ['pos',   Type.Float, None, 'Position to move to']],
188         None, 'List of motor/position pairs']
189    ]
190
191    def run(self, *args, **kwargs):
192        pass
193
194
195class pt7d1(Macro):
196    """Macro with a list of pair Motor,Float. Default value for last
197    repeat parameter element.
198    Usages from Spock, ex.:
199    pt7d1 [[mot1 1] [mot2 3]]
200    pt7d1 mot1 1 mot2 3
201    Using default value, ex.:
202    pt7d1 [[mot1] [mot2 3]] # at any repetition
203    pt7d1 mot1 # if only one repetition
204
205    """
206
207    param_def = [
208        ['m_p_pair', [['motor', Type.Motor, None, 'Motor to move'],
209                      ['pos',   Type.Float, 2, 'Position to move to']],
210         None, 'List of motor/position pairs']
211    ]
212
213    def run(self, *args, **kwargs):
214        pass
215
216
217class pt7d2(Macro):
218    """Macro with a list of pair Motor,Float. Default value for both
219    repeat parameters elements.
220    Usages from Spock, ex.:
221    pt7d2 [[mot1 1] [mot2 3]]
222    pt7d2 mot1 1 mot2 3
223    Using both default values, ex.:
224    pt7d2 [[] [mot2 3] []] # at any repetition
225    """
226
227    param_def = [
228        ['m_p_pair', [['motor', Type.Motor, 'mot1', 'Motor to move'],
229                      ['pos',   Type.Float, 2, 'Position to move to']],
230         None, 'List of motor/position pairs']
231    ]
232
233    def run(self, *args, **kwargs):
234        pass
235
236
237class pt8(Macro):
238    """Macro with a list of pair Motor,Float. The min and max elements have been
239    explicitly stated.
240    Usages from Spock, ex.:
241    pt8 [[mot1 1] [mot2 3]]
242    pt8 mot1 1 mot2 3
243    """
244
245    param_def = [
246        ['m_p_pair', [['motor', Type.Motor, None, 'Motor to move'],
247                      ['pos',   Type.Float, None, 'Position to move to'],
248                      {'min': 1, 'max': 2}],
249         None, 'List of motor/position pairs']
250    ]
251
252    def run(self, *args, **kwargs):
253        pass
254
255
256class pt9(Macro):
257    """Same as macro pt7 but with min and max number of repetitions of the
258    repeat parameter.
259    Usages from Spock, ex.:
260    pt9 [[mot1 1][mot2 3]]
261    pt9 mot1 1 mot2 3
262    """
263
264    param_def = [
265        ['m_p_pair', [['motor', Type.Motor, None, 'Motor to move'],
266                      ['pos',  Type.Float, None, 'Position to move to'],
267                      {'min': 1, 'max': 2}],
268         None, 'List of motor/position pairs'],
269    ]
270
271    def run(self, *args, **kwargs):
272        pass
273
274
275class pt10(Macro):
276    """Macro with list of numbers followed by a motor parameter. The repeat
277    parameter may be defined as first one.
278    Usage from Spock, ex.:
279    pt10 [1 3] mot1
280    pt10 1 mot1 # if only one repetition
281    """
282
283    param_def = [
284        ['numb_list', [['pos', Type.Float, None, 'value']], None, 'List of values'],
285        ['motor', Type.Motor, None, 'Motor to move']
286    ]
287
288    def run(self, *args, **kwargs):
289        pass
290
291
292class pt11(Macro):
293    """Macro with counter parameter followed by a list of numbers, followed by
294    a motor parameter. The repeat parameter may be defined in between other
295    parameters.
296    Usages from Spock, ex.:
297    pt11 ct1 [1 3] mot1
298    pt11 ct1 1 mot1 # if only one repetition
299    """
300
301    param_def = [
302        ['counter', Type.ExpChannel, None, 'Counter to count'],
303        ['numb_list', [['pos', Type.Float, None, 'value']], None, 'List of values'],
304        ['motor', Type.Motor, None, 'Motor to move']
305    ]
306
307    def run(self, *args, **kwargs):
308        pass
309
310
311class pt12(Macro):
312    """Macro with list of motors followed by list of numbers. Two repeat
313    parameters may defined.
314    Usage from Spock, ex.:
315    pt12 [1 3 4] [mot1 mot2]
316    pt12 1 mot1 # if only one repetition for each repeat parameter
317    """
318
319    param_def = [
320        ['numb_list', [['pos', Type.Float, None, 'value']], None, 'List of values'],
321        ['motor_list', [['motor', Type.Motor, None, 'Motor to move']],
322         None, 'List of motors']
323    ]
324
325    def run(self, *args, **kwargs):
326        pass
327
328
329class pt13(Macro):
330    """Macro with list of motors groups, where each motor group is a list of
331    motors. Repeat parameters may be defined as nested.
332    Usage from Spock, ex.:
333    pt13 [[mot1 mot2] [mot3 mot4]]
334"""
335
336    param_def = [
337        ['motor_group_list',
338         [['motor_list', [['motor', Type.Motor, None, 'Motor to move']],
339           None, 'List of motors']],
340         None, 'Motor groups']
341    ]
342
343    def run(self, *args, **kwargs):
344        pass
345
346
347class pt14(Macro):
348    """Macro with list of motors groups, where each motor group is a list of
349    motors and a float. Repeat parameters may be defined as nested.
350    Usage from Spock, ex.:
351    pt14 [[[mot1 mot2] 3] [[mot3] 5]]
352    """
353
354    param_def = [
355        ['motor_group_list',
356         [['motor_list', [['motor', Type.Motor, None, 'Motor to move']], None, 'List of motors'],
357          ['float', Type.Float, None, 'Number']],
358         None, 'Motor groups']
359    ]
360
361    def run(self, *args, **kwargs):
362        pass
363
364
365class pt14d(Macro):
366    """Macro with list of motors groups, where each motor group is a list of
367    motors and a float. Repeat parameters may be defined as nested.
368    Default values can be used.
369    Usages taken default values, ex.:
370    pt14d [[[mot1 mot2] 3] [[mot3] []]]
371    pt14d [[[mot1 []] 3] [[mot3] []]]
372    pt14d [[[[]] 3] [[mot3] []]]
373    """
374
375    param_def = [
376        ['motor_group_list',
377         [['motor_list', [['motor', Type.Motor, 'mot1', 'Motor to move']], None, 'List of motors'],
378          ['float', Type.Float, 33, 'Number']],
379         None, 'Motor groups']
380    ]
381
382    def run(self, *args, **kwargs):
383        pass
384
385
386class twice(Macro):
387    """A macro that returns a float that is twice its input. It also sets its
388    data to be a dictionary with 'in','out' as keys and value,result
389    as values, respectively"""
390
391    # uncomment the following lines as necessary. Otherwise you may delete them
392    param_def = [["value", Type.Float, 23, "value to be doubled"]]
393    result_def = [["result", Type.Float, None,
394                   "the double of the given value"]]
395    #hints = {}
396    # env = (,)
397
398    # uncomment the following lines if need prepare. Otherwise you may delete them
399    # def prepare(self):
400    #    pass
401
402    def run(self, n):
403        ret = 2 * n
404        self.setData({'in': n, 'out': ret})
405        return ret