Skip to main content

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',), 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_code_builder-0.0.1a2.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_code_builder-0.0.1a2-py2.py3-none-any.whl (7.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file python_code_builder-0.0.1a2.tar.gz.

File metadata

  • Download URL: python_code_builder-0.0.1a2.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for python_code_builder-0.0.1a2.tar.gz
Algorithm Hash digest
SHA256 c80fd7fce7df44846bedc2d1c5dc424f5de05a08f37e44a077c4095ecf4eebf9
MD5 22d05e153ddee6965e30407149620040
BLAKE2b-256 0d84737ab1d98f5302e534115d035899a7e0751ea87511b3475b5612bf8c6b0a

See more details on using hashes here.

File details

Details for the file python_code_builder-0.0.1a2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for python_code_builder-0.0.1a2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b0d2220476fbb177eeff211d82cf9b7a16f77f84c4c932d6e3e076a64a846f15
MD5 da90db2cfd2bcdc7aa25cf0aa6268c3c
BLAKE2b-256 d8d8dd1ccb45d1185bb2d45120a932dc578958bebf52d389d1ce60ecf985962e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page