Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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',), varargs=None, kwonlyargs=(), varkwargs=None, 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.

Release files for python-code-builder 0.0.1a2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-code-builder 0.0.1a2
File Size Uploaded
python_code_builder-0.0.1a2.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-code-builder 0.0.1a2
File Interpreter ABI Platform
python_code_builder-0.0.1a2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 14.5 kB

Release files / python_code_builder-0.0.1a2.tar.gz

Download URL python_code_builder-0.0.1a2.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c80fd7fce7df44846bedc2d1c5dc424f5de05a08f37e44a077c4095ecf4eebf9
BLAKE2b-256 checksum
How to use checksums
0d84737ab1d98f5302e534115d035899a7e0751ea87511b3475b5612bf8c6b0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / python_code_builder-0.0.1a2-py2.py3-none-any.whl

Download URL python_code_builder-0.0.1a2-py2.py3-none-any.whl
Size 7.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b0d2220476fbb177eeff211d82cf9b7a16f77f84c4c932d6e3e076a64a846f15
BLAKE2b-256 checksum
How to use checksums
d8d8dd1ccb45d1185bb2d45120a932dc578958bebf52d389d1ce60ecf985962e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page