Tagged templates for Python
Project description
tagged
A Python version of JavaScript's tagged templates.
Installation
$ pip3 install tagged
Usage
import re
from tagged import tag
@tag
def rex(strings, values):
pattern = strings[0]
for value, string in zip(values, strings[1:]):
if isinstance(value, re.Pattern):
value = value.pattern
elif isinstance(value, str):
value = re.escape(value)
else:
raise TypeError("expected re.Pattern or str")
pattern += "(?:" + value + ")" + string
return re.compile(pattern)
greeting = re.compile("Hello|Hi|Greetings")
name = "Python 3.7"
rex("{greeting}, {name}!")
# re.compile('(?:Hello|Hi|Greetings), (?:Python\\ 3\\.7)!')
Development
Running Tests
$ python3 -m unittest discover -s tests
License
This library is licensed under the MIT license. See ./LICENSE.
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 Distribution
tagged-0.0.1.tar.gz
(2.4 kB
view hashes)
Built Distribution
tagged-0.0.1-py3-none-any.whl
(3.4 kB
view hashes)