lsst.pipe.base  13.0-12-gaf0c0ec+8
Public Member Functions | 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__ (self, keyArgs)
 Create a Struct with the specified field names and values. More...
 
def getDict (self)
 Return a dictionary of attribute name: value. More...
 
def mergeItems (self, struct, nameList)
 Copy specified fields from another struct, provided they don't already exist. More...
 
def copy (self)
 Return a one-level-deep copy (values are not copied) More...
 
def __eq__ (self, other)
 
def __len__ (self)
 
def __repr__ (self)
 

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 29 of file struct.py.

Constructor & Destructor Documentation

◆ __init__()

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

Create a Struct with the specified field names and values.

For example:

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

Definition at line 49 of file struct.py.

Member Function Documentation

◆ __eq__()

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

Definition at line 107 of file struct.py.

◆ __len__()

def lsst.pipe.base.struct.Struct.__len__ (   self)

Definition at line 110 of file struct.py.

◆ __repr__()

def lsst.pipe.base.struct.Struct.__repr__ (   self)

Definition at line 113 of file struct.py.

◆ copy()

def lsst.pipe.base.struct.Struct.copy (   self)

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

Definition at line 102 of file struct.py.

◆ getDict()

def lsst.pipe.base.struct.Struct.getDict (   self)

Return a dictionary of attribute name: value.

Warning
: the values are shallow copies.

Definition at line 80 of file struct.py.

◆ mergeItems()

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 87 of file struct.py.


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