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

Source code at Sourceforge

Background

Tutorial

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 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>
        // import the Python function to use in C
        DEF double myround double
        printf("%s\n", status);
        double sum, sumsq = 0.0;
        for(int i=0;i<datalen; i++) {
            sum += data[i]; // access Python arrays
        }
        // modify Python variable
        mean = sum / datalen;
        for(int i=0;i<datalen; i++) {
            sumsq += pow((data[i] - mean),2);
        }
        // call Python code, passing variables
        stddev = myround(sqrt(sumsq / (datalen-1)));
        mean = myround(mean);
        status = "Done";
        """)
    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-2016 by Fernando Trias. All righs reserved.