Macro input examples

This chapter consists of a series of examples demonstrating how to ask for user input inside macros.

A tutorial on macro input parameter can be found here. The API documentation: input()

 1
 2from sardana.macroserver.macro import imacro, Type
 3
 4
 5@imacro()
 6def ask_number_of_points(self):
 7    """asks user for the number of points"""
 8
 9    nb_points = self.input("How many points?", data_type=Type.Integer)
10
11
12@imacro()
13def ask_for_moveable(self):
14    """asks user for a motor"""
15
16    moveable = self.input("Which moveable?", data_type=Type.Moveable)
17    self.output("You selected %s which is at %f",
18                moveable, moveable.getPosition())
19
20
21@imacro()
22def ask_for_car_brand(self):
23    """asks user for a car brand"""
24
25    car_brands = "Mazda", "Citroen", "Renault"
26    car_brand = self.input("Which car brand?", data_type=car_brands)
27    self.output("You selected %s", car_brand)
28
29
30@imacro()
31def ask_for_multiple_car_brands(self):
32    """asks user for several car brands"""
33
34    car_brands = "Mazda", "Citroen", "Renault", "Ferrari", "Porche", "Skoda"
35    car_brands = self.input("Which car brand(s)?", data_type=car_brands,
36                            allow_multiple=True, title="Favorites")
37    self.output("You selected %s", ", ".join(car_brands))
38
39
40@imacro()
41def ask_peak(self):
42    """asks user for peak current of points with a custom title"""
43
44    peak = self.input("What is the peak current?", data_type=Type.Float,
45                      title="Peak selection")
46    self.output("You selected a peak of %f A", peak)
47
48
49@imacro()
50def ask_peak_v2(self):
51    """asks user for peak current of points with a custom title,
52    default value, label and units"""
53
54    label, unit = "peak", "mA"
55    peak = self.input("What is the peak current?", data_type=Type.Float,
56                      title="Peak selection", key=label, unit=unit,
57                      default_value=123.4)
58    self.output("You selected a %s of %f %s", label, peak, unit)
59
60
61@imacro()
62def ask_peak_v3(self):
63    """asks user for peak current of points with a custom title,
64    default value, label, units and ranges"""
65
66    label, unit = "peak", "mA"
67    peak = self.input("What is the peak current?", data_type=Type.Float,
68                      title="Peak selection", key=label, unit=unit,
69                      default_value=123.4, minimum=0.0, maximum=200.0)
70    self.output("You selected a %s of %f %s", label, peak, unit)
71
72
73@imacro()
74def ask_peak_v4(self):
75    """asks user for peak current of points with a custom title,
76    default value, label, units, ranges and step size"""
77
78    label, unit = "peak", "mA"
79    peak = self.input("What is the peak current?", data_type=Type.Float,
80                      title="Peak selection", key=label, unit=unit,
81                      default_value=123.4, minimum=0.0, maximum=200.0,
82                      step=5)
83    self.output("You selected a %s of %f %s", label, peak, unit)
84
85
86@imacro()
87def ask_peak_v5(self):
88    """asks user for peak current of points with a custom title,
89    default value, label, units, ranges, step size and decimal places"""
90
91    label, unit = "peak", "mA"
92    peak = self.input("What is the peak current?", data_type=Type.Float,
93                      title="Peak selection", key=label, unit=unit,
94                      default_value=123.4, minimum=0.0, maximum=200.0,
95                      step=5, decimals=2)
96    self.output("You selected a %s of %f %s", label, peak, unit)