Pyruler API

Module contents

Pyruler main API objects to generate validations.

Submodules

Module pyruler.pyruler

Ruler definitions.

class pyruler.pyruler.Ruler

Bases: object

Store RuleSet objects to be applied over a given data. This class can apply one single stored RuleSet, a list of of some of the configured RuleSet objects or all of them.

add_many(rule_sets: Iterable[pyruler.ruleset.RuleSet])NoReturn

Add many RuleSet objects at ones.

Parameters

rule_sets – Iterable object of rule sets to be added

add_set(rule_set: pyruler.ruleset.RuleSet)NoReturn

Add a new Rule set to the ruler.

Parameters

rule_set – configured rule set object

Raises

RulerConfigError – When the ruler detects that the provided rule_set was already configured

apply(data: Any, sets: Optional[Union[AnyStr, Tuple, List, Set]] = None, fail_fast: Optional[bool] = True)NoReturn

Apply specified rule set, all of the stored sets or a sub collection of the stored rule sets.

Parameters
  • sets – Rule sets to be applied

  • data – Data to be validated by the rule sets

  • fail_fast – Run on fail fast mode

Raises
  • RuleError – When some rule was not asserted successfully by the rule set

  • RulerError – When some set can’t be applied

count_sets()int

Count the total of configured sets.

Return int

Count of sets

rule_set_names()List

Return a list of all configured RuleSet objects in the ruler.

Returns

List of rule set names

Module pyruler.ruleset

Implementation of RuleSet policies.

class pyruler.ruleset.RuleSet(name: AnyStr)

Bases: object

Rule Set definition to apply a set of rules to a context info.

Parameters

name – Identifier name of the rule set

add_many(rules: Iterable[pyruler.rule.Rule])None

Add many rules at ones.

Parameters

rules – Iterable object of rules

add_rule(rule: pyruler.rule.Rule)NoReturn

Add new role to the set.

Parameters

rule – New Rule object to be applied by the set

Raises

RuleSetConfigError – when the set detects that the provided rule was already added to the set

apply(data: Any, fail_fast: Optional[bool] = True)NoReturn

Apply the configured rule set to a specific data.

Parameters
  • data – Data to be validated by the rule set

  • fail_fast – flag to determine if the error will raise at first not True rule, or will execute all and collect all the rules that fails with the data before raise it.

Raises

RuleError – when the data is not complaint with some rule of the set

count_rules()int

Count the total of rules configured on the set.

Return int

Count of rules

property name

Get property value.

rule_names()List

Return a list with the names of all the configured rules of the set.

Return List[AnyStr]

List of rule names

Module pyruler.rule

Implementation of simple rule.

class pyruler.rule.Rule(name: AnyStr, resolver: Optional[Callable] = None, error: Optional[Exception] = None)

Bases: object

Definition for atomic operation that validates data.

Parameters
  • name – Name of the rule

  • resolver – Callable function that receives exactly 1 param as the data that will be validated

  • error – Custom exception that can be raised by the RuleSet in case the rule validation fails

property error

Return the save custom error.

Return Exception

Configured error

execute(data: Any)bool

Execute rule validation.

Parameters

data – Data to be validated by the Rule

Raises

RuleConfigError – When the resolver is not callable

property name

Return the name of the rule.

Return AnyStr

Rule name

Module pyruler.errors

Custom ruler errors.

exception pyruler.errors.RuleConfigError

Bases: Exception

Error for Rule configuration

exception pyruler.errors.RuleError

Bases: Exception

Base Rule error.

exception pyruler.errors.RuleSetConfigError

Bases: Exception

Base error for RuleSet configuration.

exception pyruler.errors.RuleSetError

Bases: Exception

Base RuleSet error.

exception pyruler.errors.RulerConfigError

Bases: Exception

Base error for Ruler configuration.

exception pyruler.errors.RulerError

Bases: Exception

Base Ruler error.

Module pyruler.linked_list

General purpose LinkedList that handle RuleSet Rules.

class pyruler.linked_list.LinkedList(values: Optional[Iterable[pyruler.linked_list.T]] = None)

Bases: Generic[pyruler.linked_list.T], Sized, Iterable, abc.ABC

LinkedList class.

Parameters

values – Optional iterable object to initialize LinkedList object nodes

add(value: pyruler.linked_list.T)NoReturn

This is an alias for add_last method. :param value: Value to be added at the end of the list

add_first(value: pyruler.linked_list.T)NoReturn

Add new node at the beginning of the list.

add_last(value: pyruler.linked_list.T)NoReturn

Add new node at the end of the list. :param value: New list value

add_many(values: Iterable[pyruler.linked_list.T])NoReturn

Add many items to the list at ones with the add_last method. :param values: Iterable object with values to be added to the LinkedList

empty()bool

Return a boolean assertion if the list is empty. :return bool: Assertion

first()pyruler.linked_list.T

Return first element of the list. :return T: First element value

last()pyruler.linked_list.T

return the last element of the list :return T: Last element value

class pyruler.linked_list.ListNode(value: Any)

Bases: object

Single list node class.