Subclass for string.Template: adds placeholders() method.
Project description
This package offers a very thin subclass of string.Template which adds a few extra features to the standard library class.
NOTE: this package has no dependencies and will therefore not incur an additional 3rd-party dependency cost. This is intentional and will not change.
Get var names in the template
The placeholders() method returns a generator of the variable names inside the template:
# main.py
from cjrh_template import Template
s = '$person1 gave $object to $person2'
tmpl = Template(s)
print(list(tmpl.placeholders()))
Output:
$ python main.py
['person1', 'object', 'person2']
A typical use-case for this is to be able to see a UI with the required parameters for template substitution. Also, note that the method returns a generator that will return the names sequentially in a memory-efficient way.
There is also an allow_repeats parameter to placeholders() that will allow the generator to return even repeated variable names, depending on the sequence in which they’re found in the template. This could be used to generate frequency counts of variable names. Default is False.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for cjrh_template-2017.5.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 234c0f46c3b37a91b4b9cac275fc9fc352b3d9ffa2f2c10c7d16bd25f28c557f |
|
MD5 | 299516022eef013f2e5c492ad1521d22 |
|
BLAKE2b-256 | 8fa8e038fa35899160dfed5c8ed92443c6e2a1ff2c6ec6abf7303b3d26509b66 |