LSST Applications g02d81e74bb+86cf3d8bc9,g180d380827+7a4e862ed4,g2079a07aa2+86d27d4dc4,g2305ad1205+e1ca1c66fa,g29320951ab+012e1474a1,g295015adf3+341ea1ce94,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g33d1c0ed96+0e5473021a,g3a166c0a6a+0e5473021a,g3ddfee87b4+c429d67c83,g48712c4677+f88676dd22,g487adcacf7+27e1e21933,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+b41db86c35,g5a732f18d5+53520f316c,g64a986408d+86cf3d8bc9,g858d7b2824+86cf3d8bc9,g8a8a8dda67+585e252eca,g99cad8db69+84912a7fdc,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+a2b54eae19,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+6681f309db,gc120e1dc64+f0fcc2f6d8,gc28159a63d+0e5473021a,gcf0d15dbbd+c429d67c83,gdaeeff99f8+f9a426f77a,ge6526c86ff+0433e6603d,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gff1a9f87cc+86cf3d8bc9,w.2024.17
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | Properties | List of all members
lsst.pex.config.dictField.Dict Class Reference
Inheritance diagram for lsst.pex.config.dictField.Dict:
lsst.pex.config.configDictField.ConfigDict lsst.pex.config.dictField.DictField lsst.pex.config.configDictField.ConfigDictField

Public Member Functions

 __init__ (self, config, field, value, at, label, setHistory=True)
 
ItemTypeVar __getitem__ (self, KeyTypeVar k)
 
int __len__ (self)
 
Iterator[KeyTypeVar__iter__ (self)
 
bool __contains__ (self, Any k)
 
None __setitem__ (self, KeyTypeVar k, ItemTypeVar x, Any at=None, str label="setitem", bool setHistory=True)
 
None __delitem__ (self, KeyTypeVar k, Any at=None, str label="delitem", bool setHistory=True)
 
 __repr__ (self)
 
 __str__ (self)
 
 __setattr__ (self, attr, value, at=None, label="assignment")
 
 __reduce__ (self)
 

Protected Member Functions

Config _config (self)
 

Protected Attributes

 _field
 
 _config_
 
 _dict
 
 _history
 
 _config
 

Properties

 history = property(lambda x: x._history)
 

Detailed Description

An internal mapping container.

This class emulates a `dict`, but adds validation and provenance.

Parameters
----------
config : `~lsst.pex.config.Config`
    Config to proxy.
field : `~lsst.pex.config.DictField`
    Field to use.
value : `~typing.Any`
    Value to store.
at : `list` of `~lsst.pex.config.callStack.StackFrame`
    Stack frame for history recording. Will be calculated if `None`.
label : `str`, optional
    Label to use for history recording.
setHistory : `bool`, optional
    Whether to append to the history record.

Definition at line 53 of file dictField.py.

Constructor & Destructor Documentation

◆ __init__()

lsst.pex.config.dictField.Dict.__init__ ( self,
config,
field,
value,
at,
label,
setHistory = True )

Reimplemented in lsst.pex.config.configDictField.ConfigDict, lsst.pex.config.configDictField.ConfigDictField, and lsst.pex.config.dictField.DictField.

Definition at line 74 of file dictField.py.

74 def __init__(self, config, field, value, at, label, setHistory=True):
75 self._field = field
76 self._config_ = weakref.ref(config)
77 self._dict = {}
78 self._history = self._config._history.setdefault(self._field.name, [])
79 self.__doc__ = field.doc
80 if value is not None:
81 try:
82 for k in value:
83 # do not set history per-item
84 self.__setitem__(k, value[k], at=at, label=label, setHistory=False)
85 except TypeError:
86 msg = f"Value {value} is of incorrect type {_typeStr(value)}. Mapping type expected."
87 raise FieldValidationError(self._field, self._config, msg)
88 if setHistory:
89 self._history.append((dict(self._dict), at, label))
90

Member Function Documentation

◆ __contains__()

bool lsst.pex.config.dictField.Dict.__contains__ ( self,
Any k )

Definition at line 112 of file dictField.py.

112 def __contains__(self, k: Any) -> bool:
113 return k in self._dict
114

◆ __delitem__()

None lsst.pex.config.dictField.Dict.__delitem__ ( self,
KeyTypeVar k,
Any at = None,
str label = "delitem",
bool setHistory = True )

Reimplemented in lsst.pex.config.configDictField.ConfigDict.

Definition at line 156 of file dictField.py.

158 ) -> None:
159 if self._config._frozen:
160 raise FieldValidationError(self._field, self._config, "Cannot modify a frozen Config")
161
162 del self._dict[k]
163 if setHistory:
164 if at is None:
165 at = getCallStack()
166 self._history.append((dict(self._dict), at, label))
167

◆ __getitem__()

ItemTypeVar lsst.pex.config.dictField.Dict.__getitem__ ( self,
KeyTypeVar k )

Definition at line 103 of file dictField.py.

103 def __getitem__(self, k: KeyTypeVar) -> ItemTypeVar:
104 return self._dict[k]
105

◆ __iter__()

Iterator[KeyTypeVar] lsst.pex.config.dictField.Dict.__iter__ ( self)

Definition at line 109 of file dictField.py.

109 def __iter__(self) -> Iterator[KeyTypeVar]:
110 return iter(self._dict)
111

◆ __len__()

int lsst.pex.config.dictField.Dict.__len__ ( self)

Definition at line 106 of file dictField.py.

106 def __len__(self) -> int:
107 return len(self._dict)
108

◆ __reduce__()

lsst.pex.config.dictField.Dict.__reduce__ ( self)

Definition at line 186 of file dictField.py.

186 def __reduce__(self):
187 raise UnexpectedProxyUsageError(
188 f"Proxy container for config field {self._field.name} cannot "
189 "be pickled; it should be converted to a built-in container before "
190 "being assigned to other objects or variables."
191 )
192
193

◆ __repr__()

lsst.pex.config.dictField.Dict.__repr__ ( self)

Definition at line 168 of file dictField.py.

168 def __repr__(self):
169 return repr(self._dict)
170

◆ __setattr__()

lsst.pex.config.dictField.Dict.__setattr__ ( self,
attr,
value,
at = None,
label = "assignment" )

Definition at line 174 of file dictField.py.

174 def __setattr__(self, attr, value, at=None, label="assignment"):
175 if hasattr(getattr(self.__class__, attr, None), "__set__"):
176 # This allows properties to work.
177 object.__setattr__(self, attr, value)
178 elif attr in self.__dict__ or attr in ["_field", "_config_", "_history", "_dict", "__doc__"]:
179 # This allows specific private attributes to work.
180 object.__setattr__(self, attr, value)
181 else:
182 # We throw everything else.
183 msg = f"{_typeStr(self._field)} has no attribute {attr}"
184 raise FieldValidationError(self._field, self._config, msg)
185

◆ __setitem__()

None lsst.pex.config.dictField.Dict.__setitem__ ( self,
KeyTypeVar k,
ItemTypeVar x,
Any at = None,
str label = "setitem",
bool setHistory = True )

Reimplemented in lsst.pex.config.configDictField.ConfigDict.

Definition at line 115 of file dictField.py.

117 ) -> None:
118 if self._config._frozen:
119 msg = f"Cannot modify a frozen Config. Attempting to set item at key {k!r} to value {x}"
120 raise FieldValidationError(self._field, self._config, msg)
121
122 # validate keytype
123 k = _autocast(k, self._field.keytype)
124 if type(k) is not self._field.keytype:
125 msg = f"Key {k!r} is of type {_typeStr(k)}, expected type {_typeStr(self._field.keytype)}"
126 raise FieldValidationError(self._field, self._config, msg)
127
128 # validate itemtype
129 x = _autocast(x, self._field.itemtype)
130 if self._field.itemtype is None:
131 if type(x) not in self._field.supportedTypes and x is not None:
132 msg = f"Value {x} at key {k!r} is of invalid type {_typeStr(x)}"
133 raise FieldValidationError(self._field, self._config, msg)
134 else:
135 if type(x) is not self._field.itemtype and x is not None:
136 msg = "Value {} at key {!r} is of incorrect type {}. Expected type {}".format(
137 x,
138 k,
139 _typeStr(x),
140 _typeStr(self._field.itemtype),
141 )
142 raise FieldValidationError(self._field, self._config, msg)
143
144 # validate item using itemcheck
145 if self._field.itemCheck is not None and not self._field.itemCheck(x):
146 msg = f"Item at key {k!r} is not a valid value: {x}"
147 raise FieldValidationError(self._field, self._config, msg)
148
149 if at is None:
150 at = getCallStack()
151
152 self._dict[k] = x
153 if setHistory:
154 self._history.append((dict(self._dict), at, label))
155

◆ __str__()

lsst.pex.config.dictField.Dict.__str__ ( self)

Definition at line 171 of file dictField.py.

171 def __str__(self):
172 return str(self._dict)
173

◆ _config()

Config lsst.pex.config.dictField.Dict._config ( self)
protected

Definition at line 92 of file dictField.py.

92 def _config(self) -> Config:
93 # Config Fields should never outlive their config class instance
94 # assert that as such here
95 value = self._config_()
96 assert value is not None
97 return value
98

Member Data Documentation

◆ _config

lsst.pex.config.dictField.Dict._config
protected

Definition at line 87 of file dictField.py.

◆ _config_

lsst.pex.config.dictField.Dict._config_
protected

Definition at line 76 of file dictField.py.

◆ _dict

lsst.pex.config.dictField.Dict._dict
protected

Definition at line 77 of file dictField.py.

◆ _field

lsst.pex.config.dictField.Dict._field
protected

Definition at line 75 of file dictField.py.

◆ _history

lsst.pex.config.dictField.Dict._history
protected

Definition at line 78 of file dictField.py.

Property Documentation

◆ history

lsst.pex.config.dictField.Dict.history = property(lambda x: x._history)
static

Definition at line 99 of file dictField.py.


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