Introducing cython (and a small skeleton application to bind C++ classes)

69 views
Skip to first unread message

Dilawar

unread,
Jul 10, 2014, 7:34:11 AM7/10/14
to wncc...@googlegroups.com
(Almost) everything in Python is object. That brings convenience but also makes it slow. Loops in python are really slow.

Pyrex is another implementation of python which adds types to the language. Cython takes advantages of this static typing and can convert Python code into very good C/C++ code. This can improve the performance of Python programs a lot (or one can move to Julia language).

This example shows how to reuse a class written in C++. It creates bindings to python and produce a module which can be imported into Python. This is not an introductory example; it is meant for tweaking and running.

For beginning,  you should consult Cython tutorial, and go through the basic of language as well.

This example can be found here https://github.com/dilawar/cython-example .

The `setup.py` file is quite generic; it can easily be reused in a new cython application.  This example can also be used as a starting point of your cython application: change the class and its function (and also the file name), modify setup file a bit to reflect changes in filename; and you have a framework for calling C++ in python. See the `test.py` file how to call the generated module into Python. Script `./run.sh` runs the example.

Dilawar

Vinayak Gagrani

unread,
Jul 10, 2014, 10:18:36 PM7/10/14
to wncc_iitb
Better to use Boost. That way you need not alter any existing classes also.
Boost is wrapper on the Python/C API provided by python. One can use that also
but its very low level and you need to keep track of pointers passed around C++ and Python.


--
--
The website for the club is http://wncc-iitb.org/
To post to this group, send email to wncc...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Web and Coding Club IIT Bombay" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wncc_iitb+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Vinayak Gagrani 
Quantitative Researcher | Jump Trading International Limited, London
B.Tech (Hons.) Computer Science and Engineering Indian Institute of Technology, Bombay
+44-74-055-49-288 | http://www.cse.iitb.ac.in/alumni/~gagrani09 | Skype id : vinayak.gagrani

Dilawar Singh

unread,
Jul 10, 2014, 10:43:06 PM7/10/14
to wncc...@googlegroups.com
Boost::Python is a great option if one is already using boost framework. Skewed.de
graph-tool uses boost::python to wrap boost::graph for python. Personally, I
have never used boost::python (thought big fan to boost::graph) - so no comments.

Cython does not touch the original C++ classes. Few wrappers need to be written
and thats all. Cython is actively supported by official python.

Only recently, I've started using Cython only recently in some serious way --
porting Moose simulator onto cython as hobby. Behind the scene, it uses
Python/C/C++ API.

Dilawar

On Thu, Jul 10, 2014 at 11:01:33PM +0100, Vinayak Gagrani wrote:
>Better to use Boost. That way you need not alter any existing classes also.
>Boost is wrapper on the Python/C API provided by python. One can use that also
>but its very low level and you need to keep track of pointers passed around C++
>and Python.
>
>
>On Thu, Jul 10, 2014 at 12:34 PM, Dilawar <dilawar....@gmail.com> wrote:
>
>> (Almost) everything in Python is object. That brings convenience but also
>> makes it slow. Loops in python are really slow.
>>
>> Pyrex is another implementation of python which adds types to the language.
>> Cython takes advantages of this static typing and can convert Python code
>> into very good C/C++ code. This can improve the performance of Python
>> programs a lot (or one can move to Julia language).
>>
>> This example shows how to reuse a class written in C++. It creates bindings
>> to python and produce a module which can be imported into Python. *This is
>> not an introductory example*; it is meant for tweaking and running.
>>
>> For beginning, you should consult Cython tutorial, and go through the basic
>> of language as well.
>>
>> This example can be found here https://github.com/dilawar/cython-example .
>>
>> The `setup.py` file is quite generic; it can easily be reused in a new cython
>> application. This example can also be used as a starting point of your
>> cython application: change the class and its function (and also the file
>> name), modify setup file a bit to reflect changes in filename; and you have a
>> framework for calling C++ in python. See the `test.py` file how to call the
>> generated module into Python. Script `./run.sh` runs the example.
>>
>> Dilawar
>>
>> -- -- The website for the club is http://wncc-iitb.org/ To post to this
>> group, send email to wncc...@googlegroups.com --- You received this
>> message because you are subscribed to the Google Groups "Web and Coding Club
>> IIT Bombay" group. To unsubscribe from this group and stop receiving emails
>> from it, send an email to wncc_iitb+...@googlegroups.com. For more
>> options, visit https://groups.google.com/d/optout.
>>
>
>
>
>-- *Vinayak Gagrani* Quantitative Researcher | Jump Trading International
>Limited, London B.Tech (Hons.) | Computer Science and Engineering | Indian
>Institute of Technology, Bombay +44-74-055-49-288 |
>http://www.cse.iitb.ac.in/alumni/~gagrani09 | Skype id : vinayak.gagrani
>
Reply all
Reply to author
Forward
0 new messages