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 contains_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 field

Context mapping.


context_merge_kwargs field

Keyword arguments passed to merge_dicts.


eval_id field

One or more identifiers at which to evaluate this instance.


get_context_vars method

CustomTemplate.get_context_vars()

Get context variables.


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 and from vectorbtpro import *.


resolve_strict method

CustomTemplate.resolve_strict(
    strict=None
)

Resolve CustomTemplate.strict.

If strict is None, uses strict in template.


strict field

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 field

Template to be processed.


Rep class

Rep(
    *args,
    **kwargs
)

Class for replacing a template 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
)

Class for evaluating a template expression using evaluate 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
)

Class for calling a template function with argument names from context.

Superclasses

Inherited members


substitute method

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

Call RepFunc.template as a function.


SafeSub class

SafeSub(
    *args,
    **kwargs
)

Class for substituting parts of a template string with the respective values from context.

Uses string.Template.safe_substitute.

Always returns a string.

Superclasses

Inherited members


substitute method

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

Substitute parts of Sub.template as a regular template.


Sub class

Sub(
    *args,
    **kwargs
)

Class for substituting parts of a template string with the respective values from context.

Uses string.Template.substitute.

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.