SimpleConstraint

class jwst.associations.lib.constraint.SimpleConstraint(*args, **kwargs)[source]

Bases: SimpleConstraintABC

A basic constraint.

Parameters:
initdict

Dictionary where the key-value pairs define the following parameters.

sourcesfunc(item) or None

Function taking item as 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 value to 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 attr of an object matches the value my_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 of True and a reprocess list:

>>> item = "my_value"
>>> c.check_and_set(item)
(True, [])

If it doesn’t match, False will be returned:

>>> bad_item = "not_my_value"
>>> c.check_and_set(bad_item)
(False, [])

A SimpleConstraint can also be initialized by a dict of 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, the SimpleConstraint will successfully match whatever object given. However, a new SimpleConstraint will be returned where the value is 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_unique parameter:

>>> 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:
SimpleConstraintABC

New 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.

eq(value1, value2)[source]

Check if constraint.value and item are equal.

Parameters:
value1any

The first value to compare.

value2any

The second value to compare.

Returns:
bool

True if the two values are deemed equal.