SimpleConstraint
- class jwst.associations.lib.constraint.SimpleConstraint(*args, **kwargs)[source]
Bases:
SimpleConstraintABCA basic constraint.
- Parameters:
- initdict
Dictionary where the key-value pairs define the following parameters.
- sourcesfunc(item) or None
Function taking
itemas argument used to retrieve a value to check against. If None, the item itself is used as the value.- force_uniquebool
If the constraint is satisfied, reset
valueto the value of the source.- testfunction
The test function for the constraint. Takes two arguments:
constraint, and
object to compare against.
Returns a boolean. Default is
eq().- reprocess_on_matchbool
Reprocess the item if the constraint is satisfied.
- reprocess_on_failbool
Reprocess the item if the constraint is not satisfied.
- work_overListCategory.[BOTH, EXISTING, RULES]
The condition on which this constraint should operate.
- reprocess_rules[rule[,..]] or None
List of rules to be applied to. If None, calling function will determine the ruleset. If empty, [], all rules will be used.
Examples
Create a constraint where the attribute
attrof an object matches the valuemy_value:>>> from jwst.associations.lib.constraint import SimpleConstraint >>> c = SimpleConstraint(value="my_value") >>> print(c) SimpleConstraint({'name': None, 'value': 'my_value'})
To check a constraint, call
check_and_set(). A successful match will return a tuple ofTrueand a reprocess list:>>> item = "my_value" >>> c.check_and_set(item) (True, [])
If it doesn’t match,
Falsewill be returned:>>> bad_item = "not_my_value" >>> c.check_and_set(bad_item) (False, [])
A
SimpleConstraintcan also be initialized by adictof the relevant parameters:>>> init = {"value": "my_value"} >>> c = SimpleConstraint(init) >>> print(c) SimpleConstraint({'name': None, 'value': 'my_value'})
If the value to check is
None, theSimpleConstraintwill successfully match whatever object given. However, a newSimpleConstraintwill be returned where thevalueis now set to whatever the attribute was of the object:>>> c = SimpleConstraint(value=None) >>> matched, reprocess = c.check_and_set(item) >>> print(c) SimpleConstraint({'name': None, 'value': 'my_value'})
This behavior can be overridden by the
force_uniqueparameter:>>> c = SimpleConstraint(value=None, force_unique=False) >>> matched, reprocess = c.check_and_set(item) >>> print(c) SimpleConstraint({'name': None, 'value': None})
Force creation of the constraint attribute dict before anything else.
- Returns:
SimpleConstraintABCNew instance of class.
Methods Summary
check_and_set(item)Check and set the constraint.
eq(value1, value2)Check if constraint.value and item are equal.
Methods Documentation
- check_and_set(item)[source]
Check and set the constraint.
- Returns:
- successbool
True if check is successful.
- reprocesslist of
ProcessList List of ProcessLists.