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

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

# 

# LSST Data Management System 

# 

# Copyright 2008-2017 AURA/LSST. 

# 

# 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/>. 

# 

 

 

__all__ = ["getstate", "setstate"] 

 

import enum 

import numbers 

import warnings 

 

from lsst.utils import continueClass 

 

from .propertySet import PropertySet 

from .propertyList import PropertyList 

 

import lsst.pex.exceptions 

from ..dateTime import DateTime 

 

 

class ReturnStyle(enum.Enum): 

ARRAY = enum.auto() 

SCALAR = enum.auto() 

AUTO = enum.auto() 

 

 

def _propertyContainerElementTypeName(container, name): 

"""Return name of the type of a particular element""" 

t = container.typeOf(name) 

for checkType in ("Bool", "Short", "Int", "Long", "LongLong", "Float", "Double", "String", "DateTime"): 

if t == getattr(container, "TYPE_" + checkType): 

return checkType 

return None 

 

 

def _propertyContainerGet(container, name, returnStyle): 

"""Get a value of unknown type as a scalar or array 

 

Parameters 

---------- 

container : ``lsst.daf.base.PropertySet`` or ``lsst.daf.base.PropertyList`` 

Container from which to get the value 

name : ``str`` 

Name of item 

returnStyle : ``ReturnStyle`` 

Control whether numeric or string data is returned as an array 

or scalar (the other types, ``PropertyList``, ``PropertySet`` 

and ``PersistablePtr``, are always returned as a scalar): 

- ReturnStyle.ARRAY: return numeric or string data types 

as an array of values. 

- ReturnStyle.SCALAR: return numeric or string data types 

as a single value; if the item has multiple values then 

return the last value. 

- ReturnStyle.AUTO: (deprecated) return numeric or string data 

as a scalar if there is just one item, or as an array 

otherwise. 

""" 

if not container.exists(name): 

raise lsst.pex.exceptions.NotFoundError(name + " not found") 

79 ↛ 80line 79 didn't jump to line 80, because the condition on line 79 was never true if returnStyle not in ReturnStyle: 

raise ValueError("returnStyle {} must be a ReturnStyle".format(returnStyle)) 

 

elemType = _propertyContainerElementTypeName(container, name) 

if elemType: 

value = getattr(container, "getArray" + elemType)(name) 

if returnStyle == ReturnStyle.ARRAY or (returnStyle == ReturnStyle.AUTO and len(value) > 1): 

return value 

return value[-1] 

 

try: 

return container.getAsPropertyListPtr(name) 

except Exception: 

pass 

93 ↛ 95line 93 didn't jump to line 95, because the condition on line 93 was never false if container.typeOf(name) == container.TYPE_PropertySet: 

return container.getAsPropertySetPtr(name) 

try: 

return container.getAsPersistablePtr(name) 

except Exception: 

pass 

raise lsst.pex.exceptions.TypeError('Unknown PropertySet value type for ' + name) 

 

 

def _guessIntegerType(container, name, value): 

"""Given an existing container and name, determine the type 

that should be used for the supplied value. The supplied value 

is assumed to be a scalar. 

 

On Python 3 all ints are LongLong but we need to be able to store them 

in Int containers if that is what is being used (testing for truncation). 

Int is assumed to mean 32bit integer (2147483647 to -2147483648). 

 

If there is no pre-existing value we have to decide what to do. For now 

we pick Int if the value is less than maxsize. 

 

Returns None if the value supplied is a bool or not an integral value. 

""" 

useType = None 

maxInt = 2147483647 

minInt = -2147483648 

 

# We do not want to convert bool to int so let the system work that 

# out itself 

if isinstance(value, bool): 

return useType 

 

if isinstance(value, numbers.Integral): 

try: 

containerType = _propertyContainerElementTypeName(container, name) 

except lsst.pex.exceptions.NotFoundError: 

# nothing in the container so choose based on size. Safe option is to 

# always use LongLong 

131 ↛ 134line 131 didn't jump to line 134, because the condition on line 131 was never false if value <= maxInt and value >= minInt: 

useType = "Int" 

else: 

useType = "LongLong" 

else: 

136 ↛ 142line 136 didn't jump to line 142, because the condition on line 136 was never false if containerType == "Int": 

# Always use an Int even if we know it won't fit. The later 

# code will trigger OverflowError if appropriate. Setting the 

# type to LongLong here will trigger a TypeError instead so it's 

# best to trigger a predictable OverflowError. 

useType = "Int" 

elif containerType == "LongLong": 

useType = "LongLong" 

return useType 

 

 

def _propertyContainerSet(container, name, value, typeMenu, *args): 

"""Set a single Python value of unknown type""" 

if hasattr(value, "__iter__") and not isinstance(value, str): 

exemplar = value[0] 

else: 

exemplar = value 

 

t = type(exemplar) 

setType = _guessIntegerType(container, name, exemplar) 

 

if setType is not None or t in typeMenu: 

if setType is None: 

setType = typeMenu[t] 

return getattr(container, "set" + setType)(name, value, *args) 

# Allow for subclasses 

162 ↛ 165line 162 didn't jump to line 165, because the loop on line 162 didn't complete for checkType in typeMenu: 

if isinstance(exemplar, checkType): 

return getattr(container, "set" + typeMenu[checkType])(name, value, *args) 

raise lsst.pex.exceptions.TypeError("Unknown value type for %s: %s" % (name, t)) 

 

 

def _propertyContainerAdd(container, name, value, typeMenu, *args): 

"""Add a single Python value of unknown type""" 

if hasattr(value, "__iter__"): 

exemplar = value[0] 

else: 

exemplar = value 

 

t = type(exemplar) 

addType = _guessIntegerType(container, name, exemplar) 

 

if addType is not None or t in typeMenu: 

if addType is None: 

addType = typeMenu[t] 

return getattr(container, "add" + addType)(name, value, *args) 

# Allow for subclasses 

183 ↛ 186line 183 didn't jump to line 186, because the loop on line 183 didn't complete for checkType in typeMenu: 

if isinstance(exemplar, checkType): 

return getattr(container, "add" + typeMenu[checkType])(name, value, *args) 

raise lsst.pex.exceptions.TypeError("Unknown value type for %s: %s" % (name, t)) 

 

 

def getstate(self): 

190 ↛ 196line 190 didn't jump to line 196, because the condition on line 190 was never false if isinstance(self, PropertyList): 

return [(name, _propertyContainerElementTypeName(self, name), 

_propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO), 

self.getComment(name)) 

for name in self.getOrderedNames()] 

else: 

return [(name, _propertyContainerElementTypeName(self, name), 

_propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO)) 

for name in self.paramNames(False)] 

 

 

def setstate(self, state): 

202 ↛ 206line 202 didn't jump to line 206, because the condition on line 202 was never false if isinstance(self, PropertyList): 

for name, elemType, value, comment in state: 

getattr(self, "set" + elemType)(name, value, comment) 

else: 

for name, elemType, value in state: 

getattr(self, "set" + elemType)(name, value) 

 

 

@continueClass 

class PropertySet: 

# Mapping of type to method names; 

# int types are omitted due to use of _guessIntegerType 

_typeMenu = {bool: "Bool", 

float: "Double", 

str: "String", 

DateTime: "DateTime", 

PropertySet: "PropertySet", 

PropertyList: "PropertySet", 

} 

 

# Map unicode to String, but this only works on Python 2 

# so catch the error and do nothing on Python 3. 

try: 

_typeMenu[unicode] = "String" # noqa F821 

except Exception: 

pass 

 

def get(self, name): 

"""Return an item as a scalar or array 

 

Return an array if the item is of numeric or string type and has 

more than one value, otherwise return a scalar. 

 

.. deprecated:: 20180-06 

`get` is superseded by `getArray` or `getScalar` 

 

Parameters 

---------- 

name : ``str`` 

Name of item 

 

Raises 

------ 

lsst.pex.exceptions.NotFoundError 

If the item does not exist. 

""" 

warnings.warn("Use getArray or getScalar instead", DeprecationWarning, stacklevel=2) 

return _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO) 

 

def getArray(self, name): 

"""Return an item as an array if the item is numeric or string 

 

If the item is a ``PropertySet``, ``PropertyList`` or 

``lsst.daf.base.PersistablePtr`` then return the item as a scalar. 

 

Parameters 

---------- 

name : ``str`` 

Name of item 

 

Raises 

------ 

lsst.pex.exceptions.NotFoundError 

If the item does not exist. 

""" 

return _propertyContainerGet(self, name, returnStyle=ReturnStyle.ARRAY) 

 

def getScalar(self, name): 

"""Return an item as a scalar 

 

If the item has more than one value then the last value is returned 

 

Parameters 

---------- 

name : ``str`` 

Name of item 

 

Raises 

------ 

lsst.pex.exceptions.NotFoundError 

If the item does not exist. 

""" 

return _propertyContainerGet(self, name, returnStyle=ReturnStyle.SCALAR) 

 

def set(self, name, value): 

"""Set the value of an item 

 

If the item already exists it is silently replaced; the types 

need not match. 

 

Parameters 

---------- 

name : ``str`` 

Name of item 

value : any supported type 

Value of item; may be a scalar or array 

""" 

return _propertyContainerSet(self, name, value, self._typeMenu) 

 

def add(self, name, value): 

"""Append one or more values to a given item, which need not exist 

 

If the item exists then the new value(s) are appended; 

otherwise it is like calling `set` 

 

Parameters 

---------- 

name : ``str`` 

Name of item 

value : any supported type 

Value of item; may be a scalar or array 

 

Notes 

----- 

If `value` is an ``lsst.daf.base.PropertySet`` or 

``lsst.daf.base.PropertyList`` then `value` replaces 

the existing value. Also the item is added as a live 

reference, so updating `value` will update this container 

and vice-versa. 

 

Raises 

------ 

lsst::pex::exceptions::TypeError 

If the type of `value` is incompatible with the existing value 

of the item. 

""" 

return _propertyContainerAdd(self, name, value, self._typeMenu) 

 

def toDict(self): 

"""Returns a (possibly nested) dictionary with all properties. 

""" 

 

d = {} 

for name in self.names(): 

v = _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO) 

 

if isinstance(v, PropertySet): 

d[name] = PropertySet.toDict(v) 

else: 

d[name] = v 

return d 

 

 

@continueClass 

class PropertyList: 

# Mapping of type to method names 

_typeMenu = {bool: "Bool", 

int: "Int", 

float: "Double", 

str: "String", 

DateTime: "DateTime", 

PropertySet: "PropertySet", 

PropertyList: "PropertySet", 

} 

 

# Map unicode to String, but this only works on Python 2 

# so catch the error and do nothing on Python 3. 

try: 

_typeMenu[unicode] = "String" # noqa F821 

except Exception: 

pass 

 

def get(self, name): 

"""Return an item as a scalar or array 

 

Return an array if the item has more than one value, 

otherwise return a scalar. 

 

.. deprecated:: 20180-06 

`get` is superseded by `getArray` or `getScalar` 

 

Parameters 

---------- 

name : ``str`` 

Name of item 

 

Raises 

------ 

lsst.pex.exceptions.NotFoundError 

If the item does not exist. 

""" 

warnings.warn("Use getArray or getScalar instead", DeprecationWarning, stacklevel=2) 

return _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO) 

 

def getArray(self, name): 

"""Return an item as an array 

 

Parameters 

---------- 

name : ``str`` 

Name of item 

 

Raises 

------ 

lsst.pex.exceptions.NotFoundError 

If the item does not exist. 

""" 

return _propertyContainerGet(self, name, returnStyle=ReturnStyle.ARRAY) 

 

def getScalar(self, name): 

"""Return an item as a scalar 

 

If the item has more than one value then the last value is returned 

 

Parameters 

---------- 

name : ``str`` 

Name of item 

 

Raises 

------ 

lsst.pex.exceptions.NotFoundError 

If the item does not exist. 

""" 

return _propertyContainerGet(self, name, returnStyle=ReturnStyle.SCALAR) 

 

def set(self, name, value, comment=None): 

"""Set the value of an item 

 

If the item already exists it is silently replaced; the types 

need not match. 

 

Parameters 

---------- 

name : ``str`` 

Name of item 

value : any supported type 

Value of item; may be a scalar or array 

""" 

args = [] 

if comment is not None: 

args.append(comment) 

return _propertyContainerSet(self, name, value, self._typeMenu, *args) 

 

def add(self, name, value, comment=None): 

"""Append one or more values to a given item, which need not exist 

 

If the item exists then the new value(s) are appended; 

otherwise it is like calling `set` 

 

Parameters 

---------- 

name : ``str`` 

Name of item 

value : any supported type 

Value of item; may be a scalar or array 

 

Notes 

----- 

If `value` is an ``lsst.daf.base.PropertySet`` items are added 

using dotted names (e.g. if name="a" and value contains 

an item "b" which is another PropertySet and contains an 

item "c" which is numeric or string, then the value of "c" 

is added as "a.b.c", appended to the existing values of 

"a.b.c" if any (in which case the types must be compatible). 

 

Raises 

------ 

lsst::pex::exceptions::TypeError 

If the type of `value` is incompatible with the existing value 

of the item. 

""" 

args = [] 

if comment is not None: 

args.append(comment) 

return _propertyContainerAdd(self, name, value, self._typeMenu, *args) 

 

def toList(self): 

orderedNames = self.getOrderedNames() 

ret = [] 

for name in orderedNames: 

if self.isArray(name): 

values = _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO) 

for v in values: 

ret.append((name, v, self.getComment(name))) 

else: 

ret.append((name, _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO), 

self.getComment(name))) 

return ret 

 

def toOrderedDict(self): 

"""Return an ordered dictionary with all properties in the order that 

they were inserted. 

""" 

from collections import OrderedDict 

 

d = OrderedDict() 

for name in self.getOrderedNames(): 

d[name] = _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO) 

return d