fmt

Formatting functions/classes

sub

sub_value

The elevaso_spine.fmt.sub.sub_value() function substitute placeholder patterns with values based on the provided dictionary of values. The placeholder pattern defaults to ${NAME} where NAME is the placeholder name. If the variable type is a collection (list, set, tuple, dict) the function will call the elevaso_spine.itr.itr.iterate() function to check for the pattern within the collection.

Below is a simple example of how to use the elevaso_spine.fmt.sub.sub_value() function.

from elevaso_spine.fmt import sub

value = {
  "key1": [
    "No replacement",
    "${REPLACE_ME}",
    "No replacement"
  ],
  "key2": [
    "No replacement",
    "${REPLACE_ME_2}",
    "No replacement"
  ]
}

placeholder_values = {
  "REPLACE_ME": "I've been replaced",
  "REPLACE_ME_2": "I've also been replaced",
}

print(sub.sub_value(value, placeholder_values))

Running this could would produce following:

{
  "key1": [
    "No replacement",
    "I've been replaced",
    "No replacement"
  ],
  "key2": [
    "No replacement",
    "I've also been replaced",
    "No replacement"
  ]
}
elevaso_spine.fmt.sub.sub_value(value: object, sub_values: dict, **kwargs) object[source]

Substitute values based on a RegEx pattern and return updated object

Args:

value (object): Python object to look for the pattern

sub_values (dict): Dictionary of possible values to substitute

Kwargs:

pattern (re.Pattern, Optional): RegEx Pattern to search for, defaults to DEFAULT_SUB_VALUE_REGEX

default_val (object, Optional): Default value for placeholder if not found in sub_values, default to None (returns original value/item)

error_not_exist (bool, Optional): Raise error if the placeholder key does not exist in sub_values, default is False

copy_val (bool, Optional): True/False contents of dict/list should be copied prior to modifying, defaults to True

Raises:

TypeError: If sub_values type is not dict or OrderedDict

Returns:

object: Updated object with values substituted