On Wed, Mar 10, 2010 at 4:01 PM, Ondrej Certik <[email protected]> wrote:
> On Wed, Mar 10, 2010 at 3:49 PM, Ondrej Certik <[email protected]> wrote:
>> On Wed, Mar 3, 2010 at 6:23 PM, Lisandro Dalcin <[email protected]> wrote:
>>> On 3 March 2010 23:08, Ondrej Certik <[email protected]> wrote:
>>>>>
>>>>> Am I being clear enough?
>>>>
>>>> Yes, that is clear to me, thanks.
>>>>
>>>
>>> OK. Once you have something working, let's polish it and ask other for
>>> opinions, then push to cython-devel.
>>
>> If I go the global Python() class route, I would need to generate the
>> Python() class automatically, since the list of my Cython functions
>> can be quite long, so I don't want to keep updating the Python class
>> interface by hand.
>>
>> So I am probably going to take the combination of both approaches that
>> you suggested:
>>
>> 1) implement the global Python() class, that takes care of execution
>> contexts, initializing python and the Cython API and has the following
>> methods:
>>
>> void cmd(const char*) // runs a Python command in the (possibly local)
>> context/namespace
>> void insert_object(const char*, PyObject *) // inserts a Python object
>> into the context/namespace
>> PyObject *get_object(const char*) // gets a Python object from the context
>>
>> 2) the helper functions for doing the actual conversion to and from Python 
>> like:
>>
>> static PyObject *(*c2py_CooMatrix)(struct CooMatrix *);
>> static PyObject *(*c2py_CSRMatrix)(struct CSRMatrix *);
>> static PyObject *(*c2py_int)(int);
>> static int (*py2c_int)(PyObject *);
>> static char *(*py2c_str)(PyObject *);
>> static double (*py2c_double)(PyObject *);
>> static PyObject *(*c2numpy_int)(int *, int);
>> static PyObject *(*c2numpy_int_inplace)(int *, int);
>> static PyObject *(*c2numpy_double)(double *, int);
>> static PyObject *(*c2numpy_double_inplace)(double *, int);
>> static void (*numpy2c_int_inplace)(PyObject *, int **, int *);
>> static void (*numpy2c_double_inplace)(PyObject *, double **, int *);
>> .... [way more]
>
> Here is how it would be used:
>
> in the main.cpp:
>
> #include "python_api.h"
>
> int main(int argc, char* argv[])
> {
>    python = Python(argc, argv);
> }
>
> and then somewhere else in the C++ program, like linsystem.cpp:
>
> #include "python_api.h"
>
> ...
> python.cmd("print 'ok'");
> python.insert_object("i", c2py_int(5));
> python.cmd("i += 5");
> int i = py2c_int(python.get_object("i"));
> // i == 10 now
> ...
>
>
>
> This assumes that "python" is a global variable, that is defined in my
> project only, e.g. then the user can define it locally for each class
> that needs to access python. And then, when I expose more of my
> functionality either from C or from Python, I just add more c2py_* and
> py2c_* methods in the .pyx file and that's it.

After I get this working, and test that it indeed works, I'll try to
put it into cython somehow, so that anyone can use it.

One of my friends told me that he prefers Lua because it's super easy
to use from C/C++. Currently unfortunately Python is way more
difficult to use, but with my approach above, Python will be the same
easy to use.

Ondrej
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to