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

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010, 2011 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/>. 

# 

 

__all__ = ["Struct"] 

 

 

class Struct: 

"""A container to which you can add fields as attributes. 

 

Parameters 

---------- 

keyArgs 

keyword arguments specifying fields and their values. 

 

Notes 

----- 

Intended to be used for the return value from `~lsst.pipe.base.Task.run` and other `~lsst.pipe.base.Task` 

methods, and useful for any method that returns multiple values. 

 

The intent is to allow accessing returned items by name, instead of unpacking a tuple. 

This makes the code much more robust and easier to read. It allows one to change what values are returned 

without inducing mysterious failures: adding items is completely safe, and removing or renaming items 

causes errors that are caught quickly and reported in a way that is easy to understand. 

 

The primary reason for using Struct instead of dict is that the fields may be accessed as attributes, 

e.g. ``aStruct.foo`` instead of ``aDict["foo"]``. Admittedly this only saves a few characters, but it 

makes the code significantly more readable. 

 

Struct is preferred over named tuples, because named tuples can be used as ordinary tuples, thus losing 

all the safety advantages of Struct. In addition, named tuples are clumsy to define and Structs 

are much more mutable (e.g. one can trivially combine Structs and add additional fields). 

 

Examples 

-------- 

>>> myStruct = Struct( 

>>> strVal = 'the value of the field named "strVal"', 

>>> intVal = 35, 

>>> ) 

 

""" 

 

def __init__(self, **keyArgs): 

for name, val in keyArgs.items(): 

self.__safeAdd(name, val) 

 

def __safeAdd(self, name, val): 

"""Add a field if it does not already exist and name does not start with ``__`` (two underscores). 

 

Parameters 

---------- 

name : `str` 

Name of field to add. 

val : object 

Value of field to add. 

 

Raises 

------ 

RuntimeError 

Raised if name already exists or starts with ``__`` (two underscores). 

""" 

if hasattr(self, name): 

raise RuntimeError("Item %s already exists" % (name,)) 

if name.startswith("__"): 

raise RuntimeError("Item name %r invalid; must not begin with __" % (name,)) 

setattr(self, name, val) 

 

def getDict(self): 

"""Get a dictionary of fields in this struct. 

 

Returns 

------- 

structDict : `dict` 

Dictionary with field names as keys and field values as values. The values are shallow copies. 

""" 

return self.__dict__.copy() 

 

def mergeItems(self, struct, *nameList): 

"""Copy specified fields from another struct, provided they don't already exist. 

 

Parameters 

---------- 

struct : `Struct` 

`Struct` from which to copy. 

*nameList : `str` 

All remaining arguments are names of items to copy. 

 

Raises 

------ 

RuntimeError 

Raised if any item in nameList already exists in self (but any items before the conflicting item 

in nameList will have been copied). 

 

Examples 

-------- 

For example:: 

 

foo.copyItems(other, "itemName1", "itemName2") 

 

copies ``other.itemName1`` and ``other.itemName2`` into self. 

""" 

for name in nameList: 

self.__safeAdd(name, getattr(struct, name)) 

 

def copy(self): 

"""Make a one-level-deep copy (values are not copied). 

 

Returns 

------- 

copy : `Struct` 

One-level-deep copy of this Struct. 

""" 

return Struct(**self.getDict()) 

 

def __eq__(self, other): 

return self.__dict__ == other.__dict__ 

 

def __len__(self): 

return len(self.__dict__) 

 

def __repr__(self): 

itemList = ["%s=%r" % (name, val) for name, val in self.getDict().items()] 

return "%s(%s)" % (self.__class__.__name__, "; ".join(itemList))