Permitted alphabet constraint

class pyasn1.type.constraint.PermittedAlphabetConstraint(*alphabet)

Create a PermittedAlphabetConstraint object.

The PermittedAlphabetConstraint satisfies any character string for as long as all its characters are present in the set of permitted characters.

Objects of this type are iterable (emitting constraint values) and can act as operands for some arithmetic operations e.g. addition and subtraction.

The PermittedAlphabetConstraint object can only be applied to the character ASN.1 types such as IA5String.

Parameters

*alphabet (str) – Full set of characters permitted by this constraint object.

Example

class BooleanValue(IA5String):
    '''
    ASN.1 specification:

    BooleanValue ::= IA5String (FROM ('T' | 'F'))
    '''
    subtypeSpec = PermittedAlphabetConstraint('T', 'F')

# this will succeed
truth = BooleanValue('T')
truth = BooleanValue('TF')

# this will raise ValueConstraintError
garbage = BooleanValue('TAF')

ASN.1 FROM … EXCEPT … clause can be modelled by combining multiple PermittedAlphabetConstraint objects into one:

Example

class Lipogramme(IA5String):
    '''
    ASN.1 specification:

    Lipogramme ::=
        IA5String (FROM (ALL EXCEPT ("e"|"E")))
    '''
    subtypeSpec = (
        PermittedAlphabetConstraint(*string.printable) -
        PermittedAlphabetConstraint('e', 'E')
    )

# this will succeed
lipogramme = Lipogramme('A work of fiction?')

# this will raise ValueConstraintError
lipogramme = Lipogramme('Eel')

Note

Although ConstraintsExclusion object could seemingly be used for this purpose, practically, for it to work, it needs to represent its operand constraints as sets and intersect one with the other. That would require the insight into the constraint values (and their types) that are otherwise hidden inside the constraint object.

Therefore it’s more practical to model EXCEPT clause at PermittedAlphabetConstraint level instead.