Annotated Constraints

The constraint module implements the following classes:

  • Constraint: Abstract class representing annotated YANG constraints.

  • Intervals: Class representing a sequence of numeric intervals.

  • Pattern: Class representing regular expression pattern.

  • Must: Class representing the constraint specified by a “must” statement.

Type Aliases

yangson.constraint.Number

Union of numeric classes appearing in interval constraints.

yangson.constraint.Interval

Numeric interval implemented as a list containing either a single number or a pair of numbers (lower and upper bound).

class yangson.constraint.Constraint(error_tag: str | None, error_message: str | None)

Abstract class for annotated YANG constraints, i.e. those for which a custom error tag and error message can be defined.

The constructor arguments initialize the instance attributes.

Instance Attributes

error_tag

String tag indicating violation of the constraint. A custom error tag can be defined by the error-app-tag statement, see sec. 7.5.4.2 in [RFC7950]. If the value is None, then some predefined error tag is used in an exception trigerred by violating the constraint.

error_message

Text describing the error condition connected to violation of the constraint. A custom error message can be defined by the error-message statement, see sec. 7.5.4.1 in [RFC7950]. If the value is None, then some predefined error message, which may be empty, is used in an exception trigerred by violating the constraint.

class yangson.constraint.Intervals(intervals: List[Interval], parser: Callable[[str], Number | None] = None, error_tag: str = None, error_message: str = None)

This class is a subclass of Constraint. It represents a sequence of intervals that restrict a numeric leaf value or length of a string, usually specified by the statements range and length, respectively (see sections 9.2.4 and 9.4.4 in [RFC7950]).

The constructor arguments initialize the instance attributes. If parser is None (default value), then a parser for dekadic integers is used.

>>> iints = Intervals([[0, 10]], error_tag="out-of-range")

Instance Attributes

intervals

A list of numeric intervals.

parser

A function that receives a string as the only argument and returns the corresponding numeric value of the appropriate type, or None if parsing fails.

Public Methods

__contains__(value: Number) bool

Return True if value is contained in one of the receiver’s intervals, otherwise return False.

This method enables the Python operators in and not in for use with instances of this class.

>>> 5 in iints
True
__str__() str

Return string representation of the receiver.

>>> str(iints)
'0..10'
restrict_with(expr: str, error_tag: str = None, error_message: str = None) None

Restrict the receiver with range expression expr. Each of the other two arguments, if specified and not equal to None, replaces the value of the corresponding instance attribute.

This method raises InvalidArgument if expr is not a valid range expression.

>>> iints.restrict_with('2..4|6|8..max')
>>> str(iints)
'2..4 | 6 | 8..10'
class yangson.constraint.Pattern(pattern: str, invert_match: bool = False, error_tag: str = None, error_message: str = None)

This class is a subclass of Constraint. It represents a constraint defined by the regular expression pattern, usually specified by the pattern statement (sec. 9.4.5 in [RFC7950]).

The remaining constructor arguments initialize the instance attributes.

The constructor raises InvalidArgument if pattern is not a valid YANG regular expression pattern.

Instance Attributes

regex

This attribute contains the Python regular expression (see module re) translated from the constructor argument pattern.

invert_match

This is a modifier that reverses the meaning of the pattern matching constraint: it is satisfied if a given string does not match the pattern. This modifier is usually specified by the statement

modifier invert-match;
>>> pat = Pattern('[A-Z][a-z]*')
>>> pat.regex.search('Yangson').group()
'Yangson'
>>> pat.regex.search('iPhone') is None
True

Note that the string in the last example doesn’t match pat because YANG patterns are implicitly “anchored” – in most other flavours of regular expressions the anchoring has to be specified explicitly with special symbols ^ and $.

class yangson.constraint.Must(expression: Expr, error_tag: str = None, error_message: str = None)

This class is a subclass of Constraint. It represents a constraint defined by the must statement (sec. 7.5.3 in [RFC7950]).

The constructor arguments initialize the instance attributes.

Instance Attributes

expression

A compiled XPath expression, i.e. an instance of the xpathast.Expr class.