LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
LSSTDataManagementBasePackage
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
lsst.daf.persistence.policy.Policy Class Reference
Inheritance diagram for lsst.daf.persistence.policy.Policy:
lsst.daf.persistence.policy._PolicyBase lsst.daf.persistence.policy._PolicyMeta lsst.daf.persistence.access.AccessCfg lsst.daf.persistence.butler.ButlerCfg

Public Member Functions

def __init__ (self, other=None)
 
def ppprint (self)
 
def __repr__ (self)
 
def __getitem__ (self, name)
 
def __setitem__ (self, name, value)
 
def __contains__ (self, key)
 
def update (self, other)
 
def merge (self, other)
 
def names (self, topLevelOnly=False)
 
def asArray (self, name)
 
def getValue (self, name)
 
def setValue (self, name, value)
 
def mergeDefaults (self, other)
 
def exists (self, key)
 
def getString (self, key)
 
def getBool (self, key)
 
def getPolicy (self, key)
 
def getStringArray (self, key)
 
def __lt__ (self, other)
 
def __le__ (self, other)
 
def __eq__ (self, other)
 
def __ne__ (self, other)
 
def __gt__ (self, other)
 
def __ge__ (self, other)
 
def dump (self, output)
 i/o # More...
 
def dumpToFile (self, path)
 

Static Public Member Functions

def defaultPolicyFile (productName, fileName, relativePath=None)
 

Public Attributes

 data
 

Detailed Description

Policy implements a datatype that is used by Butler for configuration parameters.
It is essentially a dict with key/value pairs, including nested dicts (as values). In fact, it can be
initialized with a dict. The only caveat is that keys may NOT contain dots ('.'). This is explained next:
Policy extends the dict api so that hierarchical values may be accessed with dot-delimited notiation.
That is, foo.getValue('a.b.c') is the same as foo['a']['b']['c'] is the same as foo['a.b.c'], and either
of these syntaxes may be used.

Storage formats supported:
- yaml: read and write is supported.
- pex policy: read is supported, although this is deprecated and will at some point be removed.

Definition at line 58 of file policy.py.

Constructor & Destructor Documentation

◆ __init__()

def lsst.daf.persistence.policy.Policy.__init__ (   self,
  other = None 
)
Initialize the Policy. Other can be used to initialize the Policy in a variety of ways:
other (string) Treated as a path to a policy file on disk. Must end with '.paf' or '.yaml'.
other (Pex Policy) Initializes this Policy with the values in the passed-in Pex Policy.
other (Policy) Copies the other Policy's values into this one.
other (dict) Copies the values from the dict into this Policy.

Definition at line 71 of file policy.py.

71  def __init__(self, other=None):
72  """Initialize the Policy. Other can be used to initialize the Policy in a variety of ways:
73  other (string) Treated as a path to a policy file on disk. Must end with '.paf' or '.yaml'.
74  other (Pex Policy) Initializes this Policy with the values in the passed-in Pex Policy.
75  other (Policy) Copies the other Policy's values into this one.
76  other (dict) Copies the values from the dict into this Policy.
77  """
78  collections.UserDict.__init__(self)
79 
80  if other is None:
81  return
82 
83  if isinstance(other, collections.abc.Mapping):
84  self.update(other)
85  elif isinstance(other, Policy):
86  self.data = copy.deepcopy(other.data)
87  elif isinstance(other, basestring):
88  # if other is a string, assume it is a file path.
89  self.__initFromFile(other)
90  elif isinstance(other, pexPolicy.Policy):
91  # if other is an instance of a Pex Policy, load it accordingly.
92  self.__initFromPexPolicy(other)
93  else:
94  # if the policy specified by other could not be loaded raise a runtime error.
95  raise RuntimeError("A Policy could not be loaded from other:%s" % other)
96 
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
def __init__(self, minimum, dataRange, Q)

Member Function Documentation

◆ __contains__()

def lsst.daf.persistence.policy.Policy.__contains__ (   self,
  key 
)

Definition at line 201 of file policy.py.

201  def __contains__(self, key):
202  d = self.data
203  keys = key.split('.')
204  for k in keys[0:-1]:
205  if k in d:
206  d = d[k]
207  else:
208  return False
209  return keys[-1] in d
210 

◆ __eq__()

def lsst.daf.persistence.policy.Policy.__eq__ (   self,
  other 
)

Definition at line 392 of file policy.py.

392  def __eq__(self, other):
393  if isinstance(other, Policy):
394  other = other.data
395  return self.data == other
396 

◆ __ge__()

def lsst.daf.persistence.policy.Policy.__ge__ (   self,
  other 
)

Definition at line 407 of file policy.py.

407  def __ge__(self, other):
408  if isinstance(other, Policy):
409  other = other.data
410  return self.data >= other
411 

◆ __getitem__()

def lsst.daf.persistence.policy.Policy.__getitem__ (   self,
  name 
)

Definition at line 172 of file policy.py.

172  def __getitem__(self, name):
173  data = self.data
174  for key in name.split('.'):
175  if data is None:
176  return None
177  if key in data:
178  data = data[key]
179  else:
180  return None
181  if isinstance(data, collections.abc.Mapping):
182  data = Policy(data)
183  return data
184 

◆ __gt__()

def lsst.daf.persistence.policy.Policy.__gt__ (   self,
  other 
)

Definition at line 402 of file policy.py.

402  def __gt__(self, other):
403  if isinstance(other, Policy):
404  other = other.data
405  return self.data > other
406 

◆ __le__()

def lsst.daf.persistence.policy.Policy.__le__ (   self,
  other 
)

Definition at line 387 of file policy.py.

387  def __le__(self, other):
388  if isinstance(other, Policy):
389  other = other.data
390  return self.data <= other
391 

◆ __lt__()

def lsst.daf.persistence.policy.Policy.__lt__ (   self,
  other 
)

Definition at line 382 of file policy.py.

382  def __lt__(self, other):
383  if isinstance(other, Policy):
384  other = other.data
385  return self.data < other
386 

◆ __ne__()

def lsst.daf.persistence.policy.Policy.__ne__ (   self,
  other 
)

Definition at line 397 of file policy.py.

397  def __ne__(self, other):
398  if isinstance(other, Policy):
399  other = other.data
400  return self.data != other
401 

◆ __repr__()

def lsst.daf.persistence.policy.Policy.__repr__ (   self)

Definition at line 106 of file policy.py.

106  def __repr__(self):
107  return self.data.__repr__()
108 

◆ __setitem__()

def lsst.daf.persistence.policy.Policy.__setitem__ (   self,
  name,
  value 
)

Definition at line 185 of file policy.py.

185  def __setitem__(self, name, value):
186  if isinstance(value, collections.abc.Mapping):
187  keys = name.split('.')
188  d = {}
189  cur = d
190  for key in keys[0:-1]:
191  cur[key] = {}
192  cur = cur[key]
193  cur[keys[-1]] = value
194  self.update(d)
195  data = self.data
196  keys = name.split('.')
197  for key in keys[0:-1]:
198  data = data.setdefault(key, {})
199  data[keys[-1]] = value
200 

◆ asArray()

def lsst.daf.persistence.policy.Policy.asArray (   self,
  name 
)
Get a value as an array. May contain one or more elements.

:param key:
:return:

Definition at line 289 of file policy.py.

289  def asArray(self, name):
290  """Get a value as an array. May contain one or more elements.
291 
292  :param key:
293  :return:
294  """
295  val = self.get(name)
296  if isinstance(val, basestring):
297  val = [val]
298  elif not isinstance(val, collections.abc.Container):
299  val = [val]
300  return val
301 

◆ defaultPolicyFile()

def lsst.daf.persistence.policy.Policy.defaultPolicyFile (   productName,
  fileName,
  relativePath = None 
)
static
Get the path to a default policy file.

Determines a directory for the product specified by productName. Then Concatenates
productDir/relativePath/fileName (or productDir/fileName if relativePath is None) to find the path
to the default Policy file

@param productName (string) The name of the product that the default policy is installed as part of
@param fileName (string) The name of the policy file. Can also include a path to the file relative to
                 the directory where the product is installed.
@param relativePath (string) The relative path from the directior where the product is installed to
                     the location where the file (or the path to the file) is found. If None
                     (default), the fileName argument is relative to the installation
                     directory.

Definition at line 212 of file policy.py.

212  def defaultPolicyFile(productName, fileName, relativePath=None):
213  """Get the path to a default policy file.
214 
215  Determines a directory for the product specified by productName. Then Concatenates
216  productDir/relativePath/fileName (or productDir/fileName if relativePath is None) to find the path
217  to the default Policy file
218 
219  @param productName (string) The name of the product that the default policy is installed as part of
220  @param fileName (string) The name of the policy file. Can also include a path to the file relative to
221  the directory where the product is installed.
222  @param relativePath (string) The relative path from the directior where the product is installed to
223  the location where the file (or the path to the file) is found. If None
224  (default), the fileName argument is relative to the installation
225  directory.
226  """
227  basePath = lsst.utils.getPackageDir(productName)
228  if not basePath:
229  raise RuntimeError("No product installed for productName: %s" % basePath)
230  if relativePath is not None:
231  basePath = os.path.join(basePath, relativePath)
232  fullFilePath = os.path.join(basePath, fileName)
233  return fullFilePath
234 
std::string getPackageDir(std::string const &packageName)
return the root directory of a setup package
Definition: packaging.cc:33

◆ dump()

def lsst.daf.persistence.policy.Policy.dump (   self,
  output 
)

i/o #

Writes the policy to a yaml stream.

:param stream:
:return:

Definition at line 415 of file policy.py.

415  def dump(self, output):
416  """Writes the policy to a yaml stream.
417 
418  :param stream:
419  :return:
420  """
421  # First a set of known keys is handled and written to the stream in a specific order for readability.
422  # After the expected/ordered keys are weritten to the stream the remainder of the keys are written to
423  # the stream.
424  data = copy.copy(self.data)
425  keys = ['defects', 'needCalibRegistry', 'levels', 'defaultLevel', 'defaultSubLevels', 'camera',
426  'exposures', 'calibrations', 'datasets']
427  for key in keys:
428  try:
429  yaml.safe_dump({key: data.pop(key)}, output, default_flow_style=False)
430  output.write('\n')
431  except KeyError:
432  pass
433  if data:
434  yaml.safe_dump(data, output, default_flow_style=False)
435 

◆ dumpToFile()

def lsst.daf.persistence.policy.Policy.dumpToFile (   self,
  path 
)
Writes the policy to a file.

:param path:
:return:

Definition at line 436 of file policy.py.

436  def dumpToFile(self, path):
437  """Writes the policy to a file.
438 
439  :param path:
440  :return:
441  """
442  with open(path, 'w') as f:
443  self.dump(f)
444 

◆ exists()

def lsst.daf.persistence.policy.Policy.exists (   self,
  key 
)
Query if a key exists in this Policy

:param key:
:return: True if the key exists, else false.

Definition at line 332 of file policy.py.

332  def exists(self, key):
333  """Query if a key exists in this Policy
334 
335  :param key:
336  :return: True if the key exists, else false.
337  """
338  warnings.warn("Deprecated. Use 'key in object'", DeprecationWarning)
339  return key in self
340 

◆ getBool()

def lsst.daf.persistence.policy.Policy.getBool (   self,
  key 
)
Get the value of a key.

:param key:
:return: the value for key

Definition at line 350 of file policy.py.

350  def getBool(self, key):
351  """Get the value of a key.
352 
353  :param key:
354  :return: the value for key
355  """
356  warnings.warn("Deprecated. Use []", DeprecationWarning)
357  return bool(self[key])
358 

◆ getPolicy()

def lsst.daf.persistence.policy.Policy.getPolicy (   self,
  key 
)
Get a subpolicy.

:param key:
:return:

Definition at line 359 of file policy.py.

359  def getPolicy(self, key):
360  """Get a subpolicy.
361 
362  :param key:
363  :return:
364  """
365  warnings.warn("Deprecated. Use []", DeprecationWarning)
366  return self[key]
367 

◆ getString()

def lsst.daf.persistence.policy.Policy.getString (   self,
  key 
)
Get the string value of a key.

:param key:
:return: the value for key

Definition at line 341 of file policy.py.

341  def getString(self, key):
342  """Get the string value of a key.
343 
344  :param key:
345  :return: the value for key
346  """
347  warnings.warn("Deprecated. Use []", DeprecationWarning)
348  return str(self[key])
349 

◆ getStringArray()

def lsst.daf.persistence.policy.Policy.getStringArray (   self,
  key 
)
Get a value as an array. May contain one or more elements.

:param key:
:return:

Definition at line 368 of file policy.py.

368  def getStringArray(self, key):
369  """Get a value as an array. May contain one or more elements.
370 
371  :param key:
372  :return:
373  """
374  warnings.warn("Deprecated. Use asArray()", DeprecationWarning)
375  val = self.get(key)
376  if isinstance(val, basestring):
377  val = [val]
378  elif not isinstance(val, collections.abc.Container):
379  val = [val]
380  return val
381 

◆ getValue()

def lsst.daf.persistence.policy.Policy.getValue (   self,
  name 
)
Get the value for a parameter name/key. See class notes about dot-delimited access.

:param name:
:return: the value for the given name.

Definition at line 305 of file policy.py.

305  def getValue(self, name):
306  """Get the value for a parameter name/key. See class notes about dot-delimited access.
307 
308  :param name:
309  :return: the value for the given name.
310  """
311  warnings.warn_explicit("Deprecated. Use []", DeprecationWarning)
312  return self[name]
313 

◆ merge()

def lsst.daf.persistence.policy.Policy.merge (   self,
  other 
)
Like Policy.update, but will add keys & values from other that DO NOT EXIST in self. Keys and
values that already exist in self will NOT be overwritten.

:param other:
:return:

Definition at line 259 of file policy.py.

259  def merge(self, other):
260  """Like Policy.update, but will add keys & values from other that DO NOT EXIST in self. Keys and
261  values that already exist in self will NOT be overwritten.
262 
263  :param other:
264  :return:
265  """
266  otherCopy = copy.deepcopy(other)
267  otherCopy.update(self)
268  self.data = otherCopy.data
269 

◆ mergeDefaults()

def lsst.daf.persistence.policy.Policy.mergeDefaults (   self,
  other 
)
For any keys in other that are not present in self, sets that key and its value into self.

:param other: another Policy
:return: None

Definition at line 323 of file policy.py.

323  def mergeDefaults(self, other):
324  """For any keys in other that are not present in self, sets that key and its value into self.
325 
326  :param other: another Policy
327  :return: None
328  """
329  warnings.warn("Deprecated. Use .merge()", DeprecationWarning)
330  self.merge(other)
331 

◆ names()

def lsst.daf.persistence.policy.Policy.names (   self,
  topLevelOnly = False 
)
Get the dot-delimited name of all the keys in the hierarchy.
NOTE: this is different than the built-in method dict.keys, which will return only the first level
keys.

Definition at line 270 of file policy.py.

270  def names(self, topLevelOnly=False):
271  """Get the dot-delimited name of all the keys in the hierarchy.
272  NOTE: this is different than the built-in method dict.keys, which will return only the first level
273  keys.
274  """
275  if topLevelOnly:
276  return list(self.keys())
277 
278  def getKeys(d, keys, base):
279  for key in d:
280  val = d[key]
281  levelKey = base + '.' + key if base is not None else key
282  keys.append(levelKey)
283  if isinstance(val, collections.abc.Mapping):
284  getKeys(val, keys, levelKey)
285  keys = []
286  getKeys(self.data, keys, None)
287  return keys
288 
daf::base::PropertyList * list
Definition: fits.cc:885

◆ ppprint()

def lsst.daf.persistence.policy.Policy.ppprint (   self)
helper function for debugging, prints a policy out in a readable way in the debugger.

use: pdb> print myPolicyObject.pprint()
:return: a prettyprint formatted string representing the policy

Definition at line 97 of file policy.py.

97  def ppprint(self):
98  """helper function for debugging, prints a policy out in a readable way in the debugger.
99 
100  use: pdb> print myPolicyObject.pprint()
101  :return: a prettyprint formatted string representing the policy
102  """
103  import pprint
104  return pprint.pformat(self.data, indent=2, width=1)
105 

◆ setValue()

def lsst.daf.persistence.policy.Policy.setValue (   self,
  name,
  value 
)
Set the value for a parameter name/key. See class notes about dot-delimited access.

:param name:
:return: None

Definition at line 314 of file policy.py.

314  def setValue(self, name, value):
315  """Set the value for a parameter name/key. See class notes about dot-delimited access.
316 
317  :param name:
318  :return: None
319  """
320  warnings.warn("Deprecated. Use []", DeprecationWarning)
321  self[name] = value
322 

◆ update()

def lsst.daf.persistence.policy.Policy.update (   self,
  other 
)
Like dict.update, but will add or modify keys in nested dicts, instead of overwriting the nested
dict entirely.

For example, for the given code:
foo = {'a': {'b': 1}}
foo.update({'a': {'c': 2}})

If foo is a dict, then after the update foo == {'a': {'c': 2}}
But if foo is a Policy, then after the update foo == {'a': {'b': 1, 'c': 2}}

Definition at line 235 of file policy.py.

235  def update(self, other):
236  """Like dict.update, but will add or modify keys in nested dicts, instead of overwriting the nested
237  dict entirely.
238 
239  For example, for the given code:
240  foo = {'a': {'b': 1}}
241  foo.update({'a': {'c': 2}})
242 
243  If foo is a dict, then after the update foo == {'a': {'c': 2}}
244  But if foo is a Policy, then after the update foo == {'a': {'b': 1, 'c': 2}}
245  """
246  def doUpdate(d, u):
247  for k, v in u.items():
248  if isinstance(d, collections.abc.Mapping):
249  if isinstance(v, collections.abc.Mapping):
250  r = doUpdate(d.get(k, {}), v)
251  d[k] = r
252  else:
253  d[k] = u[k]
254  else:
255  d = {k: u[k]}
256  return d
257  doUpdate(self.data, other)
258 

Member Data Documentation

◆ data

lsst.daf.persistence.policy.Policy.data

Definition at line 86 of file policy.py.


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