Supported YAML Tags¶
YAML allows defining custom so-called tags which can be distinguished during loading and serialization of objects.
paramspace
makes heavy use of this possibility, as it greatly simplifies the definition and usage of configuration files.
Python builtins and basic operators¶
paramspace.yaml
adds YAML constructors for a number of frequently used Python built-in functions and operators.
Having these available while specifying configurations can make the definition of configurations files more versatile.
Warning
The YAML tags provided here are only meant to allow basic operations, i.e. summing two parameters to create a third. Don’t overdo it. Configuration files should remain easy to read.
The tags shown below call the equivalent Python builtin or the operators defined in the operator
Python module.
Example:
any: !any [false, 0, true] # == True
all: !all [true, 5, 0] # == False
abs: !abs -1 # +1
int: !int 1.23 # 1
round: !round 9.87 # 10
sum: !sum [1, 2, 3] # 6
prod: !prod [2, 3, 4] # 24
min: !min [1, 2, 3] # 1
max: !max [1, 2, 3] # 3
sorted: !sorted [2, 1, 3] # [1, 2, 3]
isorted: !isorted [2, 1, 3] # [3, 2, 1]
# Operators
add: !add [1, 2] # 1 + 2
sub: !sub [2, 1] # 2 - 1
mul: !mul [3, 4] # 3 * 4
mod: !mod [3, 2] # 3 % 2
pow: !pow [2, 4] # 2 ** 4
truediv: !truediv [3, 2] # 3 // 2
floordiv: !floordiv [3, 2] # 3 / 2
pow_mod: !pow [2, 4, 3] # 2 ** 4 % 3
not: !not [true]
and: !and [true, false]
or: !or [true, false]
xor: !xor [true, true]
lt: !lt [1, 2] # 1 < 2
le: !le [2, 2] # 2 <= 2
eq: !eq [3, 3] # 3 == 3
ne: !ne [3, 1] # 3 != 1
ge: !ge [2, 2] # 2 >= 2
gt: !gt [4, 3] # 4 > 3
negate: !negate [1] # -1
invert: !invert [true] # ~true
contains: !contains [[1,2,3], 4] # 4 in [1,2,3] == False
concat: !concat [[1,2,3], [4,5], [6,7,8]] # […]+[…]+[…]+…
format1: !format ["{} is not {}", foo, bar]
format2: !format
fstr: "{some_key:}: {some_value:}"
some_key: fish
some_value: spam
format3: !format
fstr: "results: {stats[mean]:.2f} ± {stats[std]:.2f}"
stats:
mean: 1.632
std: 0.026