A library for dynamically constructing Python code with strong, explicit scoping and symbol tracking.
Project description
python-code-builder
A library for dynamically building Python code with strong, explicit scoping and symbol tracking.
This is work in progress. Currently, the following constructs are supported:
- Module
- Expression
- Constant
- LoadName
- UnaryOperation
- BinaryOperation
- GetAttribute
- GetItem
- Call
- Statement
- Function
- Assign
- Import
- ImportFrom
- Return
- For
Features
- Build expressions and statements as objects, not strings.
- Manage symbol tables for modules, functions, and loops; catch scoping bugs at construction time.
Installation
pip install python-code-builder
Example Usage
# coding=utf-8
from __future__ import print_function
from python_code_builder import *
mod = Module()
Import(mod, module='math')
Assign(mod, 'x', Constant(3))
func = Function(mod, name='foo', args=['a'], decorators=[])
Assign(func, 'y', BinaryOperation(LoadName('a'), BinaryOperator.ADD, LoadName('x')))
loop = For(func, target='item', iterable=Call(LoadName('range'), [Constant(5)], {}))
Assign(loop, 'y', BinaryOperation(LoadName('y'), BinaryOperator.ADD, LoadName('item')))
Return(func, value=LoadName('y'))
print(mod.to_source())
Producing:
import math
x = (3)
def foo(a)
y = (a + x)
for item in range((5)):
y = (y + item)
return y
For-Loop Scoping and Variable Rules
This library enforces a modern, safe local-scoping model for loop variables - unlike standard Python:
| Situation | Allowed? | Notes |
|---|---|---|
| Using an outer variable as a loop target | ❌ | Shadowing outer names is not allowed |
| Reusing the same loop variable name in new loops | ✅ | Each for loop gets a fresh, local variable |
| Accessing a loop variable after the loop ends | ❌ | Loop variable is not available/leaked outside |
Examples
Disallowed (shadowing an outer name)
# coding=utf-8
from __future__ import print_function
from python_code_builder import *
mod = Module()
Assign(mod, 'x', Constant(42))
Assign(mod, 'foo', Constant('ABC'))
For(mod, target='x', iterable=LoadName('foo')) # Raises ValueError!
print(mod.to_source())
Raises:
ValueError: target cannot be defined in an outer scope
Allowed (distinct loops, same target name)
# coding=utf-8
from __future__ import print_function
from python_code_builder import *
mod = Module()
Assign(mod, 'foo', Constant('ABC'))
Assign(mod, 'bar', Constant('123'))
For(mod, target='x', iterable=LoadName('foo'))
For(mod, target='x', iterable=LoadName('bar')) # OK!
print(mod.to_source())
Prints:
foo = ('ABC')
bar = ('123')
for x in foo:
pass
for x in bar:
pass
Each x is local to its own loop; no conflict.
Disallowed (leaking target)
# coding=utf-8
from __future__ import print_function
from python_code_builder import *
mod = Module()
Assign(mod, 'foo', Constant('ABC'))
For(mod, target='x', iterable=LoadName('foo'))
Assign(mod, 'last_x', LoadName('x')) # Raises ValueError!
print(mod.to_source())
Raises:
ValueError: Name x is not defined
Why These Rules?
This strict, explicit scoping:
- Prevents accidental overwriting of names
- Ensures code generation won't result in mysterious NameError at runtime
- Reduces bugs caused by Python's sometimes surprising loop-variable leaks
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
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 python_code_builder-0.0.1a0.tar.gz.
File metadata
- Download URL: python_code_builder-0.0.1a0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c95c356176177cdd9adee2a2a71bedb69ad4d97c48b8c7f41bc8c7efb07fc884
|
|
| MD5 |
f5853b3f8d2b596bb8fc818a78508d20
|
|
| BLAKE2b-256 |
9d2e8018e5499d2688e1d87cee62ca30672513794ea51f442fcfcc73487e7149
|
File details
Details for the file python_code_builder-0.0.1a0-py2.py3-none-any.whl.
File metadata
- Download URL: python_code_builder-0.0.1a0-py2.py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4346224bca521601f6c04ad8bfdcd8462b9efff89bcb189c3a181808816ed952
|
|
| MD5 |
2c2b804ff342b094cdec91f7397b0f28
|
|
| BLAKE2b-256 |
ad3d67a4dc3e65147b832ff274a28070746fed7bf7766fb71b28bc5c0dba902e
|