template module¶
Utilities for working with templates.
has_templates function¶
Check if the object has any templates.
Uses any_in_obj().
Default can be overridden with search_kwargs under template.
substitute_templates function¶
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¶
Class for substituting templates.
Superclasses
Inherited members
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.hash
- DefineMixin.hash_key
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
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¶
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¶
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¶
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¶
Template string to be replaced with the respective value from context.
Superclasses
Inherited members
- CustomTemplate.context
- CustomTemplate.context_merge_kwargs
- CustomTemplate.eval_id
- CustomTemplate.hash
- CustomTemplate.hash_key
- CustomTemplate.resolve_context()
- CustomTemplate.resolve_strict()
- CustomTemplate.strict
- CustomTemplate.template
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
substitute method¶
Replace Rep.template as a key.
RepEval class¶
Template expression to be evaluated using multiline_eval() with context used as locals.
Superclasses
Inherited members
- CustomTemplate.context
- CustomTemplate.context_merge_kwargs
- CustomTemplate.eval_id
- CustomTemplate.hash
- CustomTemplate.hash_key
- CustomTemplate.resolve_context()
- CustomTemplate.resolve_strict()
- CustomTemplate.strict
- CustomTemplate.template
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
substitute method¶
Evaluate RepEval.template as an expression.
RepFunc class¶
Template function to be called with argument names from context.
Superclasses
Inherited members
- CustomTemplate.context
- CustomTemplate.context_merge_kwargs
- CustomTemplate.eval_id
- CustomTemplate.hash
- CustomTemplate.hash_key
- CustomTemplate.resolve_context()
- CustomTemplate.resolve_strict()
- CustomTemplate.strict
- CustomTemplate.template
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
substitute method¶
Call RepFunc.template as a function.
Sub class¶
Template string to substitute parts with the respective values from context.
Always returns a string.
Superclasses
Inherited members
- CustomTemplate.context
- CustomTemplate.context_merge_kwargs
- CustomTemplate.eval_id
- CustomTemplate.hash
- CustomTemplate.hash_key
- CustomTemplate.resolve_context()
- CustomTemplate.resolve_strict()
- CustomTemplate.strict
- CustomTemplate.template
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
substitute method¶
Substitute parts of Sub.template as a regular template.