LSST Applications g0da5cf3356+25b44625d0,g17e5ecfddb+50a5ac4092,g1c76d35bf8+585f0f68a2,g295839609d+8ef6456700,g2e2c1a68ba+cc1f6f037e,g38293774b4+62d12e78cb,g3b44f30a73+2891c76795,g48ccf36440+885b902d19,g4b2f1765b6+0c565e8f25,g5320a0a9f6+bd4bf1dc76,g56364267ca+403c24672b,g56b687f8c9+585f0f68a2,g5c4744a4d9+78cd207961,g5ffd174ac0+bd4bf1dc76,g6075d09f38+3075de592a,g667d525e37+cacede5508,g6f3e93b5a3+da81c812ee,g71f27ac40c+cacede5508,g7212e027e3+eb621d73aa,g774830318a+18d2b9fa6c,g7985c39107+62d12e78cb,g79ca90bc5c+fa2cc03294,g881bdbfe6c+cacede5508,g91fc1fa0cf+82a115f028,g961520b1fb+2534687f64,g96f01af41f+f2060f23b6,g9ca82378b8+cacede5508,g9d27549199+78cd207961,gb065e2a02a+ad48cbcda4,gb1df4690d6+585f0f68a2,gb35d6563ee+62d12e78cb,gbc3249ced9+bd4bf1dc76,gbec6a3398f+bd4bf1dc76,gd01420fc67+bd4bf1dc76,gd59336e7c4+c7bb92e648,gf46e8334de+81c9a61069,gfed783d017+bd4bf1dc76,v25.0.1.rc3
LSST Data Management Base Package
Loading...
Searching...
No Matches
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

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 format supported:
- yaml: read and write is supported.

Definition at line 48 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 '.yaml'.
other (Policy) Copies the other Policy's values into this one.
other (dict) Copies the values from the dict into this Policy.

Reimplemented in lsst.daf.persistence.access.AccessCfg.

Definition at line 60 of file policy.py.

60 def __init__(self, other=None):
61 """Initialize the Policy. Other can be used to initialize the Policy in a variety of ways:
62 other (string) Treated as a path to a policy file on disk. Must end with '.yaml'.
63 other (Policy) Copies the other Policy's values into this one.
64 other (dict) Copies the values from the dict into this Policy.
65 """
66 collections.UserDict.__init__(self)
67
68 if other is None:
69 return
70
71 if isinstance(other, collections.abc.Mapping):
72 self.update(other)
73 elif isinstance(other, Policy):
74 self.data = copy.deepcopy(other.data)
75 elif isinstance(other, str):
76 # if other is a string, assume it is a file path.
77 self.__initFromFile(other)
78 else:
79 # if the policy specified by other could not be loaded raise a runtime error.
80 raise RuntimeError("A Policy could not be loaded from other:%s" % other)
81

Member Function Documentation

◆ __contains__()

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

Definition at line 161 of file policy.py.

161 def __contains__(self, key):
162 d = self.data
163 keys = key.split('.')
164 for k in keys[0:-1]:
165 if k in d:
166 d = d[k]
167 else:
168 return False
169 return keys[-1] in d
170

◆ __eq__()

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

Definition at line 352 of file policy.py.

352 def __eq__(self, other):
353 if isinstance(other, Policy):
354 other = other.data
355 return self.data == other
356

◆ __ge__()

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

Definition at line 367 of file policy.py.

367 def __ge__(self, other):
368 if isinstance(other, Policy):
369 other = other.data
370 return self.data >= other
371

◆ __getitem__()

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

Definition at line 132 of file policy.py.

132 def __getitem__(self, name):
133 data = self.data
134 for key in name.split('.'):
135 if data is None:
136 return None
137 if key in data:
138 data = data[key]
139 else:
140 return None
141 if isinstance(data, collections.abc.Mapping):
142 data = Policy(data)
143 return data
144

◆ __gt__()

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

Definition at line 362 of file policy.py.

362 def __gt__(self, other):
363 if isinstance(other, Policy):
364 other = other.data
365 return self.data > other
366

◆ __le__()

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

Definition at line 347 of file policy.py.

347 def __le__(self, other):
348 if isinstance(other, Policy):
349 other = other.data
350 return self.data <= other
351

◆ __lt__()

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

Definition at line 342 of file policy.py.

342 def __lt__(self, other):
343 if isinstance(other, Policy):
344 other = other.data
345 return self.data < other
346

◆ __ne__()

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

Definition at line 357 of file policy.py.

357 def __ne__(self, other):
358 if isinstance(other, Policy):
359 other = other.data
360 return self.data != other
361

◆ __repr__()

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

Definition at line 91 of file policy.py.

91 def __repr__(self):
92 return self.data.__repr__()
93

◆ __setitem__()

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

Definition at line 145 of file policy.py.

145 def __setitem__(self, name, value):
146 if isinstance(value, collections.abc.Mapping):
147 keys = name.split('.')
148 d = {}
149 cur = d
150 for key in keys[0:-1]:
151 cur[key] = {}
152 cur = cur[key]
153 cur[keys[-1]] = value
154 self.update(d)
155 data = self.data
156 keys = name.split('.')
157 for key in keys[0:-1]:
158 data = data.setdefault(key, {})
159 data[keys[-1]] = value
160

◆ 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 249 of file policy.py.

249 def asArray(self, name):
250 """Get a value as an array. May contain one or more elements.
251
252 :param key:
253 :return:
254 """
255 val = self.get(name)
256 if isinstance(val, str):
257 val = [val]
258 elif not isinstance(val, collections.abc.Container):
259 val = [val]
260 return val
261

◆ 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 172 of file policy.py.

172 def defaultPolicyFile(productName, fileName, relativePath=None):
173 """Get the path to a default policy file.
174
175 Determines a directory for the product specified by productName. Then Concatenates
176 productDir/relativePath/fileName (or productDir/fileName if relativePath is None) to find the path
177 to the default Policy file
178
179 @param productName (string) The name of the product that the default policy is installed as part of
180 @param fileName (string) The name of the policy file. Can also include a path to the file relative to
181 the directory where the product is installed.
182 @param relativePath (string) The relative path from the directior where the product is installed to
183 the location where the file (or the path to the file) is found. If None
184 (default), the fileName argument is relative to the installation
185 directory.
186 """
187 basePath = lsst.utils.getPackageDir(productName)
188 if not basePath:
189 raise RuntimeError("No product installed for productName: %s" % basePath)
190 if relativePath is not None:
191 basePath = os.path.join(basePath, relativePath)
192 fullFilePath = os.path.join(basePath, fileName)
193 return fullFilePath
194

◆ 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 375 of file policy.py.

375 def dump(self, output):
376 """Writes the policy to a yaml stream.
377
378 :param stream:
379 :return:
380 """
381 # First a set of known keys is handled and written to the stream in a specific order for readability.
382 # After the expected/ordered keys are weritten to the stream the remainder of the keys are written to
383 # the stream.
384 data = copy.copy(self.data)
385 keys = ['defects', 'needCalibRegistry', 'levels', 'defaultLevel', 'defaultSubLevels', 'camera',
386 'exposures', 'calibrations', 'datasets']
387 for key in keys:
388 try:
389 yaml.safe_dump({key: data.pop(key)}, output, default_flow_style=False)
390 output.write('\n')
391 except KeyError:
392 pass
393 if data:
394 yaml.safe_dump(data, output, default_flow_style=False)
395

◆ dumpToFile()

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

:param path:
:return:

Definition at line 396 of file policy.py.

396 def dumpToFile(self, path):
397 """Writes the policy to a file.
398
399 :param path:
400 :return:
401 """
402 with open(path, 'w') as f:
403 self.dump(f)

◆ 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 292 of file policy.py.

292 def exists(self, key):
293 """Query if a key exists in this Policy
294
295 :param key:
296 :return: True if the key exists, else false.
297 """
298 warnings.warn("Deprecated. Use 'key in object'", DeprecationWarning)
299 return key in self
300

◆ 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 310 of file policy.py.

310 def getBool(self, key):
311 """Get the value of a key.
312
313 :param key:
314 :return: the value for key
315 """
316 warnings.warn("Deprecated. Use []", DeprecationWarning)
317 return bool(self[key])
318

◆ getPolicy()

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

:param key:
:return:

Definition at line 319 of file policy.py.

319 def getPolicy(self, key):
320 """Get a subpolicy.
321
322 :param key:
323 :return:
324 """
325 warnings.warn("Deprecated. Use []", DeprecationWarning)
326 return self[key]
327

◆ 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 301 of file policy.py.

301 def getString(self, key):
302 """Get the string value of a key.
303
304 :param key:
305 :return: the value for key
306 """
307 warnings.warn("Deprecated. Use []", DeprecationWarning)
308 return str(self[key])
309

◆ 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 328 of file policy.py.

328 def getStringArray(self, key):
329 """Get a value as an array. May contain one or more elements.
330
331 :param key:
332 :return:
333 """
334 warnings.warn("Deprecated. Use asArray()", DeprecationWarning)
335 val = self.get(key)
336 if isinstance(val, str):
337 val = [val]
338 elif not isinstance(val, collections.abc.Container):
339 val = [val]
340 return val
341

◆ 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 265 of file policy.py.

265 def getValue(self, name):
266 """Get the value for a parameter name/key. See class notes about dot-delimited access.
267
268 :param name:
269 :return: the value for the given name.
270 """
271 warnings.warn_explicit("Deprecated. Use []", DeprecationWarning)
272 return self[name]
273

◆ 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 219 of file policy.py.

219 def merge(self, other):
220 """Like Policy.update, but will add keys & values from other that DO NOT EXIST in self. Keys and
221 values that already exist in self will NOT be overwritten.
222
223 :param other:
224 :return:
225 """
226 otherCopy = copy.deepcopy(other)
227 otherCopy.update(self)
228 self.data = otherCopy.data
229

◆ 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 283 of file policy.py.

283 def mergeDefaults(self, other):
284 """For any keys in other that are not present in self, sets that key and its value into self.
285
286 :param other: another Policy
287 :return: None
288 """
289 warnings.warn("Deprecated. Use .merge()", DeprecationWarning)
290 self.merge(other)
291

◆ 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 230 of file policy.py.

230 def names(self, topLevelOnly=False):
231 """Get the dot-delimited name of all the keys in the hierarchy.
232 NOTE: this is different than the built-in method dict.keys, which will return only the first level
233 keys.
234 """
235 if topLevelOnly:
236 return list(self.keys())
237
238 def getKeys(d, keys, base):
239 for key in d:
240 val = d[key]
241 levelKey = base + '.' + key if base is not None else key
242 keys.append(levelKey)
243 if isinstance(val, collections.abc.Mapping):
244 getKeys(val, keys, levelKey)
245 keys = []
246 getKeys(self.data, keys, None)
247 return keys
248
daf::base::PropertyList * list
Definition: fits.cc:928

◆ 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 82 of file policy.py.

82 def ppprint(self):
83 """helper function for debugging, prints a policy out in a readable way in the debugger.
84
85 use: pdb> print myPolicyObject.pprint()
86 :return: a prettyprint formatted string representing the policy
87 """
88 import pprint
89 return pprint.pformat(self.data, indent=2, width=1)
90

◆ 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 274 of file policy.py.

274 def setValue(self, name, value):
275 """Set the value for a parameter name/key. See class notes about dot-delimited access.
276
277 :param name:
278 :return: None
279 """
280 warnings.warn("Deprecated. Use []", DeprecationWarning)
281 self[name] = value
282

◆ 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 195 of file policy.py.

195 def update(self, other):
196 """Like dict.update, but will add or modify keys in nested dicts, instead of overwriting the nested
197 dict entirely.
198
199 For example, for the given code:
200 foo = {'a': {'b': 1}}
201 foo.update({'a': {'c': 2}})
202
203 If foo is a dict, then after the update foo == {'a': {'c': 2}}
204 But if foo is a Policy, then after the update foo == {'a': {'b': 1, 'c': 2}}
205 """
206 def doUpdate(d, u):
207 for k, v in u.items():
208 if isinstance(d, collections.abc.Mapping):
209 if isinstance(v, collections.abc.Mapping):
210 r = doUpdate(d.get(k, {}), v)
211 d[k] = r
212 else:
213 d[k] = u[k]
214 else:
215 d = {k: u[k]}
216 return d
217 doUpdate(self.data, other)
218

Member Data Documentation

◆ data

lsst.daf.persistence.policy.Policy.data

Definition at line 74 of file policy.py.


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