SimpleConstraint

class jwst.associations.lib.constraint.SimpleConstraint(init=None, sources=None, force_unique=True, test=None, reprocess_on_match=False, reprocess_on_fail=False, work_over=ListCategory.BOTH, reprocess_rules=None, **kwargs)[source]

Bases: SimpleConstraintABC

A basic constraint

Parameters:
  • init (dict) – dict where the key:value pairs define the following parameters

  • value (object or None) – Value that must be matched. If None, any retrieved value will match.

  • sources (func(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_unique (bool) – If the constraint is satisfied, reset value to the value of the source.

  • test (function) –

    The test function for the constraint. Takes two arguments:

    • constraint

    • object to compare against.

    Returns a boolean. Default is SimpleConstraint.eq

  • name (str or None) – Option name for constraint

  • reprocess_on_match (bool) – Reprocess the item if the constraint is satisfied.

  • reprocess_on_fail (bool) – Reprocess the item if the constraint is not satisfied.

  • work_over (ListCategory.[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.

All `Parameters` are also `Attributes`

Examples

Create a constraint where the attribute attr of an object matches the value my_value:

>>> 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})

Methods Summary

check_and_set(item)

Check and set the constraint

eq(value1, value2)

True if constraint.value and item are equal.

Methods Documentation

check_and_set(item)[source]

Check and set the constraint

Returns:

success, reprocess

Returns 2-tuple of

Return type:

bool, [ProcessList[,…]]

eq(value1, value2)[source]

True if constraint.value and item are equal.