Create your own type

A variable has a type. This type enables the variable to define the values that are accepted by this variable.

There is a series of default types, but obviously not all cases are taken.

It’s possible to create your own type.

Here an example to a lipogram option (in a string, we cannot use “e” character):

the lipogram.py file content
from tiramisu import StrOption
class LipogramOption(StrOption):
    __slots__ = tuple()
    _type = 'lipogram'

    def validate(self,
                 value):
        super().validate(value)
        # verify that there is any 'e' in the sentense
        if 'e' in value:
            raise ValueError('Perec wrote a book without any "e", you could not do it in a simple sentence?')

To add the new lipogram type in Rougail:

>>> from rougail import Rougail, RougailConfig
>>> RougailConfig['main_structural_directories'] = ['dict']
>>> RougailConfig['custom_types']['lipogram'] = LipogramOption

Now, we can use lipogram type. Here is a dict/00-base.yml structure file:

---
version: '1.1'
var:
  type: lipogram
>>> rougail = Rougail()
>>> config = rougail.get_config()
>>> config.option('rougail.var').value.set('blah')
>>> config.option('rougail.var').value.set('I just want to add a quality string that has no bad characters')
[...]
tiramisu.error.ValueOptionError: "I just want to add a quality string that has no bad characters" is an invalid lipogram for "var", Perec wrote a book without any "e", you could not do it in a simple sentence?