Skip to content
New: Star-import, signal unraveling, Python 3.12 support, and more

# 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,
        sub_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(
        template,
        context=None,
        strict=None,
        sub_id=None,
        context_merge_kwargs=None
    )

Class for substituting templates.

Method generated by attrs for class CustomTemplate.

Subclasses

- Rep
- RepEval
- RepFunc
- Sub

### context class variable

Context mapping.

### context_merge_kwargs class variable

Keyword arguments passed to merge_dicts().

### meets_sub_id method

    CustomTemplate.meets_sub_id(
        sub_id=None
    )

Return whether the substitution id of the template meets the global substitution id.

### resolve_context method

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

Resolve CustomTemplate.context.

Merges `context` in template, CustomTemplate.context, and `context`. Automatically appends `sub_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().

### sub_id class variable

Substitution id or ids at which to evaluate this template

Checks against `sub_id` passed by substitute_templates().

### substitute method

    CustomTemplate.substitute(
        context=None,
        strict=None,
        sub_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(
        template,
        context=None,
        strict=None,
        sub_id=None,
        context_merge_kwargs=None
    )

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

Method generated by attrs for class CustomTemplate.

Superclasses

- CustomTemplate

Inherited members

- CustomTemplate.context
- CustomTemplate.context_merge_kwargs
- CustomTemplate.meets_sub_id()
- CustomTemplate.resolve_context()
- CustomTemplate.resolve_strict()
- CustomTemplate.strict
- CustomTemplate.sub_id
- CustomTemplate.template

### substitute method

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

Replace Rep.template as a key.

## RepEval class

    RepEval(
        template,
        context=None,
        strict=None,
        sub_id=None,
        context_merge_kwargs=None
    )

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

Method generated by attrs for class CustomTemplate.

Superclasses

- CustomTemplate

Inherited members

- CustomTemplate.context
- CustomTemplate.context_merge_kwargs
- CustomTemplate.meets_sub_id()
- CustomTemplate.resolve_context()
- CustomTemplate.resolve_strict()
- CustomTemplate.strict
- CustomTemplate.sub_id
- CustomTemplate.template

### substitute method

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

Evaluate RepEval.template as an expression.

## RepFunc class

    RepFunc(
        template,
        context=None,
        strict=None,
        sub_id=None,
        context_merge_kwargs=None
    )

Template function to be called with argument names from `context`.

Method generated by attrs for class CustomTemplate.

Superclasses

- CustomTemplate

Inherited members

- CustomTemplate.context
- CustomTemplate.context_merge_kwargs
- CustomTemplate.meets_sub_id()
- CustomTemplate.resolve_context()
- CustomTemplate.resolve_strict()
- CustomTemplate.strict
- CustomTemplate.sub_id
- CustomTemplate.template

### substitute method

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

Call RepFunc.template as a function.

## Sub class

    Sub(
        template,
        context=None,
        strict=None,
        sub_id=None,
        context_merge_kwargs=None
    )

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

Always returns a string.

Method generated by attrs for class CustomTemplate.

Superclasses

- CustomTemplate

Inherited members

- CustomTemplate.context
- CustomTemplate.context_merge_kwargs
- CustomTemplate.meets_sub_id()
- CustomTemplate.resolve_context()
- CustomTemplate.resolve_strict()
- CustomTemplate.strict
- CustomTemplate.sub_id
- CustomTemplate.template

### substitute method

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

Substitute parts of Sub.template as a regular template.

#### Cookie consent

We use cookies to recognize your repeated visits and preferences, as well as to measure the effectiveness of our documentation and whether users find what they're searching for. With your consent, you're helping us to make our documentation better.

- Google Analytics
- GitHub

Manage settings