Package specific_fns :: Module api_objects
[hide private]
[frames] | no frames]

Source Code for Module specific_fns.api_objects

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2012 Edward d'Auvergne                                        # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """A module of special objects used within the specific function API.""" 
 25   
 26  # Python module imports. 
 27  from types import FunctionType, MethodType 
 28   
 29  # relax module imports. 
 30  from relax_errors import RelaxError 
 31   
 32   
33 -class Param_list:
34 """A special object for handling global and spin parameters.""" 35
36 - def __init__(self):
37 """Set up the class.""" 38 39 # Initialise the lists and dictionaries for the parameter info. 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 # Append the values. 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 # The parameter string. 76 if string: 77 self._string[name] = string 78 else: 79 self._string[name] = name 80 81 # The Grace string. 82 if grace_string: 83 self._grace_string[name] = grace_string 84 else: 85 self._grace_string[name] = name
86 87
88 - def contains(self, name):
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 # Check. 98 if name in self._names: 99 return True 100 101 # No match. 102 return False
103 104
105 - def get_conv_factor(self, name):
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 # Check. 115 if name not in self._names: 116 return 1.0 117 118 # No factor. 119 if self._conv_factor[name] == None: 120 return 1.0 121 122 # Function. 123 if isinstance(self._conv_factor[name], FunctionType) or isinstance(self._conv_factor[name], MethodType): 124 return self._conv_factor[name]() 125 126 # Value. 127 return self._conv_factor[name]
128 129
130 - def get_default(self, name):
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 # Check. 140 if name not in self._names: 141 return None 142 143 # Return the default value. 144 return self._defaults[name]
145 146
147 - def get_desc(self, name):
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 # Check. 157 if name not in self._names: 158 return None 159 160 # Return the description. 161 return self._desc[name]
162 163
164 - def get_grace_string(self, name):
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 # Check. 174 if name not in self._names: 175 raise RelaxError("The parameter '%s' does not exist." % name) 176 177 # Return the value. 178 return self._grace_string[name]
179 180
181 - def get_units(self, name):
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 # Check. 191 if name not in self._names: 192 raise RelaxError("The parameter '%s' does not exist." % name) 193 194 # Function. 195 if isinstance(self._conv_factor[name], FunctionType) or isinstance(self._conv_factor[name], MethodType): 196 return self._units[name]() 197 198 # Return the value. 199 return self._units[name]
200