YAML Tags Reference#
This document provides a comprehensive reference for all YAML tags provided by yayaml.
These tags allow you to create Python objects directly within YAML files.
Built-in Function Tags#
These tags apply Python built-in functions to their arguments.
Aggregation Functions#
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Returns |
|
|
|
Returns |
|
|
|
Returns the minimum value |
|
|
|
Returns the maximum value |
|
|
|
Returns the sum of elements |
|
|
|
Returns the product of elements |
|
|
|
Returns sorted list (ascending) |
|
|
|
Returns sorted list (descending) |
|
|
|
Returns length of sequence/mapping |
|
|
Note on !len syntax: The !len tag requires a wrapper list around its argument. This enables composability with nested constructors:
# Length of a plain list (note the double brackets)
list_len: !len [[1, 2, 3]] # 3
# Length of a dict
dict_len: !len [{a: 1, b: 2}] # 2
# Length of a string
str_len: !len [foobar] # 6
# Works with nested constructors
range_len: !len [!range 10] # 10
sorted_len: !len [!sorted [3, 1, 2]] # 3
Scalar Functions#
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Absolute value |
|
|
|
Convert to integer |
|
|
|
Round to nearest integer |
|
|
Operator Tags#
These tags provide access to Python operators.
Arithmetic Operators#
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Addition ( |
|
|
|
Subtraction ( |
|
|
|
Multiplication ( |
|
|
|
True division ( |
|
|
|
Floor division ( |
|
|
|
Modulo ( |
|
|
|
Power ( |
|
|
|
Negation ( |
|
|
The !pow tag also supports a third argument for modular exponentiation:
pow_mod: !pow [2, 4, 3] # pow(2, 4, 3) = 16 % 3 = 1
Logical Operators#
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Logical NOT |
|
|
|
Bitwise AND |
|
|
|
Bitwise OR |
|
|
|
Bitwise XOR |
|
|
|
Bitwise inversion ( |
|
|
Comparison Operators#
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Less than ( |
|
|
|
Less than or equal ( |
|
|
|
Equal ( |
|
|
|
Not equal ( |
|
|
|
Not equal (alias for |
|
|
|
Greater than or equal ( |
|
|
|
Greater than ( |
|
|
Container Operators#
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Check membership ( |
|
|
|
Concatenate sequences |
|
|
String Tags#
These tags provide string manipulation capabilities.
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
String formatting |
|
|
|
Join strings |
|
|
|
Split string |
|
|
|
Collapse whitespace |
|
|
|
Collapse whitespace (same as |
|
|
Format Examples#
The !format tag supports both positional and keyword arguments:
# Positional arguments
greeting: !format ["{} is not {}", foo, bar] # "foo is not bar"
# Keyword arguments via mapping
message: !format
fstr: "{name}: {value}"
name: fish
value: spam
# Result: "fish: spam"
# Nested formatting
stats: !format
fstr: "results: {stats[mean]:.2f} +/- {stats[std]:.2f}"
stats:
mean: 1.632
std: 0.026
# Result: "results: 1.63 +/- 0.03"
Type Tags#
These tags create Python type objects.
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Create slice object |
|
|
|
Create range object |
|
|
|
Deep copy value |
|
|
Slice Syntax#
slices:
- !slice 5 # slice(5) - equivalent to [:5]
- !slice [5] # slice(5)
- !slice [0, ~] # slice(0, None) - equivalent to [0:]
- !slice [~, 0] # slice(None, 0) - equivalent to [:0]
- !slice [0, 10, 2] # slice(0, 10, 2) - equivalent to [0:10:2]
Range Syntax#
ranges:
- !range 10 # range(10)
- !range [10] # range(10)
- !range [5, 10] # range(5, 10)
- !range [5, 10, 2] # range(5, 10, 2)
List Generation Tags#
!listgen#
Generate lists with various options:
# Simple range-based generation
simple: !listgen [0, 10, 2] # [0, 2, 4, 6, 8]
# Advanced options via mapping
advanced: !listgen
from_range: [0, 10, 3]
unique: true
append: [100]
remove: [0]
sort: true
# Result: [3, 6, 9, 100]
NumPy Tags#
These tags create NumPy arrays or lists of values.
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Create NumPy array |
|
|
|
Evenly spaced values |
|
|
|
Logarithmically spaced |
|
|
|
Values with step |
|
|
Note: !linspace, !logspace, and !arange return Python lists (not NumPy arrays) for better YAML serialization.
Conditional Tags#
These tags allow conditional value selection.
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Conditional selection |
|
|
|
Unix platform check |
|
|
|
Windows platform check |
|
|
Combining with Operators#
Conditionals can be combined with comparison operators:
# Check if 8 != 9
result: !if-else [!ne [8, 9], "8 is not 9", "8 is 9"]
# Result: "8 is not 9"
Environment Variable Tags#
These tags read environment variables.
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Get env var (required) |
|
Value of |
|
Alias for |
|
Value of |
|
Get env var as boolean |
|
|
|
Alias for |
|
|
With Fallback Values#
# With fallback (returns fallback if var not set)
username: !getenv [USER, "unknown_user"]
# Boolean env vars (parses strings like "true", "1", "yes", "on")
debug_mode: !getboolenv [DEBUG, "false"]
Path Tags#
These tags help with file path manipulation.
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Expand |
|
|
|
Join path components |
|
|
Path Examples#
# Expand user home directory
config_path: !expanduser ~/.config/myapp
# Join path components
data_file: !joinpath
- "~"
- data
- output
- results.csv
# Result: "~/data/output/results.csv"
Expression Tag#
The !expr tag evaluates simple mathematical expressions:
expressions:
simple: !expr 1 + 2 * 3 # 7
power: !expr 2**4 # 16
parens: !expr (2 + 3) * 4 # 20
scientific: !expr 1e-10 # 0.0000000001
infinity: !expr inf # float('inf')
nan: !expr NaN # float('nan')
Supported operations:
Basic arithmetic:
+,-,*,/,**Parentheses for grouping
Scientific notation:
1e10,1E-5Special values:
inf,-inf,NaN
Utility Tags#
Tag |
Description |
Example |
Result |
|---|---|---|---|
|
Convert string to bool |
|
|
|
Deep copy any value |
|
Copy of anchored value |
Boolean String Values#
The following strings are recognized as boolean values by !str-to-bool and !getboolenv:
True:
true,yes,on,1False:
false,no,off,0
(Case-insensitive)