pypyr.steps.contextmerge permalink

merging context values permalink

Merges values into context, preserving the existing hierarchy while only updating the differing values as specified in the contextMerge input.

By comparison, contextcopy and set overwrite the destination hierarchy that is in context already.

This step merges the contents of the context key contextMerge into context. The contents of the contextMerge key must be a dictionary.

examples permalink

For example, say input context is:

key1: value1
key2: value2
key3:
    k31: value31
    k32: value32
contextMerge:
    key2: 'aaa_{key1}_zzz'
    key3:
        k33: value33_{key1}
    key4: 'bbb_{key2}_yyy'

This will result in return context:

key1: value1
key2: aaa_value1_zzz
key3:
    k31: value31
    k32: value32
    k33: value33_value1
key4: bbb_aaa_value1_zzz_yyy

See a worked example for contextmerge.

merging collections like lists, dicts, sets & tuples permalink

List, Set and Tuple merging is purely additive, with no checks for uniqueness or already existing list items. E.g context [0,1,2] with contextMerge [2,3,4] will result in [0,1,2,2,3,4].

Keep this in mind especially where complex types like dicts nest inside a list - a merge will always add a new dict list item, not merge it into whatever dicts might exist on the list already.

see also

last updated on .