Re: Converting Python code to C/C++

8 views
Skip to first unread message

Kurt Smith

unread,
Jun 23, 2009, 11:49:53 AM6/23/09
to pytho...@python.org
On Mon, Jun 22, 2009 at 9:49 PM, Andras
Pikler<Andras...@students.olin.edu> wrote:
> Hi!
>
>
>
> Short: I need to turn a Python program that I (mostly) wrote into C code,
> and I am at a loss.
>
>
>
> Long: I’m doing research/programming for a professor, and we are working
> with MIDI files (a type of simple music file). The research deals with
> generating variations from a musical melody; currently, my Python code uses
> a Python midi package I found online to read the notes in question from a
> midi file, about 350 lines of my own code to generate a variation based on
> these notes and the professor’s algorithms, and finally the package again to
> write the new melody to another midi file.
>
>
>
> Now, my professor would like to have this exact code in C/C++, as she
> believes C is more compatible with MATLAB, and wants the code to be
> available in multiple languages in case a programmer works for her in the
> future who knows C but not Python. While I know a tiny bit of C (emphasis on
> the tiny), I would much prefer if there were some sort of automatic compiler
> I could use to turn my Python code into C than taking a week or two or three
> to learn the minimum I need about C, find a way to access MIDI files in it,
> and rewrite all of my code.
>
>
>
> After some googling, I found and tried Shedskin, but it doesn’t work, as the
> Python midi package I’m using uses modules which Shedskin does not support.
> Otherwise, I haven’t found much. Is there anything out there to help me do
> this? If not, from anyone who has experience in this regard, how daunting
> should I expect this to be?


Taking on C from a cold start and being able to handle the ins and
outs of interfacing with Python isn't something that's feasible in
'two or three weeks'. Here are a couple of options -- take 'em or
leave 'em:

1) Put the code in Cython: http://www.cython.org/ (full disclosure:
I'm doing a GSoC project with Cython). It will convert pretty much
any python code into C code (even closures are supported in the most
recent version, I think), and the C code can then be compiled into an
extension module.

The only problem with the above is the C code isn't, at first blush,
easy to read. Nor is it supposed to be changed by the user. So that
leads us to option...

2) Write the core functionality in C yourself, and then wrap those C
functions in Cython. You'll want to take a look at the documentation:

http://docs.cython.org/

and, more specifically on wrapping C code:

http://docs.cython.org/docs/external_C_code.html

I don't think you'll be able to avoid learning C, though.

Kurt

Couper, Tim T

unread,
Jun 24, 2009, 3:15:06 AM6/24/09
to pytho...@python.org
Your prof. may find this thread of interest

http://mail.python.org/pipermail/python-list/2000-June/039779.html

My experience is that developers who know C and C++ can be productive in
less than 1 week in python, and find it liberating, and educational, to
do so. And at the same time they will have added a second language to
their toolbox. As Kurt points out, learning C/C++ takes considerably
longer (weeks/months to attain a level of competence).

Python is now used in a number of universities as the language in which
to teach comp sci undergraduate courses (I know of Leeds, & MIT),
biomathematics, and my daughter just finished her PhD in speech and
language processing at Edinburgh .. using python and Matplotlib .. as
the extensive C/C++ libraries in that infomatics world are wrapped in
python - and the MSc Comp Sci course has replaced Java as the language
for teaching with Python.

Dr Tim Couper

http://docs.cython.org/

http://docs.cython.org/docs/external_C_code.html

Kurt
--
http://mail.python.org/mailman/listinfo/python-list


*****************************************************************************
This communication is sent by the Standard Bank Plc or one of its affiliates
The registered details of Standard Bank Plc are:
Registered in England No. 2130447, Registered Office 25 Dowgate Hill London EC4R 2SB
Authorised and Regulated by the Financial Services Authority.

More information on Standard Bank is available at www.standardbank.com

Everything in this email and any attachments relating to the official business of Standard Bank Group Limited and any or all subsidiaries, the Company, is proprietary to the Company. It is confidential, legally privileged and protected by relevant laws. The Company does not own and endorse any other content. Views and opinions are those of the sender unless clearly stated as being that of the Company.

The person or persons addressed in this email are the sole authorised recipient. Please notify the sender immediately if it has unintentionally, or inadvertently reached you and do not read, disclose or use the content in any way and delete this e-mail from your system.

The Company cannot ensure that the integrity of this email has beenmaintained nor that it is free of errors, virus, interception or interference. The sender therefore does not accept liability for any errors or omissions in the contents of this message which arise as a result of e-mail transmission. If verification is required please request a hard-copy version. This message is provided for informational purposes and should not be construed as a solicitation or offer to buy or sell any securities or related financial instruments.

*****************************************************************************

Grant Edwards

unread,
Jun 24, 2009, 10:10:43 AM6/24/09
to
On 2009-06-24, Couper, Tim T <Tim.C...@standardbank.com> wrote:

> Your prof. may find this thread of interest
>
> http://mail.python.org/pipermail/python-list/2000-June/039779.html
>
> My experience is that developers who know C and C++ can be productive in
> less than 1 week in python, and find it liberating, and educational, to
> do so. And at the same time they will have added a second language to
> their toolbox. As Kurt points out, learning C/C++ takes considerably
> longer (weeks/months to attain a level of competence).

I agree. Your professor is deluded and knows nothing about
software development [not that either is particularly unusual
in an academic setting]. Converting a Python program to C or
C++ is a complete waste of time (both now _and_ later) unless
there are severe, insurmountable performance problems with the
Python version.

Python is a far, far better language for both real-world
production application development and for algorithm R&D. With
Python, you spend your time working on algorithms and solving
real-world problems. In C or C++, you spend your time fighting
with the bugs in your code that are preventing the program from
running. An algorithm that takes a few hours to implement and
test in Python will take weeks in C or C++.

That said, you'll be niether the first nor last person to be
forced by a professor or manager to waste weeks or months of
time doing something that's obviously stupid and futile from a
technical point of view.

--
Grant Edwards grante Yow! What's the MATTER
at Sid? ... Is your BEVERAGE
visi.com unsatisfactory?

bobicanprogram

unread,
Jun 24, 2009, 12:01:13 PM6/24/09
to


3) use Python-SIMPL to connect your C module to your Python module
using a 5 function API without any need for wrappers. ie. have your
cake and eat it too

http://www.icanprogram.com/06py/main.html

bob

Scott David Daniels

unread,
Jun 24, 2009, 12:10:50 PM6/24/09
to
Couper, Tim T wrote:
> ... My experience is that developers who know C and C++ can be productive

> in less than 1 week in python, and find it liberating, and educational, to
> do so. And at the same time they will have added a second language to
> their toolbox. As Kurt points out, learning C/C++ takes considerably
> longer (weeks/months to attain a level of competence).

Yup. Remember that "be productive" is not quite the same as "master."
The nice thing is that the mastery comes on a gentle slope, adding to
your productivity without requiring drastic rethinking of all you
understood (as is done to physics students, for example).

> Python is now used in a number of universities as the language in which
> to teach comp sci undergraduate courses (I know of Leeds, & MIT),
> biomathematics, and my daughter just finished her PhD in speech and
> language processing at Edinburgh .. using python and Matplotlib .. as
> the extensive C/C++ libraries in that infomatics world are wrapped in
> python - and the MSc Comp Sci course has replaced Java as the language
> for teaching with Python.

As a data point, Georgia Tech (I believe) had a two-semester Computer
Science intro course in C++. In introducing a Python intro course, they
provided for a few years both the Python and the C++ first semester,
and kept the C++ second semester. They found no measurable difference
in the performance on the second course (still in C++) even though the
Python course people had to learn a new language in addition to learning
the rest of the coursework. The first course is now in Python, since
at the end of the two-semester sequence they know two languages and
apparently suffer no compensating loss.

--Scott David Daniels
Scott....@Acm.Org

Terry Reedy

unread,
Jun 24, 2009, 1:25:12 PM6/24/09
to pytho...@python.org

>> Short: I need to turn a Python program that I (mostly) wrote into C
>> code, and I am at a loss.
>>
>> Now, my professor would like to have this exact code in C/C++, as she
>> believes C is more compatible with MATLAB, and wants the code to be
>> available in multiple languages in case a programmer works for her in
>> the future who knows C but not Python. While I know a tiny bit of C
>> (emphasis on the tiny),

Your professor should wait until your Python version is complete and in
final form and until a C version is needed. Then hire someone competent
in both languages to do the translation.

Grant Edwards

unread,
Jun 24, 2009, 2:35:34 PM6/24/09
to
On 2009-06-24, Terry Reedy <tjr...@udel.edu> wrote:

> Your professor should wait until your Python version is complete and in
> final form and until a C version is needed. Then hire someone competent
> in both languages to do the translation.

Professor... hire...

Good one! :)

--
Grant Edwards grante Yow! BARRY ... That was
at the most HEART-WARMING
visi.com rendition of "I DID IT MY
WAY" I've ever heard!!

Neuruss

unread,
Jun 24, 2009, 7:44:48 PM6/24/09
to

There's another (very good) option: Try shedskin http://code.google.com/p/shedskin/
.
Shedskin can compile a whole program or part of it as an extension
module.
It translates python code to c++ and compiles it.

The good thing is that you don't have to know anything about c or c++.
You simply have to restrict your coding style a little bit to make it
explicitly static.
For example: if you declare a = 5, that means that "a" is an integer,
so you cannot then change it to a string (a = "hello", won't work).

Luis

Carl Banks

unread,
Jun 24, 2009, 9:42:25 PM6/24/09
to
On Jun 24, 7:10 am, Grant Edwards <invalid@invalid> wrote:

> On 2009-06-24, Couper, Tim T <Tim.Cou...@standardbank.com> wrote:
>
> > Your prof. may find this thread of interest
>
> >http://mail.python.org/pipermail/python-list/2000-June/039779.html
>
> > My experience is that developers who know C and C++ can be productive in
> > less than 1 week in python, and find it liberating, and educational, to
> > do so. And at the same time they will have added a second language to
> > their toolbox. As Kurt points out, learning C/C++ takes considerably
> > longer (weeks/months to attain a level of competence).
>
> I agree.  Your professor is deluded and knows nothing about
> software development [not that either is particularly unusual
> in an academic setting].  Converting a Python program to C or
> C++ is a complete waste of time (both now _and_ later) unless
> there are severe, insurmountable performance problems with the
> Python version.

What if the point of asking a student to convert Python to C is to
teach them this:

> Python is a far, far better language for both real-world
> production application development and for algorithm R&D.  With
> Python, you spend your time working on algorithms and solving
> real-world problems.  In C or C++, you spend your time fighting
> with the bugs in your code that are preventing the program from
> running.  An algorithm that takes a few hours to implement and
> test in Python will take weeks in C or C++.

If that [teaching them this] is the case, it might be best use of time
they will ever have.


Carl Banks

Reply all
Reply to author
Forward
0 new messages