23 from .config
import Config, FieldValidationError, _autocast, _typeStr, _joinNamePath
24 from .dictField
import Dict, DictField
25 from .comparison
import compareConfigs, compareScalars, getComparisonName
26 from .callStack
import getCallStack, getStackFrame
28 __all__ = [
"ConfigDictField"]
33 Config-Insternal representation of a dict of config classes 35 Much like Dict, ConfigDict is a custom MutableMapper which tracks the 36 history of changes to any of its items. 38 def __init__(self, config, field, value, at, label):
39 Dict.__init__(self, config, field, value, at, label, setHistory=
False)
40 self.
history.append((
"Dict initialized", at, label))
42 def __setitem__(self, k, x, at=None, label="setitem", setHistory=True):
44 msg =
"Cannot modify a frozen Config. "\
45 "Attempting to set item at key %r to value %s" % (k, x)
49 k = _autocast(k, self.
_field.keytype)
50 if type(k) != self.
_field.keytype:
51 msg =
"Key %r is of type %s, expected type %s" % \
52 (k, _typeStr(k), _typeStr(self.
_field.keytype))
56 dtype = self.
_field.itemtype
57 if type(x) != self.
_field.itemtype
and x != self.
_field.itemtype:
58 msg =
"Value %s at key %r is of incorrect type %s. Expected type %s" % \
59 (x, k, _typeStr(x), _typeStr(self.
_field.itemtype))
65 oldValue = self.
_dict.get(k,
None)
68 self.
_dict[k] = dtype(__name=name, __at=at, __label=label)
70 self.
_dict[k] = dtype(__name=name, __at=at, __label=label, **x._storage)
72 self.
history.append((
"Added item at key %s" % k, at, label))
76 oldValue.update(__at=at, __label=label, **x._storage)
78 self.
history.append((
"Modified item at key %s" % k, at, label))
83 Dict.__delitem__(self, k, at, label,
False)
84 self.
history.append((
"Removed item at key %s" % k, at, label))
89 Defines a field which is a mapping between a POD and a config class. 91 This behaves exactly like a DictField with the slight difference that 92 itemtype must be an subclass of Config. 94 This allows config writters to create name-to-config mappings. One use case 95 is for configuring mappings for dataset types in a butler. In this case, 96 the dataset type names are arbitrary and user-selected; the mapping 97 configurations are known and fixed. 100 DictClass = ConfigDict
102 def __init__(self, doc, keytype, itemtype, default=None, optional=False, dictCheck=None, itemCheck=None):
104 self.
_setup(doc=doc, dtype=ConfigDict, default=default, check=
None,
105 optional=optional, source=source)
107 raise ValueError(
"'keytype' %s is not a supported type" %
109 elif not issubclass(itemtype, Config):
110 raise ValueError(
"'itemtype' %s is not a supported type" %
112 if dictCheck
is not None and not hasattr(dictCheck,
"__call__"):
113 raise ValueError(
"'dictCheck' must be callable")
114 if itemCheck
is not None and not hasattr(itemCheck,
"__call__"):
115 raise ValueError(
"'itemCheck' must be callable")
123 configDict = self.
__get__(instance)
124 if configDict
is not None:
126 fullname = _joinNamePath(instance._name, self.name, k)
127 configDict[k]._rename(fullname)
131 if value
is not None:
136 msg =
"Item at key %r is not a valid value: %s" % (k, item)
138 DictField.validate(self, instance)
141 configDict = self.
__get__(instance)
142 if configDict
is None:
147 dict_[k] = configDict[k].
toDict()
151 def save(self, outfile, instance):
152 configDict = self.
__get__(instance)
153 fullname = _joinNamePath(instance._name, self.name)
154 if configDict
is None:
155 outfile.write(
u"{}={!r}\n".
format(fullname, configDict))
158 outfile.write(
u"{}={!r}\n".
format(fullname, {}))
159 for v
in configDict.values():
160 outfile.write(
u"{}={}()\n".
format(v._name, _typeStr(v)))
164 configDict = self.
__get__(instance)
165 if configDict
is not None:
169 def _compare(self, instance1, instance2, shortcut, rtol, atol, output):
170 """Helper function for Config.compare; used to compare two fields for equality. 172 @param[in] instance1 LHS Config instance to compare. 173 @param[in] instance2 RHS Config instance to compare. 174 @param[in] shortcut If True, return as soon as an inequality is found. 175 @param[in] rtol Relative tolerance for floating point comparisons. 176 @param[in] atol Absolute tolerance for floating point comparisons. 177 @param[in] output If not None, a callable that takes a string, used (possibly repeatedly) 178 to report inequalities. 180 Floating point comparisons are performed by numpy.allclose; refer to that for details. 182 d1 = getattr(instance1, self.name)
183 d2 = getattr(instance2, self.name)
185 _joinNamePath(instance1._name, self.name),
186 _joinNamePath(instance2._name, self.name)
188 if not compareScalars(
"keys for %s" % name, set(d1.keys()), set(d2.keys()), output=output):
191 for k, v1
in d1.items():
193 result =
compareConfigs(
"%s[%r]" % (name, k), v1, v2, shortcut=shortcut,
194 rtol=rtol, atol=atol, output=output)
195 if not result
and shortcut:
197 equal = equal
and result
def __init__(self, doc, keytype, itemtype, default=None, optional=False, dictCheck=None, itemCheck=None)
def __setitem__(self, k, x, at=None, label="setitem", setHistory=True)
def toDict(self, instance)
def save(self, outfile, instance)
def compareConfigs(name, c1, c2, shortcut=True, rtol=1E-8, atol=1E-8, output=None)
def validate(self, instance)
def freeze(self, instance)
def __get__(self, instance, owner=None, at=None, label="default")
def _setup(self, doc, dtype, default, check, optional, source)
def rename(self, instance)
def getStackFrame(relative=0)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
def __delitem__(self, k, at=None, label="delitem")
def compareScalars(name, v1, v2, output, rtol=1E-8, atol=1E-8, dtype=None)
def getComparisonName(name1, name2)
def __init__(self, config, field, value, at, label)