Skip to content

template module

Utilities for working with templates.


deep_substitute function

deep_substitute(
    obj,
    mapping=None,
    safe=False,
    make_copy=True
)

Traverses the object recursively and, if any template found, substitutes it using a mapping.

Traverses tuples, lists, dicts and (frozen-)sets. Does not look for templates in keys.

If safe is True, won't raise an error but return the original template.

Note

If the object is deep (such as a dict or a list), creates a copy of it if any template found inside, thus loosing the reference to the original. Make sure to do a deep or hybrid copy of the object before proceeding for consistent behavior, or disable make_copy to override the original in place.

Usage

>>> import vectorbt as vbt

>>> vbt.deep_substitute(vbt.Sub('$key', {'key': 100}))
100
>>> vbt.deep_substitute(vbt.Sub('$key', {'key': 100}), {'key': 200})
200
>>> vbt.deep_substitute(vbt.Sub('$key$key'), {'key': 100})
100100
>>> vbt.deep_substitute(vbt.Rep('key'), {'key': 100})
100
>>> vbt.deep_substitute([vbt.Rep('key'), vbt.Sub('$key$key')], {'key': 100})
[100, '100100']
>>> vbt.deep_substitute(vbt.RepFunc(lambda key: key == 100), {'key': 100})
True
>>> vbt.deep_substitute(vbt.RepEval('key == 100'), {'key': 100})
True
>>> vbt.deep_substitute(vbt.RepEval('key == 100', safe=False))
NameError: name 'key' is not defined
>>> vbt.deep_substitute(vbt.RepEval('key == 100', safe=True))
<vectorbt.utils.template.RepEval at 0x7fe3ad2ab668>

has_templates function

has_templates(
    obj
)

Check if the object has any templates.


Rep class

Rep(
    key,
    mapping=None
)

Key to be replaced with the respective value from mapping.

Superclasses


key property

Key to be replaced.


mapping property

Mapping object passed to the initializer.


replace method

Rep.replace(
    mapping=None
)

Replace Rep.key using mapping.

Merges mapping and Rep.mapping.


RepEval class

RepEval(
    expression,
    mapping=None
)

Expression to be evaluated with mapping used as locals.

Superclasses


eval method

RepEval.eval(
    mapping=None
)

Evaluate RepEval.expression using mapping.

Merges mapping and RepEval.mapping.


expression property

Expression to be evaluated.


mapping property

Mapping object passed to the initializer.


RepFunc class

RepFunc(
    func,
    mapping=None
)

Function to be called with argument names from mapping.

Superclasses


call method

RepFunc.call(
    mapping=None
)

Call RepFunc.func using mapping.

Merges mapping and RepFunc.mapping.


func property

Replacement function to be called.


mapping property

Mapping object passed to the initializer.


Sub class

Sub(
    template,
    mapping=None
)

Template to substitute parts of the string with the respective values from mapping.

Returns a string.

Superclasses


mapping property

Mapping object passed to the initializer.


substitute method

Sub.substitute(
    mapping=None
)

Substitute parts of Sub.template using mapping.

Merges mapping and Sub.mapping.


template property

Template to be processed.