Skip to main content

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


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)

Uploaded Source

Built Distribution

tagged-0.0.1-py3-none-any.whl (3.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page