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-2013 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  This file is part of the U{BMRB library<https://sourceforge.net/projects/bmrblib>}. 
 26  """ 
 27   
 28  # Python module imports. 
 29  from numpy import ndarray 
 30  from warnings import warn 
 31   
 32   
33 -def no_missing(data, name):
34 """Check that there are no None values in the data. 35 36 @param data: The data to check. 37 @type data: anything 38 @param name: The name associated with the data. 39 @type name: str 40 """ 41 42 # Init. 43 missing = False 44 45 # List data. 46 if isinstance(data, list): 47 # Loop over the data. 48 for i in range(len(data)): 49 if data[i] == None or data[i] == 'None': 50 missing = True 51 52 # None. 53 if data == None: 54 missing = True 55 56 # Fail. 57 if missing: 58 raise NameError("Data is missing from the " + name + '.')
59 60
61 -def translate(data, format='str', reverse=False):
62 """Translate all values back-and-forth between Python structures and NMR-STAR strings. 63 64 @param data: The data to translate. 65 @type data: anything 66 @keyword format: The format to convert to. This can be 'str', 'int', or 'float'. 67 @type format: str 68 """ 69 70 # From Python to NMR-STAR. 71 if not reverse: 72 # List data (including numpy arrays). 73 if isinstance(data, list) or isinstance(data, ndarray): 74 # Loop over the data. 75 new_data = [] 76 for i in range(len(data)): 77 if data[i] == None or data[i] == 'None': 78 new_data.append('?') 79 else: 80 new_data.append(str(data[i])) 81 82 # None. 83 elif data == None: 84 new_data = '?' 85 86 # Otherwise normal conversion. 87 else: 88 new_data = str(data) 89 90 # The data is None. 91 elif data == None: 92 new_data = None 93 94 # From NMR-STAR to Python. 95 else: 96 # Conversion function. 97 if format == 'str': 98 convert = str 99 elif format == 'int': 100 convert = int 101 elif format == 'float': 102 convert = float 103 104 # List data. 105 if isinstance(data, list): 106 # Loop over the data. 107 new_data = [] 108 for i in range(len(data)): 109 if data[i] in ['?', '.']: 110 new_data.append(None) 111 else: 112 new_data.append(convert(data[i])) 113 114 # None. 115 elif data in ['?', '.']: 116 new_data = None 117 118 # Otherwise normal conversion. 119 else: 120 new_data = convert(data) 121 122 # Return the translated result. 123 return new_data
124