Module

environment.tests

Built-in tests for Kida templates.

Tests are boolean predicates used withisin conditionals: {% if value is test %}or{% if value is test(arg) %}

Categories:

Type Tests:

  • defined: Value is not None
  • undefined: Value is None
  • none: Value is None (alias)
  • string: Value is a string
  • number: Value is int or float (not bool)
  • sequence: Value is list, tuple, or string
  • mapping: Value is a dict
  • iterable: Value supports iteration
  • callable: Value is callable

Boolean Tests:

  • true: Value is exactly True
  • false: Value is exactly False

Number Tests:

  • odd: Integer is odd
  • even: Integer is even
  • divisibleby(n): Integer is divisible by n

Comparison Tests:

  • eq(other)/equalto(other): Equal to other
  • ne(other): Not equal to other
  • lt(other)/lessthan(other): Less than other
  • le(other): Less than or equal
  • gt(other)/greaterthan(other): Greater than other
  • ge(other): Greater than or equal
  • sameas(other): Identity comparison (is)
  • in(seq): Value is in sequence

String Tests:

  • lower: String is all lowercase
  • upper: String is all uppercase

Negation:

Useis notfor negated tests: {% if user is not defined %}or{% if count is not even %}

Example:

```jinja
{% if posts is defined and posts is iterable %}
    {% for post in posts %}
        {% if loop.index is odd %}
            <div class="odd">{{ post.title }}</div>
        {% endif %}
    {% endfor %}
{% endif %}
```

Custom Tests:

>>> env.add_test('prime', lambda n: n > 1 and all(n % i for i in range(2, n)))
>>> # {% if 17 is prime %}Yes{% endif %}

Functions

_apply_test 2 bool
Apply a test to a value.
def _apply_test(value: Any, test_name: str, *args: Any) -> bool
Parameters
Name Type Description
value Any
test_name str
Returns
bool
_test_callable 1 bool
Test if value is callable.
def _test_callable(value: Any) -> bool
Parameters
Name Type Description
value Any
Returns
bool
_test_defined 1 bool
Test if value is defined (not None).
def _test_defined(value: Any) -> bool
Parameters
Name Type Description
value Any
Returns
bool
_test_divisible_by 2 bool
Test if value is divisible by num.
def _test_divisible_by(value: int, num: int) -> bool
Parameters
Name Type Description
value int
num int
Returns
bool
_test_eq 2 bool
Test equality.
def _test_eq(value: Any, other: Any) -> bool
Parameters
Name Type Description
value Any
other Any
Returns
bool
_test_even 1 bool
Test if value is even.
def _test_even(value: int) -> bool
Parameters
Name Type Description
value int
Returns
bool
_test_ge 2 bool
Test greater than or equal.
def _test_ge(value: Any, other: Any) -> bool
Parameters
Name Type Description
value Any
other Any
Returns
bool
_test_gt 2 bool
Test greater than.
def _test_gt(value: Any, other: Any) -> bool
Parameters
Name Type Description
value Any
other Any
Returns
bool
_test_in 2 bool
Test if value is in sequence.
def _test_in(value: Any, seq: Any) -> bool
Parameters
Name Type Description
value Any
seq Any
Returns
bool
_test_iterable 1 bool
Test if value is iterable.
def _test_iterable(value: Any) -> bool
Parameters
Name Type Description
value Any
Returns
bool
_test_le 2 bool
Test less than or equal.
def _test_le(value: Any, other: Any) -> bool
Parameters
Name Type Description
value Any
other Any
Returns
bool
_test_lower 1 bool
Test if string is lowercase.
def _test_lower(value: str) -> bool
Parameters
Name Type Description
value str
Returns
bool
_test_lt 2 bool
Test less than.
def _test_lt(value: Any, other: Any) -> bool
Parameters
Name Type Description
value Any
other Any
Returns
bool
_test_mapping 1 bool
Test if value is a mapping.
def _test_mapping(value: Any) -> bool
Parameters
Name Type Description
value Any
Returns
bool
_test_ne 2 bool
Test inequality.
def _test_ne(value: Any, other: Any) -> bool
Parameters
Name Type Description
value Any
other Any
Returns
bool
_test_none 1 bool
Test if value is None.
def _test_none(value: Any) -> bool
Parameters
Name Type Description
value Any
Returns
bool
_test_number 1 bool
Test if value is a number.
def _test_number(value: Any) -> bool
Parameters
Name Type Description
value Any
Returns
bool
_test_odd 1 bool
Test if value is odd.
def _test_odd(value: int) -> bool
Parameters
Name Type Description
value int
Returns
bool
_test_sequence 1 bool
Test if value is a sequence.
def _test_sequence(value: Any) -> bool
Parameters
Name Type Description
value Any
Returns
bool
_test_string 1 bool
Test if value is a string.
def _test_string(value: Any) -> bool
Parameters
Name Type Description
value Any
Returns
bool
_test_upper 1 bool
Test if string is uppercase.
def _test_upper(value: str) -> bool
Parameters
Name Type Description
value str
Returns
bool
_test_match 2 bool
Test if string matches regex pattern. Used by rejectattr/selectattr for filter…
def _test_match(value: Any, pattern: str) -> bool

Test if string matches regex pattern.

Used by rejectattr/selectattr for filtering by regex pattern.

Parameters
Name Type Description
value Any
pattern str
Returns
bool