O'Reilly logo

Learning Cython Programming by Philip Herron

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Python ConfigParser

Another useful case is to make Python's ConfigParser accessible in some way from C; ideally, all we really want is to have a function to which we pass the path to a config file to receive a STATUS OK/FAIL message and a filled buffer of the configuration that we need:

from ConfigParser import SafeConfigParser, NoSectionError
cdef extern from "main.h":
    struct config:
        char * path
        int number
cdef config myconfig

Here, we've Cythoned our struct and declared an instance on the stack for easier management:

cdef public config * parseConfig (char * cfg): # initialize the global stack variable for our config... myconfig.path = NULL myconfig.number = 0 # buffers for assigning python types into C types cdef char * path = NULL cdef number ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required