LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Private Member Functions | Private Attributes | List of all members
lsst.pipe.base.struct.Struct Class Reference

A struct to which you can add any fields. More...

Inheritance diagram for lsst.pipe.base.struct.Struct:

Public Member Functions

def __init__
 Create a Struct with the specified field names and values. More...
 
def getDict
 Return a dictionary of attribute name: value. More...
 
def mergeItems
 Copy specified fields from another struct, provided they don't already exist. More...
 
def copy
 Return a one-level-deep copy (values are not copied) More...
 
def __eq__
 
def __len__
 
def __repr__
 

Private Member Functions

def __safeAdd
 Add a field if it does not already exist and name does not start with __ (two underscores) More...
 

Private Attributes

 __dict__
 

Detailed Description

A struct to which you can add any fields.

Intended to be used for the return value from Task.run and other 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).

Definition at line 25 of file struct.py.

Constructor & Destructor Documentation

def lsst.pipe.base.struct.Struct.__init__ (   self,
  keyArgs 
)

Create a Struct with the specified field names and values.

For example:

1 myStruct = Struct(
2  strVal = 'the value of the field named "strVal"',
3  intVal = 35,
4 )
Parameters
[in]**keyArgskeyword arguments specifying name=value pairs

Definition at line 44 of file struct.py.

44 
45  def __init__(self, **keyArgs):
46  """!Create a Struct with the specified field names and values
47 
48  For example:
49  @code
50  myStruct = Struct(
51  strVal = 'the value of the field named "strVal"',
52  intVal = 35,
53  )
54  @endcode
55 
56  @param[in] **keyArgs keyword arguments specifying name=value pairs
57  """
58  object.__init__(self)
59  for name, val in keyArgs.iteritems():
60  self.__safeAdd(name, val)
def __init__
Create a Struct with the specified field names and values.
Definition: struct.py:44
def __safeAdd
Add a field if it does not already exist and name does not start with __ (two underscores) ...
Definition: struct.py:61

Member Function Documentation

def lsst.pipe.base.struct.Struct.__eq__ (   self,
  other 
)

Definition at line 102 of file struct.py.

103  def __eq__(self, other):
104  return self.__dict__ == other.__dict__
def lsst.pipe.base.struct.Struct.__len__ (   self)

Definition at line 105 of file struct.py.

106  def __len__(self):
107  return len(self.__dict__)
def lsst.pipe.base.struct.Struct.__repr__ (   self)

Definition at line 108 of file struct.py.

109  def __repr__(self):
110  itemList = ["%s=%r" % (name, val) for name, val in self.getDict().iteritems()]
111  return "%s(%s)" % (self.__class__.__name__, "; ".join(itemList))
def getDict
Return a dictionary of attribute name: value.
Definition: struct.py:75
def lsst.pipe.base.struct.Struct.__safeAdd (   self,
  name,
  val 
)
private

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

Parameters
[in]namename of field to add
[in]valvalue of field to add
Exceptions
RuntimeErrorif name already exists or starts with __ (two underscores)

Definition at line 61 of file struct.py.

61 
62  def __safeAdd(self, name, val):
63  """!Add a field if it does not already exist and name does not start with __ (two underscores)
64 
65  @param[in] name name of field to add
66  @param[in] val value of field to add
67 
68  @throw RuntimeError if name already exists or starts with __ (two underscores)
69  """
70  if hasattr(self, name):
71  raise RuntimeError("Item %s already exists" % (name,))
72  if name.startswith("__"):
73  raise RuntimeError("Item name %r invalid; must not begin with __" % (name,))
74  setattr(self, name, val)
def __safeAdd
Add a field if it does not already exist and name does not start with __ (two underscores) ...
Definition: struct.py:61
def lsst.pipe.base.struct.Struct.copy (   self)

Return a one-level-deep copy (values are not copied)

Definition at line 97 of file struct.py.

97 
98  def copy(self):
99  """!Return a one-level-deep copy (values are not copied)
100  """
101  return Struct(**self.getDict())
def copy
Return a one-level-deep copy (values are not copied)
Definition: struct.py:97
A struct to which you can add any fields.
Definition: struct.py:25
def getDict
Return a dictionary of attribute name: value.
Definition: struct.py:75
def lsst.pipe.base.struct.Struct.getDict (   self)

Return a dictionary of attribute name: value.

Warning
: the values are shallow copies.

Definition at line 75 of file struct.py.

75 
76  def getDict(self):
77  """!Return a dictionary of attribute name: value
78 
79  @warning: the values are shallow copies.
80  """
81  return self.__dict__.copy()
def getDict
Return a dictionary of attribute name: value.
Definition: struct.py:75
def lsst.pipe.base.struct.Struct.mergeItems (   self,
  struct,
  nameList 
)

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

Parameters
[in]structstruct from which to copy
[in]*nameListall remaining arguments are names of items to copy

For example: foo.copyItems(other, "itemName1", "itemName2") copies other.itemName1 and other.itemName2 into self.

Exceptions
RuntimeErrorif any item in nameList already exists in self (but any items before the conflicting item in nameList will have been copied)

Definition at line 82 of file struct.py.

82 
83  def mergeItems(self, struct, *nameList):
84  """!Copy specified fields from another struct, provided they don't already exist
85 
86  @param[in] struct struct from which to copy
87  @param[in] *nameList all remaining arguments are names of items to copy
88 
89  For example: foo.copyItems(other, "itemName1", "itemName2")
90  copies other.itemName1 and other.itemName2 into self.
91 
92  @throw RuntimeError if any item in nameList already exists in self
93  (but any items before the conflicting item in nameList will have been copied)
94  """
95  for name in nameList:
96  self.__safeAdd(name, getattr(struct, name))
def __safeAdd
Add a field if it does not already exist and name does not start with __ (two underscores) ...
Definition: struct.py:61
def mergeItems
Copy specified fields from another struct, provided they don't already exist.
Definition: struct.py:82

Member Data Documentation

lsst.pipe.base.struct.Struct.__dict__
private

Definition at line 103 of file struct.py.


The documentation for this class was generated from the following file: