Skip to main content
Version: 0.27.0

Advanced Expressions

This page continues Values, Variables & Expressions, which covers the everyday cases.

The field also accepts calculations, text manipulation, date handling and conditions. The notation is Python; variables are still marked with $.

Reaching into data​

When a variable holds a list or a dictionary, square brackets get you inside. After you type [, the auto-completer offers the keys or positions directly.

You typeResult
$items[0]apples — first element, counting from zero
$items[-1]plums — last element
$record['state']OK — value by key
$record['id']7
$name[0:3]Jan — first three characters

The same notation slices a list.

Working with text​

Write a dot after the variable and the name of an operation:

You typeResult
$name.upper()JAN NOVÁK
$text.strip()Invoice 2024 — trims leading and trailing spaces
$name.replace('Novák', 'Nový')Jan Nový
$date.split('.')['01', '02', '2024']
", ".join($items)apples, pears, plums
$text.strip().lower()invoice 2024 — operations chain

Functions​

The General and Math tabs of the auto-completer list what is available, each with a description and an example.

You typeResult
len($items)3 — number of elements
len($name)9 — number of characters
int($amount)1250
str($count)3 — as text
round($amount)1250
max($count, 10)10 — larger of the two; min likewise
sum([1, 2, 3])6
abs(-5)5

Lists, dictionaries and tuples​

Useful when you need to pass several values at once — into a form, or on to another action.

You typeResult
[$name, $count]['Jan Novák', 3] — list
{id:$count, name:$name}{'id': 3, 'name': 'Jan Novák'} — dictionary
($count, $amount)(3, 1250.5) — tuple

Key names need no quotes. After reopening you see them quoted — {'id':$count, …} — which is the same thing.

warning

Mind the difference: $count, $amount with nothing else is a tuple, whereas We have $count for $amount CZK is text.

Date and time​

A date stored as text must first be parsed with datetime_parse and then rendered with datetime_format.

You typeResult
datetime_parse($date, '%d.%m.%Y')the date read from the text
datetime_format(datetime_parse($date, '%d.%m.%Y'), '%Y-%m-%d')2024-02-01
datetime_format(datetime.datetime.now(), '%d.%m.%Y')today's date
datetime_format(datetime_add(datetime.datetime.now(), days=7), '%d.%m.%Y')the date a week from now
HelperWhat it takes
datetime_parse(text, format, tz=None)Reads a date out of text.
datetime_format(date, format)Renders a date as text.
datetime_add(date, …)Shifts a date by days, hours, minutes, seconds, microseconds, months or years.
datetime_set(date, …)Sets a specific part — day, month, year, hour, minute, second, microsecond.

Format codes: %d day, %m month, %Y four-digit year, %H hour, %M minute.

Constants and system values​

The Constants tab holds ready-made values; picking one inserts the whole expression.

You typeResult
datetime.datetime.now()current date and time
int(datetime.datetime.now().timestamp())seconds since the epoch, e.g. 1785841813
random.randint(0, 1000000)a random number
$CONSTANTS['FULL_DATE']a scenario-wide constant — see the full list
chr(10)a line break — offered in the tab as newline
chr(9)a tab character — offered in the tab as tab

Modules​

Six modules are available: re, math, random, datetime, pathlib and aiviro.

You typeResult
re.sub('[0-9]', '', $text)' Invoice ' — strips all digits
math.ceil($amount)1251 — rounds up
random.randint(1, 100)a random number from 1 to 100

Number bases​

You typeResult
bin($count)0b11
hex($count)0x3
oct($count)0o3
int("1100", 2)12 — from binary text back to a number

Operators​

Every operator below is also in the Operators tab of the auto-completer, with a description.

Arithmetic​

You typeResultMeaning
$amount + $count1253.5add
$amount - $count1247.5subtract
$amount * $count3751.5multiply
$amount / $count416.833…divide — result is a decimal
$amount // $count416.0integer division
$amount % $count2.5remainder — handy for odd/even tests
$count ** 29power

Joining and repeating​

+ and * work on text and lists too — there they join and repeat rather than add.

You typeResult
$items + ["berries"]['apples', 'pears', 'plums', 'berries']
"-" * 20-------------------- — a separator

You do not need + to build text from variables — just write the sentence and mark the variables with $.

Comparison​

Used mostly in Condition and loop actions, where the Editor asks "should this run?". The answer is True or False.

You typeResultMeaning
$amount > 1000Truegreater than; < is less than
$count <= 3Trueless than or equal; >= likewise
$record['state'] == "OK"Trueequals — note the two equals signs
$name != "Petr"Truedoes not equal

Logic​

You typeResultMeaning
$amount > 1000 and $count > 1Trueboth must hold
$amount > 5000 or $count > 1Trueat least one must hold
not $noteTruenegation — holds when the value is empty
$count > 1 and $count < 10Truea range

Empty text, zero and an empty list count as "no"; anything filled in counts as "yes".

Contains / is inside​

You typeResult
"apples" in $itemsTrue
"bananas" not in $itemsTrue
"state" in $recordTrue — does the dictionary have that key?
"Novák" in $nameTrue — substring search in text
$note is NoneFalse — empty text is not the same as None

Two useful tricks​

You typeResult
$note or "not filled in"not filled in — a fallback when the variable is empty
len($items) > 2True — a condition built on a function's result

One-line conditions​

For a simple "if yes then A, otherwise B", write the condition straight into the field — typically in Create variable.

You typeResult
"VIP" if $amount > 1000 else "standard"VIP
$name if $name else "unknown customer"Jan Novák, or unknown customer if $name were empty

Transforming and filtering a list​

One line can walk a list and change or filter each item.

You typeResult
[i.upper() for i in $items]['APPLES', 'PEARS', 'PLUMS']
[i for i in $items if i != 'pears']['apples', 'plums']
len([i for i in $items if i != 'pears'])2

i is just a helper name for one item — it takes no $.

Without the square brackets the same construction is a generator expression. It is useful inside functions such as any(), all() or sum(), which only need to look at the items one by one:

You typeResult
any(i.endswith('s') for i in $items)True — at least one item ends with s
sum(len(i) for i in $items)16 — the item names have 16 letters in total

F-strings​

An f-string is text with values substituted in, what used to be written as f"text {variable}". You no longer need to write it: type the text and mark variables with $, and the Editor builds the f-string itself.

You typeResult
Record $name cannot be processedRecord Jan Novák cannot be processed
f"Hello {name}"Hello Jan Novák

Written by hand it works too. Inside the braces, though, no $ — {name} is right, {$name} is not. After reopening you see the simpler form, Hello $name.

Older scenarios containing f-strings keep working and open correctly.


When to reach for Python Code instead​

The value field is meant for one value on one line. As soon as you need several steps in sequence, loops, error handling or longer logic, use the Python Code action — there you get a full editor and the code stays readable.