Jump to >

djblets.conditions

Condition rule support for applications.

This module contains convenience imports for:

class Condition(choice, operator, value=None, raw_value=None)[source]

Bases: object

A condition used to match state to a choice, operator, and value.

Conditions store a choice, operator, and value (depending on the operator). Callers can query whether a value fulfills a given condition, making it easy for users to compose sets of rules safely for controlling behavior in an application without having to write any code.

Generally, queries will be made against a ConditionSet, instead of an individual Condition.

choice

djblets.conditions.choices.BaseConditionChoice – The choice stored for this condition.

operator

djblets.conditions.operators.BaseConditionOperator – The operator stored for this condition.

value

object – The value stored for this condition.

raw_value

object – The raw (serialized) value for this condition. This is used internally, and won’t usually be needed by a caller.

classmethod deserialize(choices, data, condition_index=None, choice_kwargs={})[source]

Deserialize a condition from serialized data.

This expects data serialized by serialize().

Parameters:
  • choices (djblets.conditions.choices.ConditionChoices) – Possible choices for the condition.
  • data (dict) – Serialized data representing this condition.
  • condition_index (int, optional) – The index of the condition within the set of conditions. This is used for exceptions to help identify which condition failed during deserialization.
Returns:

The deserialized condition.

Return type:

djblets.conditions.conditions.Condition

Raises:
__init__(choice, operator, value=None, raw_value=None)[source]

Initialize the condition.

Parameters:
matches(value, value_state_cache=None)[source]

Return whether a value matches the condition.

Parameters:
  • value (object) – The value to match against.
  • value_state_cache (dict) – An optional dictionary used to cache common computable data that might be shared across instances of one or more conditions.
Returns:

True if the value fulfills the condition. False if it does not.

Return type:

bool

serialize()[source]

Serialize the condition to a JSON-serializable dictionary.

Returns:A dictionary representing the condition. It can be safely serialized to JSON.
Return type:dict
to_json()[source]

Serialize the condition to a JSON-serializable dictionary.

Returns:A dictionary representing the condition. It can be safely serialized to JSON.
Return type:dict
class ConditionSet(mode=u'all', conditions=[])[source]

Bases: object

A set of conditions used to match state and define rules.

Condition sets own multiple conditions, and are given a mode indicating how to query state against those conditions. They’re also responsible for serializing and deserializing all data around a set of conditions to a JSON-serializable format.

If using MODE_ALL, then all conditions must be satisfied for a condition set to pass. If using MODE_ANY, then only one condition must be satisfied.

mode

unicode – The matching mode for the condition set. This is one of MODE_ALL or MODE_ANY.

conditions

list of Condition – The list of conditions that comprise this set.

MODE_ALWAYS = u'always'[source]

Always match without conditions.

MODE_ALL = u'all'[source]

All conditions must match a value to satisfy the condition set.

MODE_ANY = u'any'[source]

Any condition may match a value to satisfy the condition set.

CONDITIONS = (u'always', u'all', u'any')[source]

A set of all the valid modes.

DEFAULT_MODE = u'all'[source]

The default mode.

classmethod deserialize(choices, data, choice_kwargs={})[source]

Deserialize a set of conditions from serialized data.

This expects data serialized by deserialize().

Parameters:
Returns:

The deserialized condition set.

Return type:

djblets.conditions.conditions.ConditionSet

Raises:
__init__(mode=u'all', conditions=[])[source]

Initialize the condition set.

Parameters:
  • mode (unicode, optional) – The match mode. This defaults to MODE_ALL.
  • conditions (list, optional) – The conditions that make up this set. This defaults to an empty list.
Raises:

djblets.conditions.errors.InvalidConditionModeError – The match mode is not a valid mode.

matches(**values)[source]

Check if a value matches the condition set.

Depending on the mode of the condition set, this will either require all conditions to match, or only one.

Parameters:**values (dict) – Values to match against. By default, condition choices will match against a single value keyword argument, but more specialized uses might take into account one or more other keyword arguments.
Returns:True if the value fulfills the condition set. False if it does not.
Return type:bool
serialize()[source]

Serialize the condition set to a JSON-serializable dictionary.

Returns:A dictionary representing the condition set. It can be safely serialized to JSON.
Return type:dict
to_json()[source]

Serialize the condition set to a JSON-serializable dictionary.

Returns:A dictionary representing the condition set. It can be safely serialized to JSON.
Return type:dict