Python templates with type annotations and validation.
Project description
typelate
Python templates with type annotations and validation.
Background
A somewhat hidden feature in Python is the ability to create str templates, they are similar to f-strings but they don't require you to know the values in advance.
template = "Hello, {name}!"
Using it is as simple as calling the format function
>>> template.format(name="World")
"Hello, World!"
>>> template.format(name=123)
"Hello, 123!"
In addition to that, like f-strings we can specify a format specifier to each argument.
template = "Pi is {pi: .2f}"
>>> import math
>>> template.format(pi=math.pi)
"Pi is 3.14"
Usage
With typelate this behavior is extended even more, now you can specify types for each argument in the template, in runtime a template formatting will have validation that can be handled as you wish!
from typed_template import Template
template = Template("Hello, {name: str}!")
Notice that Template uses the __call__ instead of format
>>> template(name="World")
"Hello, World!"
Now, let's pass an invalid type.
>>> template(name=123)
TypeError: Incorrect type for replacement 'name', expected: <class 'str'>.
Moreover, you can use the default format specifiers in addition to the type annotation:
from typed_template import Template
template = Template("Pi is {pi: float: .2f}")
>>> import math
>>> template(pi=math.pi)
"Pi is 3.14"
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typelate-0.1.0.tar.gz.
File metadata
- Download URL: typelate-0.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b5bd38aca3f31e6b5683521c0e80e662d4f101c92465f44c2c2aca42af0af1d
|
|
| MD5 |
6e6f4314b3b3ab3a82d63bb99f039c38
|
|
| BLAKE2b-256 |
2b8fcb1dd8bf9645dca533edad1949f0ca86006ec7de88962870efbc51e1bffa
|
File details
Details for the file typelate-0.1.0-py3-none-any.whl.
File metadata
- Download URL: typelate-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
736897195281f6775fb8568467a5252a6c9d75758ad341cf8da19978626df884
|
|
| MD5 |
e07638b06b2cb5edbae1e938aafc05e6
|
|
| BLAKE2b-256 |
78dd1d9758f619117c537ab8fea42ba7499c83a9ff6b6e618eeb481cd2c0e27e
|