tagged

A Python version of JavaScript's tagged templates, mixed with Python 3's f-strings.
For a more involved example how this package can be used in practice, see htm.py that implements JSX-like syntax in plain Python.
Installation
$ pip3 install tagged
Usage
This package defines tag, a decorator for creating new tags. Let's define a simple one:
from tagged import tag
@tag
def t(strings, values):
return strings, values
Now t can be called with a template string that can contain any Python 3 expression between { and }. The "static" parts of the string are listed in strings and the evaluated expressions are listed in values.
strings, values = t("1 + {2} equals {1 + 2}")
# strings == ('1 + ', ' equals ', '')
# values == (2, 3)
Because the expressions are evaluated in the current context they can refer to local variables:
a = [1, 2, 3]
strings, values = t("the sum of {a} is {sum(a)}")
# strings == ('the sum of ', ' is ', '')
# values == ([1, 2, 3], 6)
Double curly brackets are interpreted as a single textual curly bracket:
strings, values = t("Inline a set between {{ and }} like this: {({1, 2, 3})}")
# strings == ('Inline a set between { and } like this: ', '')
# values == ({1, 2, 3},)
rexample
Let's define a custom tag rex that allows composing regular expressions from strings and other regular expressions.
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.
Metadata
Release files for tagged 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tagged-0.0.2.tar.gz | 3.3 kB | Without provenance | Details for tagged-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tagged-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for tagged-0.0.2-py3-none-any.whl |
Total release size: 7.4 kB
Release files / tagged-0.0.2.tar.gz
| Download URL | tagged-0.0.2.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fb1e830ce83b7df7e92d03ad349babad0c78eb1d1c5dd922e067f1079b119666
|
|
BLAKE2b-256 checksum How to use checksums |
d05e96ad04eb807533cb676a790bb2d38800c4236d108b49505da822a21b4472
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.32.2 CPython/3.7.4
|
Release files / tagged-0.0.2-py3-none-any.whl
| Download URL | tagged-0.0.2-py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6f9bbb98c083536c5ec283688ddc9cec830ac0de19ab4ad9fa12db66985f7469
|
|
BLAKE2b-256 checksum How to use checksums |
43a8bf957ffdcd91d1ffad19083e4af72269ff7bb832113e2558e8270d4efd22
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.32.2 CPython/3.7.4
|
Security for tagged 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.