Builtin Symbols

The following symbols are provided by default using the from_defaults() method. These symbols can be accessed through the $ prefix, e.g. $pi. The default values can be overridden by defining a custom subclass of Context and setting the builtins attribute.

Name

Data Type

Value Description

Math related

e

FLOAT

The mathematical constant e (2.71828…).

pi

FLOAT

The mathematical constant pi (3.14159…).

Timestamp related

now

DATETIME

The current timestamp (including time) using the default timezone from default_timestamp

today

DATETIME

The current timestamp, (excluding time) using the default timezone from default_timestamp

Defining Custom Values

To remove the default builtins described above, simply initialize a Builtins instance with a values of an empty dictionary. This will remove all builtin values, and the dictionary can optionally be populated with alternative values.

To add additional values, use the from_defaults constructor, with a values dictionary. In this case, values will optionally override any of the default settings, and keys which do not overlap will be added in addition to the default builtin symbols.

class CustomBuiltinsContext(rule_engine.Context):
    def __init__(self, *args, **kwargs):
        # call the parent class's __init__ method first to set the
        # default_timezone attribute
        super(CustomBuiltinsContext, self).__init__(*args, **kwargs)
        self.builtins = rule_engine.engine.Builtins.from_defaults(
            # expose the $version symbol
            {'version': rule_engine.__version__},
            # use the specified default timezone
            timezone=self.default_timezone
        )