Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / Python

Really simple C++ code generation in Python

Rate me:
Please Sign up or sign in to vote.
4.81/5 (16 votes)
3 Apr 2013CPOL 53.2K   126   30   12
Quickly churn out repetitive C++, C#, or Java code from any data that Python can read.

Introduction

You're allergic to learning curves and want to get your code generated now! Here's an easily customized 100-line Python module just a notch above f.writeline.

Using the code

Let's start with a really simple example (no pointing out that main returns int please! It's an example.)

Python
from CodeGen import *
 
cpp = CppFile("test.cpp")
 
cpp("#include <iostream>")
 
with cpp.block("void main()"):
    for i in range(5):
        cpp('std::cout << "' + str(i) + '" << std::endl;')
    
cpp.close()

The output:

C++
#include <iostream>
void main()
{
    std::cout << "0" << std::endl;
    std::cout << "1" << std::endl;
    std::cout << "2" << std::endl;
    std::cout << "3" << std::endl;
    std::cout << "4" << std::endl;
}

Leveraging Python's with keyword we can encapsulate statements in {} blocks which get closed out automatically, with the correct indentation so that the output remains human-readable.

Substitutions

When generating more sophisticated code, you'll quickly realize your Python script becomes progressively less readable with ugly string concatenations all over the place. That's where the subs method come in:

Python
from CodeGen import *
 
cpp = CppFile("test.cpp")
 
cpp("#include <iostream>")
 
with cpp.block("void main()"):
    for i in range(5):
        with cpp.subs(i=str(i), xi="x"+str(i+1)):
            cpp('int $xi$ = $i$;')
    
cpp.close()

which produces:

C++
#include <iostream>

void main()
{
    int x1 = 0;
    int x2 = 1;
    int x3 = 2;
    int x4 = 3;
    int x5 = 4;
}

The substitutions are valid within the Python with block, which can be nested.

That's pretty much it

To finish off here's a more complicated example to generate the following code:

C++
class ClassX
{
protected:
    A a;
    B b;
    C c;
public:
    ClassX(A a, B b, C c)
    {
        this->a = a;
        this->b = b;
        this->c = c;
    }
    A getA()
    {
        return this->a;
    }
    B getB()
    {
        return this->b;
    }
    C getC()
    {
        return this->c;
    }
};
class ClassY
{
protected:
    P p;
    Q q;
public:
    ClassY(P p, Q q)
    {
        this->p = p;
        this->q = q;
    }
    P getP()
    {
        return this->p;
    }
    Q getQ()
    {
        return this->q;
    }
};

Take a look at CodeGen.py to see how the label function is implemented for a hint on how to extend the code with language-specific features.

Python
from CodeGen import *
 
cpp = CppFile("test.cpp")
 
CLASS_NAMES = ["ClassX", "ClassY"]
VAR_NAMES = { "ClassX": ["A", "B", "C"], 
                      "ClassY": ["P","Q"] }
 
for className in CLASS_NAMES:
    with cpp.subs(ClassName=className):
        with cpp.block("class $ClassName$", ";"):
            cpp.label("protected")
            for varName in VAR_NAMES[className]:
                with cpp.subs(A=varName, a=varName.lower()):
                    cpp("$A$ $a$;")
            cpp.label("public")
            with cpp.subs(**{"A a": ", ".join([x + " " + x.lower() for x in VAR_NAMES[className]])}):
                with cpp.block("$ClassName$($A a$)"):
                    for varName in VAR_NAMES[className]:
                        with cpp.subs(a=varName.lower()):
                            cpp("this->$a$ = $a$;")
            for varName in VAR_NAMES[className]:
                with cpp.subs(A=varName, a=varName.lower(), getA="get"+varName):
                    with cpp.block("$A$ $getA$()"):
                        cpp("return this->$a$;")
    
cpp.close()

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionusing the CodeGen library Pin
Member 1398507014-Sep-18 14:08
Member 1398507014-Sep-18 14:08 
PraiseGreat Tool Pin
tata narayana19-May-17 1:51
tata narayana19-May-17 1:51 
Questiongreat!! Pin
Hagit Maryuma11-Feb-16 3:35
Hagit Maryuma11-Feb-16 3:35 
GeneralI like this Pin
phillipvoyle15-Jul-15 14:47
phillipvoyle15-Jul-15 14:47 
QuestionI don't seem to be able to view the code Pin
Member 1094921215-Jul-14 17:06
Member 1094921215-Jul-14 17:06 
AnswerRe: I don't seem to be able to view the code Pin
Member 1098083830-Jul-14 2:42
Member 1098083830-Jul-14 2:42 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA10-May-13 18:37
professionalȘtefan-Mihai MOGA10-May-13 18:37 
GeneralMy vote of 5 Pin
morgr7-Apr-13 23:13
morgr7-Apr-13 23:13 
GeneralMy vote of 1 Pin
Ali Khanlarkhani5-Apr-13 20:25
Ali Khanlarkhani5-Apr-13 20:25 
GeneralRe: My vote of 1 Pin
Eric Reynolds5-Apr-13 20:40
Eric Reynolds5-Apr-13 20:40 
GeneralRe: My vote of 1 Pin
Ali Khanlarkhani5-Apr-13 21:13
Ali Khanlarkhani5-Apr-13 21:13 
GeneralRe: My vote of 1 Pin
Eric Reynolds6-Apr-13 3:32
Eric Reynolds6-Apr-13 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.