https://github.com/zerlok/astlab
A Python library for building and generating Abstract Syntax Trees (ASTs) with a simple, intuitive API.
https://github.com/zerlok/astlab
abstract-syntax-tree ast codegen codegenerator python
Last synced: 6 months ago
JSON representation
A Python library for building and generating Abstract Syntax Trees (ASTs) with a simple, intuitive API.
- Host: GitHub
- URL: https://github.com/zerlok/astlab
- Owner: zerlok
- License: mit
- Created: 2025-01-23T17:24:00.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-03-09T12:33:11.000Z (7 months ago)
- Last Synced: 2025-03-24T12:04:58.653Z (7 months ago)
- Topics: abstract-syntax-tree, ast, codegen, codegenerator, python
- Language: Python
- Homepage:
- Size: 134 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# astlab
[](https://pypi.python.org/pypi/astlab)
[](https://pypi.python.org/pypi/astlab)
[](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
[](https://codecov.io/gh/zerlok/astlab)
[](https://pypistats.org/packages/astlab)
[](https://github.com/zerlok/astlab/stargazers)**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`:
```bash
pip install astlab
```## Usage
### Simple example
Here's a basic example of how to use **astlab** to create a Python module with a dataclass.
```python
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:
```python
import builtins
import dataclasses@dataclasses.dataclass()
class Bar:
spam: builtins.int
```Built AST:
```python
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
```python
import astlabwith 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
```python
import builtinsclass Bar:
def do_stuff(self, spam: builtins.int) -> builtins.str:
result = builtins.str(spam)
return result
```### Type reference example
```python
import astlabwith astlab.package("main") as main:
with main.module("foo") as foo:
with foo.class_def("Bar") as bar:
passwith 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:
passprint(spam.render())
```#### Output
```python
import main.foo
import typingclass Eggs(main.foo.Bar):
def do_stuff(self) -> typing.Optional[main.foo.Bar]:
pass
```