templates.py
Simple function templating for Python.
Example
from templates import template
from typing import Type
@template
def cast(t: Type):
def inner(val: str):
return t(val)
return inner
casted = cast[tuple]("abc")
print(casted) # ("a", "b", "c")
Installation
Linux/macOS
python3 -m pip install -U templates.py
Windows
py -3 -m pip install -U templates.py
Usage
Start with decorating a function with templates.template and putting the generics in your signature, like so:
from templates import template
@template
def my_template(a: str, b: str, c: str): # needs to get called using my_template[a, b, c]()
...
Now, for the actual functionality, you need to define another function inside it.
Make sure to return the inner function!
Example:
from templates import template
@template
def my_template(a: str, b: str, c: str):
def inner(test: str):
...
return inner # you always need to return the inner function
Limitations
Type Checking Issue
Due to typing_extensions.ParamSpec being horrible, the following code will not raise an type issue (at least when using pylance)
@template
def add(a: int, b: int):
def inner(c: int) -> int:
return a + b + c
return inner
a = test[1, 2, 3](4) # only raises a runtime error
print(a)
Luckily, you can still see the needed generics when hovering over the template call.
Default Generics
Default generics are not supported:
@template
def my_template(a: str, b: str, c: str = "c"):
def inner(test: str):
...
return inner
my_template["a", "b"]("hi") # raises templates.TemplateError
Return Type Annotation
There is no good way to annotate function return type:
@template
def my_template(a: str, b: str, c: str) -> str: # typing error!
def inner(test: str):
...
return inner
my_template["a", "b", "c"]("hi")
However, you can set the return type of the inner function:
@template
def my_template(a: str, b: str, c: str):
def inner(test: str) -> str: # do this instead!!
...
return inner
my_template["a", "b", "c"]("hi")
Metadata
Release files for templates.py 1.0.1
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 |
|---|---|---|---|---|
| templates.py-1.0.1.tar.gz | 3.7 kB | Without provenance | Details for templates.py-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| templates.py-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for templates.py-1.0.1-py3-none-any.whl |
Total release size: 7.3 kB
Release files / templates.py-1.0.1.tar.gz
| Download URL | templates.py-1.0.1.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e62e97564b03ba8baa8fd77fbfc0b7be0321043bb544ce42ff5cf6ec621f5742
|
|
BLAKE2b-256 checksum How to use checksums |
42b8be552835cb9704e58c0aafe88c8b6feddd052497e036cd6efdcc33f8cb70
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.1 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
|
Release files / templates.py-1.0.1-py3-none-any.whl
| Download URL | templates.py-1.0.1-py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4bb3a66381e23db7beac542c08841f4cfd87aa5bfdd82e0d84648e257268d7bf
|
|
BLAKE2b-256 checksum How to use checksums |
27360168f628502b8b248e1d1e73056fb5af848695dda6f26823cc765230252c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.1 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
|
Security for templates.py 1.0.1
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.