pypyr.steps.default permalink

initialize the context with default values permalink

Sets values in context if they do not exist already. Does not overwrite existing values. Supports nested hierarchies.

This is especially useful for setting default values in context, for example when using optional arguments from the cli.

This step sets the contents of the context key defaults into context where keys in defaults do not exist in context already. The contents of the defaults key must be a dictionary.

By comparison, the in step decorator, and the steps contextcopy, contextsetf and contextmerge overwrite values even if they are in context already.

The recursive if-not-exists-then-set check happens for dictionaries, but not for items in Lists, Sets and Tuples. You can set default values of type List, Set or Tuple if their keys don’t exist in context already, but this step will not recurse through the List, Set or Tuple itself.

Supports substitutions. String interpolation applies to keys and values.

example permalink

Given a context like this:

key1: value1
key2:
    key2.1: value2.1
key3: None

And defaults input like this:

key1: updated value here won't overwrite since it already exists
key2:
    key2.2: value2.2
key3: key 3 exists so I won't overwrite

Will result in context:

key1: value1
key2:
    key2.1: value2.1
    key2.2: value2.2
key3: None

See a worked example for setting default values.

see also

last updated on .