templang
A simple annotation-based language to generate code from templates
Templates
Templates are just source files with annotations. For example:
# BEGIN
from typing import Literal
Rank = Literal[
# LOOP RANK
'RANK',
# END
]
CLEARANCE_LEVEL = int # DELETE (just to make mypy happy)
# LOOP RANK CLEARANCE_LEVEL
class RANK:
clearance_level: CLEARANCE_LEVEL
# END
# END
Template Parsing
from templang import parse
with open('ranks.py') as f:
source = f.read()
code = parse(source, translations={
'RANK': ['Captain', 'Lieutenant', 'Sergeant'],
'CLEARANCE_LEVEL': ['1', '2', '3']
})
print(code)
# from typing import Literal
#
# Rank = Literal[
# 'Captain',
# 'Lieutenant',
# 'Sergeant',
# ]
#
# class Captain:
# clearance_level: 1
#
# class Lieutenant:
# clearance_level: 2
#
# class Sergeant:
# clearance_level: 3
Rules
Now, that's most of what you need to now. But, to be precise, here are the rules:
-
# BEGINand# ENDdelimit a template -
Lines starting with
# UNCOMMENTare stripped to whatever proceeds the annotation -
Lines ending with
# DELETEare removed -
translations: Mapping[str, str | Sequence[str]]is the dictionary of translations:- If
value = translations[key]is a string, all instances ofkeyare replaced byvalue - If
value = translations[key]is a sequence of strings,keywill be allowed in a# LOOPdeclaration - The order of translations is not guaranteed. So, keys substrings of other keys will probably yield inentended results
- If
-
# LOOP VAR1 ... VARNand# ENDdelimit a loop:translations[VARi]must be a sequence of stringsLEN = len(translations[VAR1]) == ... == len(translations[VARN])- The template inside the loop is substituted with
translations[VARi][j]for eachj in range(LEN) - Nested loops are allowed
- If nested loops involve a same variable
VARmultiple times, thentranslations[VAR]must be a nested sequence of strings (matching the loop depth)-
E.g:
# BEGIN from typing import Union, Literal Marriage = Union[ # LOOP NAMES tuple [ # LOOP NAMES Literal['NAMES'], # END ], # END ] # END parse(open(__file__).read(), { 'NAMES': [['John', 'Jane'], ['Stuart', 'Alice']] }) # from typing import Union, Literal # # Marriage = Union[ # tuple [ # Literal['John'], # Literal['Jane'], # ], # tuple [ # Literal['Stuart'], # Literal['Alice'], # ], # ]
-
-
Oh, and actually,
#can be replaced by any custom string (e.g.//)
Metadata
Release files for templang 0.1.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 |
|---|---|---|---|---|
| templang-0.1.1.tar.gz | 3.4 kB | Without provenance | Details for templang-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| templang-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for templang-0.1.1-py3-none-any.whl |
Total release size: 7.2 kB
Release files / templang-0.1.1.tar.gz
| Download URL | templang-0.1.1.tar.gz |
|---|---|
| Size | 3.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
84ad17d0017271c6bdef1d17561e1cca31716bdf164a25b6ccbb952b654c31ef
|
|
BLAKE2b-256 checksum How to use checksums |
0787778be404d6c679ae2e3eb7895d33697b8ca63c981814dfc0c3e5fd3a059b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.6
|
Release files / templang-0.1.1-py3-none-any.whl
| Download URL | templang-0.1.1-py3-none-any.whl |
|---|---|
| Size | 3.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6b913bc9c914e02ce26fedb36b514f7d235f25366ac0bb0c5f1263de6e9b8c43
|
|
BLAKE2b-256 checksum How to use checksums |
b8c5d6e2d0ea701be337a9d344eb8170797119b101eeeaa52eae3f50d2796670
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.6
|
Security for templang 0.1.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.