Skip to main content

Write python with python.

Project description

Synt

Write Python with Python. Inspired by jennifer.

Python-3.12 GitHub top language GitHub Actions Workflow Status Codecov PyPI - Downloads PyPI - License PyPI - Version

Synt is a library for developers to write elegant machine-generated Python code.

Documentation


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

synt-0.1.0.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

synt-0.1.0-py3-none-any.whl (33.8 kB view details)

Uploaded Python 3

File details

Details for the file synt-0.1.0.tar.gz.

File metadata

  • Download URL: synt-0.1.0.tar.gz
  • Upload date:
  • Size: 23.9 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

Hashes for synt-0.1.0.tar.gz
Algorithm Hash digest
SHA256 09355cfb9df23396f55865d8e4868730ae4af6ac8bf292888384570ad2e55390
MD5 460ab3b1b2ae633781b7a88747f2027c
BLAKE2b-256 9593234c1443227409909c7d2e960da7f5c3ffca553cb7ae4e29a386661a9414

See more details on using hashes here.

File details

Details for the file synt-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: synt-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 33.8 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

Hashes for synt-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b6b40e26b73c0f7a2960e85ee80074d3e93d286e42d83517a20e3105fcd13886
MD5 dbb1a39f73d1e8bbe0c9c2fd3eacd8b8
BLAKE2b-256 cde2ac7593273d993dea9a42e4ab21cecba21c65b33d6152f286a7880e528ffb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page