1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Functions for manipulating NMR-STAR dictionary data.
24
25 This file is part of the U{BMRB library<https://gna.org/projects/bmrblib>}.
26 """
27
28
29 from numpy import ndarray
30 from warnings import warn
31
32
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
43 missing = False
44
45
46 if isinstance(data, list):
47
48 for i in range(len(data)):
49 if data[i] is None or data[i] == 'None':
50 missing = True
51
52
53 if data is None:
54 missing = True
55
56
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
71 if not reverse:
72
73 if isinstance(data, list) or isinstance(data, ndarray):
74
75 new_data = []
76 for i in range(len(data)):
77 if data[i] is None or data[i] == 'None':
78 new_data.append('?')
79 else:
80 new_data.append(str(data[i]))
81
82
83 elif data is None:
84 new_data = '?'
85
86
87 else:
88 new_data = str(data)
89
90
91 elif data is None:
92 new_data = None
93
94
95 else:
96
97 if format == 'str':
98 convert = str
99 elif format == 'int':
100 convert = int
101 elif format == 'float':
102 convert = float
103
104
105 if isinstance(data, list):
106
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
115 elif data in ['?', '.']:
116 new_data = None
117
118
119 else:
120 new_data = convert(data)
121
122
123 return new_data
124