Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

from builtins import zip 

from builtins import str 

from builtins import range 

 

import collections 

 

from .config import Field, FieldValidationError, _typeStr, _autocast, _joinNamePath 

from .comparison import compareScalars, getComparisonName 

from .callStack import getCallStack, getStackFrame 

 

__all__ = ["ListField"] 

 

 

class List(collections.MutableSequence): 

def __init__(self, config, field, value, at, label, setHistory=True): 

self._field = field 

self._config = config 

self._history = self._config._history.setdefault(self._field.name, []) 

self._list = [] 

self.__doc__ = field.doc 

42 ↛ 49line 42 didn't jump to line 49, because the condition on line 42 was never false if value is not None: 

try: 

for i, x in enumerate(value): 

self.insert(i, x, setHistory=False) 

except TypeError: 

msg = "Value %s is of incorrect type %s. Sequence type expected" % (value, _typeStr(value)) 

raise FieldValidationError(self._field, self._config, msg) 

49 ↛ exitline 49 didn't return from function '__init__', because the condition on line 49 was never false if setHistory: 

self.history.append((list(self._list), at, label)) 

 

def validateItem(self, i, x): 

 

if not isinstance(x, self._field.itemtype) and x is not None: 

msg = "Item at position %d with value %s is of incorrect type %s. Expected %s" % \ 

(i, x, _typeStr(x), _typeStr(self._field.itemtype)) 

raise FieldValidationError(self._field, self._config, msg) 

 

if self._field.itemCheck is not None and not self._field.itemCheck(x): 

msg = "Item at position %d is not a valid value: %s" % (i, x) 

raise FieldValidationError(self._field, self._config, msg) 

 

def list(self): 

return self._list 

 

""" 

Read-only history 

""" 

history = property(lambda x: x._history) 

 

def __contains__(self, x): 

return x in self._list 

 

def __len__(self): 

return len(self._list) 

 

def __setitem__(self, i, x, at=None, label="setitem", setHistory=True): 

78 ↛ 79line 78 didn't jump to line 79, because the condition on line 78 was never true if self._config._frozen: 

raise FieldValidationError(self._field, self._config, 

"Cannot modify a frozen Config") 

if isinstance(i, slice): 

k, stop, step = i.indices(len(self)) 

for j, xj in enumerate(x): 

xj = _autocast(xj, self._field.itemtype) 

self.validateItem(k, xj) 

x[j] = xj 

k += step 

else: 

x = _autocast(x, self._field.itemtype) 

self.validateItem(i, x) 

 

self._list[i] = x 

if setHistory: 

if at is None: 

at = getCallStack() 

self.history.append((list(self._list), at, label)) 

 

def __getitem__(self, i): 

return self._list[i] 

 

def __delitem__(self, i, at=None, label="delitem", setHistory=True): 

if self._config._frozen: 

raise FieldValidationError(self._field, self._config, 

"Cannot modify a frozen Config") 

del self._list[i] 

if setHistory: 

if at is None: 

at = getCallStack() 

self.history.append((list(self._list), at, label)) 

 

def __iter__(self): 

return iter(self._list) 

 

def insert(self, i, x, at=None, label="insert", setHistory=True): 

115 ↛ 117line 115 didn't jump to line 117, because the condition on line 115 was never false if at is None: 

at = getCallStack() 

self.__setitem__(slice(i, i), [x], at=at, label=label, setHistory=setHistory) 

 

def __repr__(self): 

return repr(self._list) 

 

def __str__(self): 

return str(self._list) 

 

def __eq__(self, other): 

try: 

if len(self) != len(other): 

return False 

 

for i, j in zip(self, other): 

131 ↛ 132line 131 didn't jump to line 132, because the condition on line 131 was never true if i != j: 

return False 

return True 

except AttributeError: 

# other is not a sequence type 

return False 

 

def __ne__(self, other): 

return not self.__eq__(other) 

 

def __setattr__(self, attr, value, at=None, label="assignment"): 

142 ↛ 144line 142 didn't jump to line 144, because the condition on line 142 was never true if hasattr(getattr(self.__class__, attr, None), '__set__'): 

# This allows properties to work. 

object.__setattr__(self, attr, value) 

elif attr in self.__dict__ or attr in ["_field", "_config", "_history", "_list", "__doc__"]: 

# This allows specific private attributes to work. 

object.__setattr__(self, attr, value) 

else: 

# We throw everything else. 

msg = "%s has no attribute %s" % (_typeStr(self._field), attr) 

raise FieldValidationError(self._field, self._config, msg) 

 

 

class ListField(Field): 

""" 

Defines a field which is a container of values of type dtype 

 

If length is not None, then instances of this field must match this length 

exactly. 

If minLength is not None, then instances of the field must be no shorter 

then minLength 

If maxLength is not None, then instances of the field must be no longer 

than maxLength 

 

Additionally users can provide two check functions: 

listCheck - used to validate the list as a whole, and 

itemCheck - used to validate each item individually 

""" 

def __init__(self, doc, dtype, default=None, optional=False, 

listCheck=None, itemCheck=None, 

length=None, minLength=None, maxLength=None): 

if dtype not in Field.supportedTypes: 

raise ValueError("Unsupported dtype %s" % _typeStr(dtype)) 

if length is not None: 

if length <= 0: 

raise ValueError("'length' (%d) must be positive" % length) 

minLength = None 

maxLength = None 

else: 

if maxLength is not None and maxLength <= 0: 

raise ValueError("'maxLength' (%d) must be positive" % maxLength) 

if minLength is not None and maxLength is not None \ 

and minLength > maxLength: 

raise ValueError("'maxLength' (%d) must be at least" 

" as large as 'minLength' (%d)" % (maxLength, minLength)) 

 

187 ↛ 188line 187 didn't jump to line 188, because the condition on line 187 was never true if listCheck is not None and not hasattr(listCheck, "__call__"): 

raise ValueError("'listCheck' must be callable") 

189 ↛ 190line 189 didn't jump to line 190, because the condition on line 189 was never true if itemCheck is not None and not hasattr(itemCheck, "__call__"): 

raise ValueError("'itemCheck' must be callable") 

 

source = getStackFrame() 

self._setup(doc=doc, dtype=List, default=default, check=None, optional=optional, source=source) 

self.listCheck = listCheck 

self.itemCheck = itemCheck 

self.itemtype = dtype 

self.length = length 

self.minLength = minLength 

self.maxLength = maxLength 

 

def validate(self, instance): 

""" 

ListField validation ensures that non-optional fields are not None, 

and that non-None values comply with length requirements and 

that the list passes listCheck if supplied by the user. 

Individual Item checks are applied at set time and are not re-checked. 

""" 

Field.validate(self, instance) 

value = self.__get__(instance) 

if value is not None: 

lenValue = len(value) 

212 ↛ 213line 212 didn't jump to line 213, because the condition on line 212 was never true if self.length is not None and not lenValue == self.length: 

msg = "Required list length=%d, got length=%d" % (self.length, lenValue) 

raise FieldValidationError(self, instance, msg) 

215 ↛ 216line 215 didn't jump to line 216, because the condition on line 215 was never true elif self.minLength is not None and lenValue < self.minLength: 

msg = "Minimum allowed list length=%d, got length=%d" % (self.minLength, lenValue) 

raise FieldValidationError(self, instance, msg) 

218 ↛ 219line 218 didn't jump to line 219, because the condition on line 218 was never true elif self.maxLength is not None and lenValue > self.maxLength: 

msg = "Maximum allowed list length=%d, got length=%d" % (self.maxLength, lenValue) 

raise FieldValidationError(self, instance, msg) 

221 ↛ 222line 221 didn't jump to line 222, because the condition on line 221 was never true elif self.listCheck is not None and not self.listCheck(value): 

msg = "%s is not a valid value" % str(value) 

raise FieldValidationError(self, instance, msg) 

 

def __set__(self, instance, value, at=None, label="assignment"): 

226 ↛ 227line 226 didn't jump to line 227, because the condition on line 226 was never true if instance._frozen: 

raise FieldValidationError(self, instance, "Cannot modify a frozen Config") 

 

229 ↛ 230line 229 didn't jump to line 230, because the condition on line 229 was never true if at is None: 

at = getCallStack() 

 

if value is not None: 

value = List(instance, self, value, at, label) 

else: 

history = instance._history.setdefault(self.name, []) 

history.append((value, at, label)) 

 

instance._storage[self.name] = value 

 

def toDict(self, instance): 

value = self.__get__(instance) 

return list(value) if value is not None else None 

 

def _compare(self, instance1, instance2, shortcut, rtol, atol, output): 

"""Helper function for Config.compare; used to compare two fields for equality. 

 

@param[in] instance1 LHS Config instance to compare. 

@param[in] instance2 RHS Config instance to compare. 

@param[in] shortcut If True, return as soon as an inequality is found. 

@param[in] rtol Relative tolerance for floating point comparisons. 

@param[in] atol Absolute tolerance for floating point comparisons. 

@param[in] output If not None, a callable that takes a string, used (possibly repeatedly) 

to report inequalities. 

 

Floating point comparisons are performed by numpy.allclose; refer to that for details. 

""" 

l1 = getattr(instance1, self.name) 

l2 = getattr(instance2, self.name) 

name = getComparisonName( 

_joinNamePath(instance1._name, self.name), 

_joinNamePath(instance2._name, self.name) 

) 

263 ↛ 264line 263 didn't jump to line 264, because the condition on line 263 was never true if not compareScalars("isnone for %s" % name, l1 is None, l2 is None, output=output): 

return False 

265 ↛ 266line 265 didn't jump to line 266, because the condition on line 265 was never true if l1 is None and l2 is None: 

return True 

if not compareScalars("size for %s" % name, len(l1), len(l2), output=output): 

return False 

equal = True 

for n, v1, v2 in zip(range(len(l1)), l1, l2): 

result = compareScalars("%s[%d]" % (name, n), v1, v2, dtype=self.dtype, 

rtol=rtol, atol=atol, output=output) 

273 ↛ 274line 273 didn't jump to line 274, because the condition on line 273 was never true if not result and shortcut: 

return False 

equal = equal and result 

return equal