How to call and execute C code in Python?

David Shi davidgshi at yahoo.co.uk
Tue May 15 08:08:14 EDT 2012


How to call and execute C code in Python?

Is there any publication/documentation for this?   For the worst scenario, how many ways are there to call and execute C codes, in Python. 


For instance, having got hold some C codes, attempting to use Python to call and execute C codes.  I.e.  Python, as the integrating language.

Can anyone send publications/instructions to davidgshi at yahoo.co.uk?

All the best to everyone!

Regards.

David



________________________________
 From: "python-list-request at python.org" <python-list-request at python.org>
To: python-list at python.org 
Sent: Sunday, 13 May 2012, 19:14
Subject: Python-list Digest, Vol 104, Issue 67
 
----- Forwarded Message -----

Send Python-list mailing list submissions to
    python-list at python.org

To subscribe or unsubscribe via the World Wide Web, visit
    http://mail.python.org/mailman/listinfo/python-list
or, via email, send a message with subject or body 'help' to
    python-list-request at python.org

You can reach the person managing the list at
    python-list-owner at python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Python-list digest..."

Today's Topics:

   1. Re: Good data structure for finding date intervals including
      a given    date (Arnaud Delobelle)
   2. Re: Good data structure for finding date intervals including
      a given    date (Emile van Sebille)
   3. Re: How to call and execute C code in Python? (Chris Angelico)
   4. Re: How to call and execute C code in Python? (Stefan Behnel)
   5. Re: How to call and execute C code in Python? (Mark Lawrence)
   6. Re: How to call and execute C code in Python? (Mark Lawrence)
   7. Re: How to call and execute C code in Python? (Stefan Behnel)
   8. Re: How to call and execute C code in Python? (Stefan Behnel)
   9. Re: How to call and execute C code in Python? (Mark Lawrence)
  10. Re: How to call and execute C code in Python? (Stefan Behnel)
On 13 May 2012 13:29, Alec Taylor <alec.taylor6 at gmail.com> wrote:
> There is an ordered dict type since Python 3.1[1] and Python 2.7.3[2].

I don't think that'll help the OP.  Python's OrderedDict keeps track
of the order in which the keys were inserted into the dictionary (a
bit like a list), it doesn't keep the keys sorted.

> If you are looking for the best possible self-sorting structure for
> searching, then perhaps you are looking for what's outlined in the
> 2002 article by Han & Thorup: Integer Sorting in O(n sqrt(log log n))
> Expected Time and Linear Space[3].

I can't access it but it seems to me it's not about self sorted data
structures, which is what the OP is looking for.

-- 
Arnaud

On 5/12/2012 5:17 AM Jean-Daniel said...
> Hello,
> 
> I have a long list of n date intervals that gets added or suppressed
> intervals regularly. I am looking for a fast way to find the intervals
> containing a given date, without having to check all intervals (less
> than O(n)).

ISTM the fastest way is to retrieve the list of intervals from a dict using the date as a key.  You don't say how long your list of intervals is, nor how large each interval can be so I don't have enough info to determine the setup reqs, but I'd suspect that a list of tens of thousands of intervals covering ranges of days to weeks would be doable.  If instead you're talking about millions of ranges covering years to decades I'd start elsewhere.

Emile



On Sun, May 13, 2012 at 11:25 PM, David Shi <davidgshi at yahoo.co.uk> wrote:
> Can anyone tell me how to call and exectute C code in Python?

Browse the documentation about Extending and Embedding Python, there's
an extensive API.

Chris Angelico

David Shi, 13.05.2012 15:25:
> Can anyone tell me how to call and exectute C code in Python?

Take a look at Cython, a Python-like language that supports native calls to
and from C/C++ code. It translates your code into very efficient C code, so
the wrapping code tends to be very fast (often faster than hand written C
code).

http://cython.org/

Here are a couple of examples:

http://docs.cython.org/src/tutorial/external.html

There's also the "ctypes" package in the standard library, which is usable
for simple wrapping cases that are not performance critical.

Stefan


On 13/05/2012 16:39, Chris Angelico wrote:
> On Sun, May 13, 2012 at 11:25 PM, David Shi<davidgshi at yahoo.co.uk>  wrote:
>> Can anyone tell me how to call and exectute C code in Python?
>
> Browse the documentation about Extending and Embedding Python, there's
> an extensive API.
>
> Chris Angelico

I like your response, my first thought was to say "yes" :)

-- 
Cheers.

Mark Lawrence.


On 13/05/2012 16:58, Stefan Behnel wrote:
> David Shi, 13.05.2012 15:25:
>> Can anyone tell me how to call and exectute C code in Python?
>
> Take a look at Cython, a Python-like language that supports native calls to
> and from C/C++ code. It translates your code into very efficient C code, so
> the wrapping code tends to be very fast (often faster than hand written C
> code).
>
> http://cython.org/
>
> Here are a couple of examples:
>
> http://docs.cython.org/src/tutorial/external.html
>
> There's also the "ctypes" package in the standard library, which is usable
> for simple wrapping cases that are not performance critical.
>
> Stefan
>

Stefan, you appear to have a lot to do with Cython. It would be polite 
to mention this when replying.

-- 
Cheers.

Mark Lawrence.


Mark Lawrence, 13.05.2012 19:23:
> On 13/05/2012 16:39, Chris Angelico wrote:
>> On Sun, May 13, 2012 at 11:25 PM, David Shi wrote:
>>> Can anyone tell me how to call and exectute C code in Python?
>>
>> Browse the documentation about Extending and Embedding Python, there's
>> an extensive API.
> 
> I like your response, my first thought was to say "yes" :)

It has a serious learning curve all by itself, though, with a lot of major
pitfalls not only for new users. It also requires C fluency, which not
everyone has or wants to learn just to be able to talk to existing C code.
And even when you're up to speed with all that, you will waste a
substantial part of your time debugging crashes, making your code reference
leak free and eventually maintaining it and porting it to different CPython
versions and platforms. Time that IMHO is much better spent adding features
and tuning the code for performance.

Stefan


Mark Lawrence, 13.05.2012 19:27:
> On 13/05/2012 16:58, Stefan Behnel wrote:
>> David Shi, 13.05.2012 15:25:
>>> Can anyone tell me how to call and exectute C code in Python?
>>
>> Take a look at Cython, a Python-like language that supports native calls to
>> and from C/C++ code. It translates your code into very efficient C code, so
>> the wrapping code tends to be very fast (often faster than hand written C
>> code).
>>
>> http://cython.org/
>>
>> Here are a couple of examples:
>>
>> http://docs.cython.org/src/tutorial/external.html
>>
>> There's also the "ctypes" package in the standard library, which is usable
>> for simple wrapping cases that are not performance critical.
> 
> Stefan, you appear to have a lot to do with Cython. It would be polite to
> mention this when replying.

Well, my name is pretty far up both on the project home page and on PyPI,
so it's not like I'm hiding this information. But sure, I'm one of the core
developers and initial founders of the project. So I can assure you that I
know what I'm talking about.

Stefan


On 13/05/2012 18:38, Stefan Behnel wrote:
> Mark Lawrence, 13.05.2012 19:23:
>> On 13/05/2012 16:39, Chris Angelico wrote:
>>> On Sun, May 13, 2012 at 11:25 PM, David Shi wrote:
>>>> Can anyone tell me how to call and exectute C code in Python?
>>>
>>> Browse the documentation about Extending and Embedding Python, there's
>>> an extensive API.
>>
>> I like your response, my first thought was to say "yes" :)
>
> It has a serious learning curve all by itself, though, with a lot of major
> pitfalls not only for new users. It also requires C fluency, which not
> everyone has or wants to learn just to be able to talk to existing C code.
> And even when you're up to speed with all that, you will waste a
> substantial part of your time debugging crashes, making your code reference
> leak free and eventually maintaining it and porting it to different CPython
> versions and platforms. Time that IMHO is much better spent adding features
> and tuning the code for performance.
>
> Stefan
>

Haven't the faintest idea what you're on about so please explain.

-- 
Cheers.

Mark Lawrence.


Mark Lawrence, 13.05.2012 19:44:
> On 13/05/2012 18:38, Stefan Behnel wrote:
>> Mark Lawrence, 13.05.2012 19:23:
>>> On 13/05/2012 16:39, Chris Angelico wrote:
>>>> On Sun, May 13, 2012 at 11:25 PM, David Shi wrote:
>>>>> Can anyone tell me how to call and exectute C code in Python?
>>>>
>>>> Browse the documentation about Extending and Embedding Python, there's
>>>> an extensive API.
>>>
>>> I like your response, my first thought was to say "yes" :)
>>
>> It has a serious learning curve all by itself, though, with a lot of major
>> pitfalls not only for new users. It also requires C fluency, which not
>> everyone has or wants to learn just to be able to talk to existing C code.
>> And even when you're up to speed with all that, you will waste a
>> substantial part of your time debugging crashes, making your code reference
>> leak free and eventually maintaining it and porting it to different CPython
>> versions and platforms. Time that IMHO is much better spent adding features
>> and tuning the code for performance.
> 
> Haven't the faintest idea what you're on about so please explain.

Sure.

Learning curve and pitfalls: almost all C-API code I've seen from new users
had at least one reference leak, usually more than one. This even happens
to core developers from time to time, so I think it's reasonable to assume
that pretty much all users of the C-API have produced at least one
reference leak in their time as programmers and at least some do so
frequently while learning to use it. I call that a common pitfall.

C fluency: obviously, you need to be able to (and want to) write C if you
want to make effective use of CPython's C-API from C code. Not every Python
developer knows C, and why should they?

Wasting time debugging crashes and making your code reference leak free:
well, if you have ref-counting bugs in your code, you have to find them and
fix them. First of all, you have to know they are there, which isn't always
obvious, especially for leaks. The problem with ref-counting bugs is that
the effects (even crashes) are not local in most cases but can happen
anywhere in your program at unpredictable times, so you will spend more
time on average debugging them than with other, more local bugs.

Wasting time maintaining: arguably, maintaining C code requires more work
than maintaining Python code. That's why we use high-level languages in the
first place, isn't it?

Wasting time porting: well, the C-API has subtle to major differences
between CPython versions, so you not only have to learn "the" C-API, but
the differences between these C-APIs, then write or adapt your code in
order to make it work with different versions. Also, being able to write C
code doesn't mean that that code will compile and run with different C
compilers and on different platforms, so more work on that front.

Spending more time adding features and tuning code instead: well, we
designed Cython to relieve programmers from all of the above, so that they
can focus on functionality and performance. And it's pretty good in that
(you may want to read the testimonials on our project home page).
Basically, the idea is that we write C so you don't have to.

Is it clearer what I mean now?

Stefan



-- 
http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120515/731d756b/attachment.html>


More information about the Python-list mailing list