1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """A module of special objects used within the specific function API."""
25
26
27 from types import FunctionType, MethodType
28
29
30 from relax_errors import RelaxError
31
32
34 """A special object for handling global and spin parameters."""
35
37 """Set up the class."""
38
39
40 self._names = []
41 self._string = {}
42 self._defaults = {}
43 self._units = {}
44 self._desc = {}
45 self._conv_factor = {}
46 self._grace_string = {}
47
48
49 - def add(self, name, string=None, default=None, units=None, desc=None, conv_factor=None, grace_string=None):
50 """Add a parameter to the list.
51
52 @param name: The name of the parameter. This will be used as the variable name.
53 @type name: str
54 @keyword string: The string representation of the parameter.
55 @type string: None or str
56 @keyword default: The default value of the parameter.
57 @type default: anything
58 @keyword units: A string representing the parameters units.
59 @type units: None or str
60 @keyword desc: The text description of the parameter.
61 @type desc: None or str
62 @keyword conv_factor: The factor of conversion between different parameter units.
63 @type conv_factor: None, float or func
64 @keyword grace_string: The string used for the axes in Grace plots of the data.
65 @type grace_string: None or str
66 """
67
68
69 self._names.append(name)
70 self._defaults[name] = default
71 self._units[name] = units
72 self._desc[name] = desc
73 self._conv_factor[name] = conv_factor
74
75
76 if string:
77 self._string[name] = string
78 else:
79 self._string[name] = name
80
81
82 if grace_string:
83 self._grace_string[name] = grace_string
84 else:
85 self._grace_string[name] = name
86
87
89 """Determine if the given name is within the parameter list.
90
91 @param name: The name of the parameter to search for.
92 @type name: str
93 @return: True if the parameter is within the list, False otherwise.
94 @rtype: bool
95 """
96
97
98 if name in self._names:
99 return True
100
101
102 return False
103
104
106 """Return the conversion factor.
107
108 @param name: The name of the parameter.
109 @type name: str
110 @return: The conversion factor.
111 @rtype: float
112 """
113
114
115 if name not in self._names:
116 return 1.0
117
118
119 if self._conv_factor[name] == None:
120 return 1.0
121
122
123 if isinstance(self._conv_factor[name], FunctionType) or isinstance(self._conv_factor[name], MethodType):
124 return self._conv_factor[name]()
125
126
127 return self._conv_factor[name]
128
129
131 """Return the default value of the parameter.
132
133 @param name: The name of the parameter.
134 @type name: str
135 @return: The default value.
136 @rtype: None or str
137 """
138
139
140 if name not in self._names:
141 return None
142
143
144 return self._defaults[name]
145
146
148 """Return the description of the parameter.
149
150 @param name: The name of the parameter.
151 @type name: str
152 @return: The description.
153 @rtype: None or str
154 """
155
156
157 if name not in self._names:
158 return None
159
160
161 return self._desc[name]
162
163
165 """Return the Grace string for the parameter.
166
167 @param name: The name of the parameter.
168 @type name: str
169 @return: The Grace string.
170 @rtype: str
171 """
172
173
174 if name not in self._names:
175 raise RelaxError("The parameter '%s' does not exist." % name)
176
177
178 return self._grace_string[name]
179
180
182 """Return the units string for the parameter.
183
184 @param name: The name of the parameter.
185 @type name: str
186 @return: The units string.
187 @rtype: str
188 """
189
190
191 if name not in self._names:
192 raise RelaxError("The parameter '%s' does not exist." % name)
193
194
195 if isinstance(self._conv_factor[name], FunctionType) or isinstance(self._conv_factor[name], MethodType):
196 return self._units[name]()
197
198
199 return self._units[name]
200