Write python with python.
Project description
Synt
Write Python with Python. Inspired by jennifer.
Synt is a library for developers to write elegant machine-generated Python code.
Installation
To install Synt, use your preferred package manager and add synt
to your dependencies, e.g. by pip:
pip install synt
Then, import Synt:
import synt # directly import Synt, or
from synt.prelude import * # import pre-organized utilities from `prelude`.
Overview
Synt creates a Python-based DSL for writing actual Python code.
Different from text-based template systems like Jinja, Synt allows you to construct Python code generator as-is:
from synt.prelude import *
expression = id_("foo") \
.expr() \
.attr("bar") \
.call(buzz=id_("buzz"))
assert expression.into_code() == "foo.bar(buzz=buzz)"
Usage
Synt keeps most of Python's standard operations as-is. Currently, Synt only supports generating expressions, and statement generating is on the to-do list.
Typically, special syntax in Python can be used with alias methods with the same name. For example, the following example shows how to create a generator comprehension:
from synt.prelude import *
comp = fstring("Item: ", fnode(id_("x"))) \
.for_(id_("x")) \
.in_(id_("it"))
# note: Comprehension expressions are a bit different
# because it accepts any amount of `for`s and `if`s.
# Thus we must add a `.expr()` to force it to be an expression.
assert comp.expr().into_code() == r'(f"Item: {x}" for x in it)'
For full api documentation, see the Documentation page.
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
Built Distribution
File details
Details for the file synt-0.4.0.tar.gz
.
File metadata
- Download URL: synt-0.4.0.tar.gz
- Upload date:
- Size: 38.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1023-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd7729e8ffddea1bf231864c8b4f4ad4963519dcadc200023c0549872761632b |
|
MD5 | 2702cc3351cc3a6d7ec1fd525802ceeb |
|
BLAKE2b-256 | b2f2e018910b260a2d72cf3807093ec70314153dde48c6950a594a4872ab0383 |
File details
Details for the file synt-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: synt-0.4.0-py3-none-any.whl
- Upload date:
- Size: 60.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1023-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cdeab25f5de40e8906b94a85ce14a275ebb95b0e52971d04d1fcf7e44b98d78 |
|
MD5 | 54e3a166d1afcc7971e1934193428314 |
|
BLAKE2b-256 | c351a652e3d8e242361227f8e89b95bd9ebf1826203fc07cc995fd3b61bebaa0 |