u/GianniMariani

▲ 1 r/Python

I published astichi v0.1.0.

It is a Python library for AST-based source composition: write valid Python-shaped snippets with marker calls such as astichi_hole(body), wire snippets together, and materialize final runnable Python source.

The goal is to let code generators produce boring, inspectable Python without string-template surgery.

Astichi handles:

  • insertion sites for blocks, expressions, function parameters, and call args
  • name collisions between inserted snippets
  • explicit cross-snippet name wiring
  • managed imports that participate in generated-name handling
  • compile-time loop unrolling
  • describe() metadata so snippets can be inspected and wired from data
  • astichi_comment("..."), which renders final # comments even though normal Python AST parsing drops comments
  • provenance-aware emission for round-trip tests

Two API details that are central to the design:

describe() exposes holes, external binds, productions, and target addresses, so a generator can inspect a snippet and wire it with data instead of hard-coded fluent calls:

for hole in root.describe().holes:
    print(hole.name, hole.address)

astichi_comment("...") is a statement marker for generated comments. Normal Python ASTs drop comments, so emit_commented() materializes the program and then renders those markers as real comments:

astichi.compile('astichi_comment("generated from {__file__}:{__line__}")\n').emit_commented()

Links:

Feedback on the API shape would be useful, especially from people who maintain code generators.

reddit.com
u/GianniMariani — 10 days ago