yaes

Yet Another Expansion Syntax (pronounced ‘Yasssss Kweeeeen’) for expanding complex data (YAML / JSON) with Jinja2 templating

yaes.each(blocks, values: dict, env=None)

Short hand each function for basic usage

Go through blocks, iterating and checking conditions, yield blocks that pass

Parameters
  • blocks (dict or list) – blocks to evaulate

  • values (dict) – values to evaluate with

  • env (Jinja2.Environment) – optional Jinja2.Environment to use for transformations

Returns

Passing blocks

Return type

Iterator

Usage

import yaes

values = {
    "a": 1,
    "cs": [2, 3],
    "ds": "nuts"
}

block = {
    "transpose": {
        "b": "a"
    },
    "iterate": {
        "c": "cs",
        "d": "ds"
    },
    "condition": "{{ c != 3 and d != 't' }}",
    "values": {"L": 7}
}

list(yaes.each(block, values))
# [
#     (block, {"a": 1, "cs": [2, 3], "ds": "nuts", "b": 1, "c": 2, "d": "n", "L": 7}),
#     (block, {"a": 1, "cs": [2, 3], "ds": "nuts", "b": 1, "c": 2, "d": "u", "L": 7}),
#     (block, {"a": 1, "cs": [2, 3], "ds": "nuts", "b": 1, "c": 2, "d": "s", "L": 7})
# ]

block = {
    "requires": "a",
}

list(yaes.each(block, {}))
# []
class yaes.Engine(env=None)

Class for expanding complex data (YAML / JSON) with Jinja2 templating

Parameters

env (jinja2.Environment) – optional jinja2 Environment to use with transform

env: jinja2.Environment

Jinja2 environment

condition(block: dict, values: dict) bool

Evaludates condition in values

It’s best to use ‘{?’ and ‘?}’ as conditions with straight Jinja2 with ‘{{’ and ‘}}’ will be deprecated.

Parameters
  • block (dict) – block to evaulate

  • values (dict) – values to evaluate with

Returns

The evaluated condition

Return type

bool

Usage

import yaes

engine = yaes.Engine()

engine.condition({}, {})
# True

block = {
    "condition": "{{ a == 1 }}"
}

engine.condition(block, {"a": 1})
# True

engine.condition(block, {"a": 2})
# False

block = {
    "condition": "{? a == 1 ?}"
}

engine.condition(block, {"a": 1})
# True

engine.condition(block, {"a": 2})
# False
each(blocks, values: dict)

Go through blocks, iterating and checking conditions, yield blocks that pass

Parameters
  • blocks (dict or list) – blocks to evaulate

  • values (dict) – values to evaluate with

Returns

Passing blocks

Return type

Iterator

Usage

import yaes

engine = yaes.Engine()

values = {
    "a": 1,
    "cs": [2, 3],
    "ds": "nuts"
}

block = {
    "transpose": {
        "b": "a"
    },
    "iterate": {
        "c": "cs",
        "d": "ds"
    },
    "condition": "{{ c != 3 and d != 't' }}",
    "values": {"L": 7}
}

list(engine.each(block, values))
# [
#     (block, {"a": 1, "cs": [2, 3], "ds": "nuts", "b": 1, "c": 2, "d": "n", "L": 7}),
#     (block, {"a": 1, "cs": [2, 3], "ds": "nuts", "b": 1, "c": 2, "d": "u", "L": 7}),
#     (block, {"a": 1, "cs": [2, 3], "ds": "nuts", "b": 1, "c": 2, "d": "s", "L": 7})
# ]

block = {
    "requires": "a",
}

list(engine.each(block, {}))
# []
iterate(block: dict, values: dict) list

Iterates values with transposition

Parameters
  • block (dict) – block to evaulate

  • values (dict) – values to evaluate with

Returns

The list of blocks iterated

Return type

list

Usage

import yaes

engine = yaes.Engine()

values = {
    "a": 1,
    "cs": [2, 3],
    "ds": "nuts"
}

engine.iterate({}, values)
# [{}]

block = {
    "transpose": {
        "b": "a"
    },
    "iterate": {
        "c": "cs",
        "d": "ds"
    }
}

engine.iterate(block, values)
# [
#     {
#         "b": 1,
#         "c": 2,
#         "d": "n"
#     },
#     {
#         "b": 1,
#         "c": 2,
#         "d": "u"
#     },
#     {
#         "b": 1,
#         "c": 2,
#         "d": "t"
#     },
#     {
#         "b": 1,
#         "c": 2,
#         "d": "s"
#     },
#     {
#         "b": 1,
#         "c": 3,
#         "d": "n"
#     },
#     {
#         "b": 1,
#         "c": 3,
#         "d": "u"
#     },
#     {
#         "b": 1,
#         "c": 3,
#         "d": "t"
#     },
#     {
#         "b": 1,
#         "c": 3,
#         "d": "s"
#     }
# ]
requires(block: dict, values: dict) bool

Determines whether values are set to process a block

Parameters
  • block (dict) – block to evaulate

  • values (dict) – values to evaluate with

Return type

bool

Usage

import yaes

engine = yaes.Engine()

engine.requires({}, {})
# True

block = {
    "requires": "a"
}

engine.requires(block, {"a": 1})
# True

engine.requires(block, {})
# False

block = {
    "requires": ["a__b", "{[ a__b ]}"]
}

engine.requires(block, {})
# False

engine.requires(block, {"a": {"b": "c"}})
# False

engine.requires(block, {"a": {"b": "c"}, "c": "yep"})
# True
transform(template, values: dict)

Renders a Jinja2 template using values sent

If the template is a str and is enclosed by ‘{?’ and ‘?}’, it will render the template but evaluate as a bool.

If the template is a str and is enclosed by ‘{[’ and ‘]}’, it will lookup the value in valuue using overscore notation.

Else if the tempalte is a str, it will render the template in the standard Jinja2 way.

If the template is a list, it will recurse and render each item.

If the template is a dict, it will recurse each key and render each item.

Else return the template as is.

Parameters
  • template (bool or str or list or dict) – template to use

  • values (dict) – values to use with the template

Returns

The rendered value

Usage

import yaes

engine = yaes.Engine()

engine.transform("{{ a }}", {"a": 1})
# '1'

engine.transform(["{{ a }}"], {"a": 1})
# ['1']

engine.transform({"b": "{{ a }}"}, {"a": 1})
# {"b": '1'}

engine.transform("{{ a == 1 }}", {"a": 1})
# 'True'

engine.transform("{{ a != 1 }}", {"a": 1})
# 'False'

engine.transform(True, {})
# True

engine.transform(False, {})
# False

engine.transform("{? 1 == 1 ?}", {})
# True

engine.transform("{? 1 == 0 ?}", {})
# False

engine.transform("{[ a__b ]}", {})
# None

engine.transform("{[ a__b ]}", {"a": {"b": 3}})
# 3
static transpose(block: dict, values: dict) dict

Transposes values, allows for the same value under a different name

Parameters
  • block (dict) – block to evaulate

  • values (dict) – values to evaluate with

Returns

The new values block transposed

Return type

dict

Usage

import yaes

engine = yaes.Engine()

engine.transpose({"transpose": {"b": "a"}}, {"a": 1})
# {"b": 1}