lsst.pex.config  16.0-5-gd0f1235+2
dictField.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 
23 __all__ = ["DictField"]
24 
25 import collections
26 
27 from .config import Field, FieldValidationError, _typeStr, _autocast, _joinNamePath
28 from .comparison import getComparisonName, compareScalars
29 from .callStack import getCallStack, getStackFrame
30 
31 
32 class Dict(collections.MutableMapping):
33  """
34  Config-Internal mapping container
35  Emulates a dict, but adds validation and provenance.
36  """
37 
38  def __init__(self, config, field, value, at, label, setHistory=True):
39  self._field = field
40  self._config = config
41  self._dict = {}
42  self._history = self._config._history.setdefault(self._field.name, [])
43  self.__doc__ = field.doc
44  if value is not None:
45  try:
46  for k in value:
47  # do not set history per-item
48  self.__setitem__(k, value[k], at=at, label=label, setHistory=False)
49  except TypeError:
50  msg = "Value %s is of incorrect type %s. Mapping type expected." % \
51  (value, _typeStr(value))
52  raise FieldValidationError(self._field, self._config, msg)
53  if setHistory:
54  self._history.append((dict(self._dict), at, label))
55 
56  """
57  Read-only history
58  """
59  history = property(lambda x: x._history)
60 
61  def __getitem__(self, k):
62  return self._dict[k]
63 
64  def __len__(self):
65  return len(self._dict)
66 
67  def __iter__(self):
68  return iter(self._dict)
69 
70  def __contains__(self, k):
71  return k in self._dict
72 
73  def __setitem__(self, k, x, at=None, label="setitem", setHistory=True):
74  if self._config._frozen:
75  msg = "Cannot modify a frozen Config. "\
76  "Attempting to set item at key %r to value %s" % (k, x)
77  raise FieldValidationError(self._field, self._config, msg)
78 
79  # validate keytype
80  k = _autocast(k, self._field.keytype)
81  if type(k) != self._field.keytype:
82  msg = "Key %r is of type %s, expected type %s" % \
83  (k, _typeStr(k), _typeStr(self._field.keytype))
84  raise FieldValidationError(self._field, self._config, msg)
85 
86  # validate itemtype
87  x = _autocast(x, self._field.itemtype)
88  if self._field.itemtype is None:
89  if type(x) not in self._field.supportedTypes and x is not None:
90  msg = "Value %s at key %r is of invalid type %s" % (x, k, _typeStr(x))
91  raise FieldValidationError(self._field, self._config, msg)
92  else:
93  if type(x) != self._field.itemtype and x is not None:
94  msg = "Value %s at key %r is of incorrect type %s. Expected type %s" % \
95  (x, k, _typeStr(x), _typeStr(self._field.itemtype))
96  raise FieldValidationError(self._field, self._config, msg)
97 
98  # validate item using itemcheck
99  if self._field.itemCheck is not None and not self._field.itemCheck(x):
100  msg = "Item at key %r is not a valid value: %s" % (k, x)
101  raise FieldValidationError(self._field, self._config, msg)
102 
103  if at is None:
104  at = getCallStack()
105 
106  self._dict[k] = x
107  if setHistory:
108  self._history.append((dict(self._dict), at, label))
109 
110  def __delitem__(self, k, at=None, label="delitem", setHistory=True):
111  if self._config._frozen:
112  raise FieldValidationError(self._field, self._config,
113  "Cannot modify a frozen Config")
114 
115  del self._dict[k]
116  if setHistory:
117  if at is None:
118  at = getCallStack()
119  self._history.append((dict(self._dict), at, label))
120 
121  def __repr__(self):
122  return repr(self._dict)
123 
124  def __str__(self):
125  return str(self._dict)
126 
127  def __setattr__(self, attr, value, at=None, label="assignment"):
128  if hasattr(getattr(self.__class__, attr, None), '__set__'):
129  # This allows properties to work.
130  object.__setattr__(self, attr, value)
131  elif attr in self.__dict__ or attr in ["_field", "_config", "_history", "_dict", "__doc__"]:
132  # This allows specific private attributes to work.
133  object.__setattr__(self, attr, value)
134  else:
135  # We throw everything else.
136  msg = "%s has no attribute %s" % (_typeStr(self._field), attr)
137  raise FieldValidationError(self._field, self._config, msg)
138 
139 
141  """
142  Defines a field which is a mapping of values
143 
144  Both key and item types are restricted to builtin POD types:
145  (int, float, complex, bool, str)
146 
147  Users can provide two check functions:
148  dictCheck: used to validate the dict as a whole, and
149  itemCheck: used to validate each item individually
150 
151  For example to define a field which is a mapping from names to int values:
152 
153  class MyConfig(Config):
154  field = DictField(
155  doc="example string-to-int mapping field",
156  keytype=str, itemtype=int,
157  default= {})
158  """
159  DictClass = Dict
160 
161  def __init__(self, doc, keytype, itemtype, default=None, optional=False, dictCheck=None, itemCheck=None):
162  source = getStackFrame()
163  self._setup(doc=doc, dtype=Dict, default=default, check=None,
164  optional=optional, source=source)
165  if keytype not in self.supportedTypes:
166  raise ValueError("'keytype' %s is not a supported type" %
167  _typeStr(keytype))
168  elif itemtype is not None and itemtype not in self.supportedTypes:
169  raise ValueError("'itemtype' %s is not a supported type" %
170  _typeStr(itemtype))
171  if dictCheck is not None and not hasattr(dictCheck, "__call__"):
172  raise ValueError("'dictCheck' must be callable")
173  if itemCheck is not None and not hasattr(itemCheck, "__call__"):
174  raise ValueError("'itemCheck' must be callable")
175 
176  self.keytype = keytype
177  self.itemtype = itemtype
178  self.dictCheck = dictCheck
179  self.itemCheck = itemCheck
180 
181  def validate(self, instance):
182  """
183  DictField validation ensures that non-optional fields are not None,
184  and that non-None values comply with dictCheck.
185  Individual Item checks are applied at set time and are not re-checked.
186  """
187  Field.validate(self, instance)
188  value = self.__get__(instance)
189  if value is not None and self.dictCheck is not None \
190  and not self.dictCheck(value):
191  msg = "%s is not a valid value" % str(value)
192  raise FieldValidationError(self, instance, msg)
193 
194  def __set__(self, instance, value, at=None, label="assignment"):
195  if instance._frozen:
196  msg = "Cannot modify a frozen Config. "\
197  "Attempting to set field to value %s" % value
198  raise FieldValidationError(self, instance, msg)
199 
200  if at is None:
201  at = getCallStack()
202  if value is not None:
203  value = self.DictClass(instance, self, value, at=at, label=label)
204  else:
205  history = instance._history.setdefault(self.name, [])
206  history.append((value, at, label))
207 
208  instance._storage[self.name] = value
209 
210  def toDict(self, instance):
211  value = self.__get__(instance)
212  return dict(value) if value is not None else None
213 
214  def _compare(self, instance1, instance2, shortcut, rtol, atol, output):
215  """Helper function for Config.compare; used to compare two fields for equality.
216 
217  @param[in] instance1 LHS Config instance to compare.
218  @param[in] instance2 RHS Config instance to compare.
219  @param[in] shortcut If True, return as soon as an inequality is found.
220  @param[in] rtol Relative tolerance for floating point comparisons.
221  @param[in] atol Absolute tolerance for floating point comparisons.
222  @param[in] output If not None, a callable that takes a string, used (possibly repeatedly)
223  to report inequalities.
224 
225  Floating point comparisons are performed by numpy.allclose; refer to that for details.
226  """
227  d1 = getattr(instance1, self.name)
228  d2 = getattr(instance2, self.name)
229  name = getComparisonName(
230  _joinNamePath(instance1._name, self.name),
231  _joinNamePath(instance2._name, self.name)
232  )
233  if not compareScalars("isnone for %s" % name, d1 is None, d2 is None, output=output):
234  return False
235  if d1 is None and d2 is None:
236  return True
237  if not compareScalars("keys for %s" % name, set(d1.keys()), set(d2.keys()), output=output):
238  return False
239  equal = True
240  for k, v1 in d1.items():
241  v2 = d2[k]
242  result = compareScalars("%s[%r]" % (name, k), v1, v2, dtype=self.itemtype,
243  rtol=rtol, atol=atol, output=output)
244  if not result and shortcut:
245  return False
246  equal = equal and result
247  return equal
def __init__(self, config, field, value, at, label, setHistory=True)
Definition: dictField.py:38
def __setitem__(self, k, x, at=None, label="setitem", setHistory=True)
Definition: dictField.py:73
def getCallStack(skip=0)
Definition: callStack.py:153
def __get__(self, instance, owner=None, at=None, label="default")
Definition: config.py:272
def _setup(self, doc, dtype, default, check, optional, source)
Definition: config.py:173
def getStackFrame(relative=0)
Definition: callStack.py:50
def __delitem__(self, k, at=None, label="delitem", setHistory=True)
Definition: dictField.py:110
def validate(self, instance)
Definition: dictField.py:181
def __setattr__(self, attr, value, at=None, label="assignment")
Definition: dictField.py:127
def compareScalars(name, v1, v2, output, rtol=1E-8, atol=1E-8, dtype=None)
Definition: comparison.py:41
def __init__(self, doc, keytype, itemtype, default=None, optional=False, dictCheck=None, itemCheck=None)
Definition: dictField.py:161
def getComparisonName(name1, name2)
Definition: comparison.py:35
def __set__(self, instance, value, at=None, label="assignment")
Definition: dictField.py:194