Name | astlab JSON |
Version |
0.4.0
JSON |
| download |
home_page | None |
Summary | provides an intuitive API for building and manipulating Abstract Syntax Trees (ASTs) to generate Python code. |
upload_time | 2025-07-12 10:23:22 |
maintainer | None |
docs_url | None |
author | zerlok |
requires_python | <4.0,>=3.9 |
license | MIT |
keywords |
python
codegen
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 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
```
Dump 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 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
```python
import builtins
class Bar:
def do_stuff(self, spam: builtins.int) -> builtins.str:
result = builtins.str(spam)
return result
```
### Type reference example
```python
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
```python
import main.foo
import typing
class Eggs(main.foo.Bar):
def do_stuff(self) -> typing.Optional[main.foo.Bar]:
pass
```
Raw data
{
"_id": null,
"home_page": null,
"name": "astlab",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": "python, codegen",
"author": "zerlok",
"author_email": "danil.troshnev@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/92/67/78675db4d3d70dc93f4002d7d8efdb4d15dfa4c50841beb90fad2d9cf015/astlab-0.4.0.tar.gz",
"platform": null,
"description": "# astlab\n\n[](https://pypi.python.org/pypi/astlab)\n[](https://pypi.python.org/pypi/astlab)\n[](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)\n[](https://codecov.io/gh/zerlok/astlab)\n[](https://pypistats.org/packages/astlab)\n[](https://github.com/zerlok/astlab/stargazers)\n\n**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.\n\n## Features\n\n- **Easy AST construction**: Build Python code using a fluent and intuitive API.\n- **Code generation**: Convert your AST into valid Python code, forget about jinja templates.\n- **Supports nested scopes & auto imports**: Create nested classes, methods, and fields effortlessly. Reference types from other modules easily.\n- **Highly customizable**: Extend and modify the API to suit your needs.\n\n## Installation\n\nYou can install **astlab** from PyPI using `pip`:\n\n```bash\npip install astlab\n```\n\n## Usage\n\n### Simple example\n\nHere's a basic example of how to use **astlab** to create a Python module with a dataclass.\n\n```python\nimport ast\nimport astlab\n\n# Create a new Python module\nwith astlab.module(\"foo\") as foo:\n # Build a \"Bar\" dataclass\n with foo.class_def(\"Bar\").dataclass() as bar:\n # Define a field \"spam\" of type int\n bar.field_def(\"spam\", int)\n\n# Generate and print the Python code from the AST\nprint(foo.render())\n# Or you can just get the AST\nprint(ast.dump(foo.build(), indent=4))\n```\n\n#### Output\n\nRender:\n\n```python\nimport builtins\nimport dataclasses\n\n@dataclasses.dataclass()\nclass Bar:\n spam: builtins.int\n```\n\nDump built AST:\n\n```python\nModule(\n body=[\n Import(\n names=[\n alias(name='builtins')]),\n Import(\n names=[\n alias(name='dataclasses')]),\n ClassDef(\n name='Bar',\n bases=[],\n keywords=[],\n body=[\n AnnAssign(\n target=Name(id='spam'),\n annotation=Attribute(\n value=Name(id='builtins'),\n attr='int'),\n simple=1)],\n decorator_list=[\n Call(\n func=Attribute(\n value=Name(id='dataclasses'),\n attr='dataclass'),\n args=[],\n keywords=[])])],\n type_ignores=[])\n```\n\n### Func def & call example\n\n```python\nimport astlab\n\nwith astlab.module(\"foo\") as foo:\n with foo.class_def(\"Bar\") as bar:\n with bar.method_def(\"do_stuff\").arg(\"spam\", int).returns(str) as stuff:\n stuff.assign_stmt(\"result\", stuff.call(str).arg(stuff.attr(\"spam\")))\n stuff.return_stmt(stuff.attr(\"result\"))\n\nprint(foo.render())\n```\n\n#### Output\n\n```python\nimport builtins\n\nclass Bar:\n\n def do_stuff(self, spam: builtins.int) -> builtins.str:\n result = builtins.str(spam)\n return result\n```\n\n### Type reference example\n\n```python\nimport astlab\n\nwith astlab.package(\"main\") as main:\n with main.module(\"foo\") as foo:\n with foo.class_def(\"Bar\") as bar:\n pass\n\n with main.module(\"spam\") as spam:\n with spam.class_def(\"Eggs\").inherits(bar) as eggs:\n with eggs.method_def(\"do_stuff\").returns(bar.ref().optional()) as stuff:\n pass\n\nprint(spam.render())\n```\n\n#### Output\n\n```python\nimport main.foo\nimport typing\n\nclass Eggs(main.foo.Bar):\n\n def do_stuff(self) -> typing.Optional[main.foo.Bar]:\n pass\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "provides an intuitive API for building and manipulating Abstract Syntax Trees (ASTs) to generate Python code.",
"version": "0.4.0",
"project_urls": {
"Homepage": "https://github.com/zerlok/astlab",
"Issues": "https://github.com/zerlok/astlab/issues"
},
"split_keywords": [
"python",
" codegen"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "6e902e974d60b8af2b2ef49f6b69ebb3266db1ea01d2e34e637c1c319e2a98a0",
"md5": "076e57d5542075cf65b3a127c0aab88b",
"sha256": "29a84f8e114a0ad98f898c8257e98c43cf6964fe9ce32b98f028eb60efe35400"
},
"downloads": -1,
"filename": "astlab-0.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "076e57d5542075cf65b3a127c0aab88b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 25468,
"upload_time": "2025-07-12T10:23:21",
"upload_time_iso_8601": "2025-07-12T10:23:21.502654Z",
"url": "https://files.pythonhosted.org/packages/6e/90/2e974d60b8af2b2ef49f6b69ebb3266db1ea01d2e34e637c1c319e2a98a0/astlab-0.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "926778675db4d3d70dc93f4002d7d8efdb4d15dfa4c50841beb90fad2d9cf015",
"md5": "1931eeb2f2b577fe51aa314460698477",
"sha256": "e5ca2f93d2433c9a377dd46c98fc26e2ef466b99e7127e217b1a8a73d6dbfa75"
},
"downloads": -1,
"filename": "astlab-0.4.0.tar.gz",
"has_sig": false,
"md5_digest": "1931eeb2f2b577fe51aa314460698477",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 22114,
"upload_time": "2025-07-12T10:23:22",
"upload_time_iso_8601": "2025-07-12T10:23:22.582467Z",
"url": "https://files.pythonhosted.org/packages/92/67/78675db4d3d70dc93f4002d7d8efdb4d15dfa4c50841beb90fad2d9cf015/astlab-0.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-12 10:23:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "zerlok",
"github_project": "astlab",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "astlab"
}