27 """A container to which you can add fields as attributes. 32 keyword arguments specifying fields and their values. 36 Intended to be used for the return value from `~lsst.pipe.base.Task.run` and other `~lsst.pipe.base.Task` 37 methods, and useful for any method that returns multiple values. 39 The intent is to allow accessing returned items by name, instead of unpacking a tuple. 40 This makes the code much more robust and easier to read. It allows one to change what values are returned 41 without inducing mysterious failures: adding items is completely safe, and removing or renaming items 42 causes errors that are caught quickly and reported in a way that is easy to understand. 44 The primary reason for using Struct instead of dict is that the fields may be accessed as attributes, 45 e.g. ``aStruct.foo`` instead of ``aDict["foo"]``. Admittedly this only saves a few characters, but it 46 makes the code significantly more readable. 48 Struct is preferred over named tuples, because named tuples can be used as ordinary tuples, thus losing 49 all the safety advantages of Struct. In addition, named tuples are clumsy to define and Structs 50 are much more mutable (e.g. one can trivially combine Structs and add additional fields). 54 >>> myStruct = Struct( 55 >>> strVal = 'the value of the field named "strVal"', 62 for name, val
in keyArgs.items():
65 def __safeAdd(self, name, val):
66 """Add a field if it does not already exist and name does not start with ``__`` (two underscores). 73 Value of field to add. 78 Raised if name already exists or starts with ``__`` (two underscores). 80 if hasattr(self, name):
81 raise RuntimeError(
"Item %s already exists" % (name,))
82 if name.startswith(
"__"):
83 raise RuntimeError(
"Item name %r invalid; must not begin with __" % (name,))
84 setattr(self, name, val)
87 """Get a dictionary of fields in this struct. 92 Dictionary with field names as keys and field values as values. The values are shallow copies. 97 """Copy specified fields from another struct, provided they don't already exist. 102 `Struct` from which to copy. 104 All remaining arguments are names of items to copy. 109 Raised if any item in nameList already exists in self (but any items before the conflicting item 110 in nameList will have been copied). 116 foo.copyItems(other, "itemName1", "itemName2") 118 copies ``other.itemName1`` and ``other.itemName2`` into self. 120 for name
in nameList:
121 self.
__safeAdd(name, getattr(struct, name))
124 """Make a one-level-deep copy (values are not copied). 129 One-level-deep copy of this Struct. 134 return self.
__dict__ == other.__dict__
140 itemList = [
"%s=%r" % (name, val)
for name, val
in self.
getDict().
items()]
141 return "%s(%s)" % (self.__class__.__name__,
"; ".join(itemList))
def mergeItems(self, struct, nameList)
def __init__(self, keyArgs)
std::vector< SchemaItem< Flag > > * items
def __safeAdd(self, name, val)