Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

srcgen: The semantic source code generation framework

Project description


Srcgen is a semantic code generation framework. Being semantic means that the code generator and the generated code bear similar structure, as well ; for instance, the body of an if statement is indented both in the generating and generated code. Here’s a short example:

>>> from srcgen.python import PythonModule
>>> m = PythonModule("foo")
>>> m.import_("sys")
>>> m.import_("os")
>>> m.sep()
>>> m.stmt("x = 5")
>>> with m.if_("x > 8"):
...     m.stmt("print 'oh no'")
>>> with m.else_():
...     m.stmt("print 'oh yes'")
>>> print m
import sys
import os

x = 5
if x > 8:
    print 'oh no'
    print 'oh yes'

You can nest deeper, of course, as well as extract common pieces of code into functions:

m = PythonModule("foo")
with m.class_("MyClass"):
    with m.method("__init__", "


At the moment Srcgen supports:

  • C
  • Python
  • Cython

More languages will be added per public demand (you’re welcome to join the effort!)

Project details

Download files

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

Files for srcgen, version 1.1.0
Filename, size File type Python version Upload date Hashes
Filename, size srcgen-1.1.0.tar.gz (10.3 kB) File type Source Python version None Upload date Hashes View
Filename, size srcgen-1.1.0.win32.exe (208.7 kB) File type Windows Installer Python version any Upload date Hashes View
Filename, size (14.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page