provides an intuitive API for building and manipulating Abstract Syntax Trees (ASTs) to generate Python code.
Project description
astlab
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 ast
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())
# Or you can just get the AST
print(ast.dump(foo.build(), indent=4))
Output
Render:
import builtins
import dataclasses
@dataclasses.dataclass()
class Bar:
spam: builtins.int
Dump built AST:
Module(
body=[
Import(
names=[
alias(name='builtins')]),
Import(
names=[
alias(name='dataclasses')]),
ClassDef(
name='Bar',
bases=[],
keywords=[],
body=[
AnnAssign(
target=Name(id='spam'),
annotation=Attribute(
value=Name(id='builtins'),
attr='int'),
simple=1)],
decorator_list=[
Call(
func=Attribute(
value=Name(id='dataclasses'),
attr='dataclass'),
args=[],
keywords=[])])],
type_ignores=[])
Func def & call example
import astlab
with astlab.module("foo") as foo:
with foo.class_def("Bar") as bar:
with bar.method_def("do_stuff").arg("spam", int).returns(str) as stuff:
stuff.assign_stmt("result", stuff.call(str).arg(stuff.attr("spam")))
stuff.return_stmt(stuff.attr("result"))
print(foo.render())
Output
import builtins
class Bar:
def do_stuff(self, spam: builtins.int) -> builtins.str:
result = builtins.str(spam)
return result
Type reference example
import astlab
with astlab.package("main") as main:
with main.module("foo") as foo:
with foo.class_def("Bar") as bar:
pass
with main.module("spam") as spam:
with spam.class_def("Eggs").inherits(bar) as eggs:
with eggs.method_def("do_stuff").returns(bar.ref().optional()) as stuff:
pass
print(spam.render())
Output
import main.foo
import typing
class Eggs(main.foo.Bar):
def do_stuff(self) -> typing.Optional[main.foo.Bar]:
pass
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file astlab-0.4.4.tar.gz.
File metadata
- Download URL: astlab-0.4.4.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03dd6ee637df33e7c346f644b77b5f1159b69d843e8facaefa41de3c9b2dee01
|
|
| MD5 |
9775b573ac8865fdca0899fd228fdb5e
|
|
| BLAKE2b-256 |
592cfa1bd2bf0142684944743580cc818874f5143e2bb96467b69b947b134178
|
Provenance
The following attestation bundles were made for astlab-0.4.4.tar.gz:
Publisher:
publish.yaml on zerlok/astlab
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
astlab-0.4.4.tar.gz -
Subject digest:
03dd6ee637df33e7c346f644b77b5f1159b69d843e8facaefa41de3c9b2dee01 - Sigstore transparency entry: 606386234
- Sigstore integration time:
-
Permalink:
zerlok/astlab@8665208f23b5e33a32f9f3d0a8b34ad75760028a -
Branch / Tag:
refs/tags/v0.4.4 - Owner: https://github.com/zerlok
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@8665208f23b5e33a32f9f3d0a8b34ad75760028a -
Trigger Event:
release
-
Statement type:
File details
Details for the file astlab-0.4.4-py3-none-any.whl.
File metadata
- Download URL: astlab-0.4.4-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
977d0ccf22c628b572c8fdfd25e276a5d9cb2f94d044da47359764fd38532dc5
|
|
| MD5 |
f02587177492956c010a3857dcefedd2
|
|
| BLAKE2b-256 |
5e763ce0d69b51f580dac29c5c87ca203517d83e5a25afdd6f9bb691f8014aa8
|
Provenance
The following attestation bundles were made for astlab-0.4.4-py3-none-any.whl:
Publisher:
publish.yaml on zerlok/astlab
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
astlab-0.4.4-py3-none-any.whl -
Subject digest:
977d0ccf22c628b572c8fdfd25e276a5d9cb2f94d044da47359764fd38532dc5 - Sigstore transparency entry: 606386252
- Sigstore integration time:
-
Permalink:
zerlok/astlab@8665208f23b5e33a32f9f3d0a8b34ad75760028a -
Branch / Tag:
refs/tags/v0.4.4 - Owner: https://github.com/zerlok
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@8665208f23b5e33a32f9f3d0a8b34ad75760028a -
Trigger Event:
release
-
Statement type: