PyEmbedC: Python Module for embedding C/C++

PyEmbedC

About this project:

Python module to embed C/C++ code within Python programs and scripts.

Download PyEmbedC files

Sourceforge project

Background

Tutorial

Quit start

easy_install embedc

Join this project

To join this project, please contact the project administrators of this project, as shown on the project summary page.

Get the source code

Source code for this project may be available as downloads or through one of the SCM repositories used by the project, as accessible from the project develop page.

 

Sample code:

from embedc import C

def myround(number):
    return round(number, 1)

def test(data):
    datalen = len(data)
    mean = 0.0
    stddev = 0.0
    status="Calculate statistics"
    C("""
        #include <math.h>
        DEF double myround double
        printf("%s: ", status);
        double sum, sumsq = 0.0;
        for(int i=0;i<datalen; i++) {
            sum += data[i];
        }
        mean = sum / datalen;
        for(int i=0;i<datalen; i++) {
            sumsq += pow((data[i] - mean),2);
        }
        stddev = myround(sqrt(
            sumsq / (datalen-1)));
        mean = myround(mean);
        status = "Done";
        fflush(stdout);
        """)
    print("Mean = %f" % mean)
    print("Stddev = %f" % stddev)
    print(status)

samples=(10.5,15.1,14.6,12.3,19.8,17.1,6.1)
test(samples)

Output:

Calculate statistics
Mean = 13.600000
Stddev = 4.500000
Done

Copyright 2010 by Fernando Trias. All righs reserved.