Skip to content

template module

Utilities for working with templates.


has_templates function

has_templates(
    obj,
    **kwargs
)

Check if the object has any templates.

Uses any_in_obj().

Default can be overridden with search_kwargs under template.


substitute_templates function

substitute_templates(
    obj,
    context=None,
    strict=None,
    eval_id=None,
    **kwargs
)

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

Uses find_and_replace_in_obj().

If strict is True, raises an error if processing template fails. Otherwise, returns the original template.

Default can be overridden with search_kwargs under template.

Usage

>>> from vectorbtpro import *

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

CustomTemplate class

CustomTemplate(
    *args,
    **kwargs
)

Class for substituting templates.

Superclasses

Inherited members

Subclasses


context class variable

Context mapping.


context_merge_kwargs class variable

Keyword arguments passed to merge_dicts().


eval_id class variable

One or more identifiers at which to evaluate this instance.


resolve_context method

CustomTemplate.resolve_context(
    context=None,
    eval_id=None
)

Resolve CustomTemplate.context.

Merges context in template, CustomTemplate.context, and context. Automatically appends eval_id, np (NumPy), pd (Pandas), and vbt (vectorbtpro).


resolve_strict method

CustomTemplate.resolve_strict(
    strict=None
)

Resolve CustomTemplate.strict.

If strict is None, uses strict in template.


strict class variable

Whether to raise an error if processing template fails.

If not None, overrides strict passed by substitute_templates().


substitute method

CustomTemplate.substitute(
    context=None,
    strict=None,
    eval_id=None
)

Abstract method to substitute the template CustomTemplate.template using the context from merging CustomTemplate.context and context.


template class variable

Template to be processed.


Rep class

Rep(
    *args,
    **kwargs
)

Template string to be replaced with the respective value from context.

Superclasses

Inherited members


substitute method

Rep.substitute(
    context=None,
    strict=None,
    eval_id=None
)

Replace Rep.template as a key.


RepEval class

RepEval(
    *args,
    **kwargs
)

Template expression to be evaluated using multiline_eval() with context used as locals.

Superclasses

Inherited members


substitute method

RepEval.substitute(
    context=None,
    strict=None,
    eval_id=None
)

Evaluate RepEval.template as an expression.


RepFunc class

RepFunc(
    *args,
    **kwargs
)

Template function to be called with argument names from context.

Superclasses

Inherited members


substitute method

RepFunc.substitute(
    context=None,
    strict=None,
    eval_id=0
)

Call RepFunc.template as a function.


Sub class

Sub(
    *args,
    **kwargs
)

Template string to substitute parts with the respective values from context.

Always returns a string.

Superclasses

Inherited members


substitute method

Sub.substitute(
    context=None,
    strict=None,
    eval_id=None
)

Substitute parts of Sub.template as a regular template.