Skip to main content

provides an intuitive API for building and manipulating Abstract Syntax Trees (ASTs) to generate Python code.

Project description

astlab

Latest Version Python Supported Versions MyPy Strict Test Coverage Downloads GitHub stars

astlab is a Python library that provides an intuitive API for building and manipulating Abstract Syntax Trees (ASTs) to generate Python code. With astlab, you can easily create Python modules, classes, fields, and more using a simple and readable syntax, and convert the AST back into executable Python code.

Features

  • Easy AST construction: Build Python code using a fluent and intuitive API.
  • Code generation: Convert your AST into valid Python code, forget about jinja templates.
  • Supports nested scopes & auto imports: Create nested classes, methods, and fields effortlessly. Reference types from other modules easily.
  • Highly customizable: Extend and modify the API to suit your needs.

Installation

You can install astlab from PyPI using pip:

pip install astlab

Usage

Simple example

Here's a basic example of how to use astlab to create a Python module with a dataclass.

import astlab

# Create a new Python module
with astlab.module("foo") as foo:
    # Build a "Bar" dataclass
    with foo.class_def("Bar").dataclass() as bar:
        # Define a field "spam" of type int
        bar.field_def("spam", int)

# Generate and print the Python code from the AST
print(foo.render())

Output:

import builtins
import dataclasses

@dataclasses.dataclass()
class Bar:
    spam: builtins.int

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

astlab-0.2.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

astlab-0.2.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file astlab-0.2.0.tar.gz.

File metadata

  • Download URL: astlab-0.2.0.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for astlab-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a0a042b2225babb713ca47e1c77115e60da80878a9f9f4efe02f645df0fe6792
MD5 95773c3446919fa7344a32ed5a1f822c
BLAKE2b-256 601aaa8366121ee8c90ac42effc0bb5e55b6faa388abb119911af38ef92e5452

See more details on using hashes here.

Provenance

The following attestation bundles were made for astlab-0.2.0.tar.gz:

Publisher: publish.yaml on zerlok/astlab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file astlab-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: astlab-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for astlab-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78df41f4db6a7a28ada5503a2fe9adfb7ef137da20c9681b6145e7b8bc3ebfc1
MD5 6b5dd9aa4116185c0398ec7d907a6d04
BLAKE2b-256 4dd10b551cd4dcbe80925682c5d12e765da25bc93e7368f385eff9673d24842e

See more details on using hashes here.

Provenance

The following attestation bundles were made for astlab-0.2.0-py3-none-any.whl:

Publisher: publish.yaml on zerlok/astlab

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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