Package bmrblib :: Module misc
[hide private]
[frames] | no frames]

Source Code for Module bmrblib.misc

  1  ############################################################################# 
  2  #                                                                           # 
  3  # The BMRB library.                                                         # 
  4  #                                                                           # 
  5  # Copyright (C) 2009-2010 Edward d'Auvergne                                 # 
  6  #                                                                           # 
  7  # This program 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 3 of the License, or         # 
 10  # (at your option) any later version.                                       # 
 11  #                                                                           # 
 12  # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.     # 
 19  #                                                                           # 
 20  ############################################################################# 
 21   
 22  # Module docstring. 
 23  """Functions for manipulating NMR-STAR dictionary data.""" 
 24   
 25  # Python module imports. 
 26  from numpy import ndarray 
 27  from warnings import warn 
 28   
 29   
30 -def no_missing(data, name):
31 """Check that there are no None values in the data. 32 33 @param data: The data to check. 34 @type data: anything 35 @param name: The name associated with the data. 36 @type name: str 37 """ 38 39 # Init. 40 missing = False 41 42 # List data. 43 if isinstance(data, list): 44 # Loop over the data. 45 for i in range(len(data)): 46 if data[i] == None or data[i] == 'None': 47 missing = True 48 49 # None. 50 if data == None: 51 missing = True 52 53 # Fail. 54 if missing: 55 raise NameError("Data is missing from the " + name + '.')
56 57
58 -def translate(data, format='str', reverse=False):
59 """Translate all values back-and-forth between Python structures and NMR-STAR strings. 60 61 @param data: The data to translate. 62 @type data: anything 63 @keyword format: The format to convert to. This can be 'str', 'int', or 'float'. 64 @type format: str 65 """ 66 67 # From Python to NMR-STAR. 68 if not reverse: 69 # List data (including numpy arrays). 70 if isinstance(data, list) or isinstance(data, ndarray): 71 # Loop over the data. 72 new_data = [] 73 for i in range(len(data)): 74 if data[i] == None or data[i] == 'None': 75 new_data.append('?') 76 else: 77 new_data.append(str(data[i])) 78 79 # None. 80 elif data == None: 81 new_data = '?' 82 83 # Otherwise normal conversion. 84 else: 85 new_data = str(data) 86 87 # The data is None. 88 elif data == None: 89 new_data = None 90 91 # From NMR-STAR to Python. 92 else: 93 # Conversion function. 94 if format == 'str': 95 convert = str 96 elif format == 'int': 97 convert = int 98 elif format == 'float': 99 convert = float 100 101 # List data. 102 if isinstance(data, list): 103 # Loop over the data. 104 new_data = [] 105 for i in range(len(data)): 106 if data[i] in ['?', '.']: 107 new_data.append(None) 108 else: 109 new_data.append(convert(data[i])) 110 111 # None. 112 elif data in ['?', '.']: 113 new_data = None 114 115 # Otherwise normal conversion. 116 else: 117 new_data = convert(data) 118 119 # Return the translated result. 120 return new_data
121