Hi Terry,

thanks a lot for this huge list of feedback. I'm forwarding it to the two 
Cython mailing lists here. We're about to release 0.14.1 real soon, so this 
is the perfect time to go through the documentation and fix it up.

Thanks!

Stefan


-------- Original-Nachricht --------
Betreff: Re: [OT] Python like lanugages [was Re: After C++, what with Python?]
Datum: Mon, 17 Jan 2011 16:38:34 -0500
Von: Terry Reedy <[email protected]>
An: Stefan Behnel <[email protected]>


> So seriously need to take a look at Cython.
>
> http://cython.org

OK. Some comments...

http://docs.cython.org/src/quickstart/build.html
Building a Cython module using distutils

   This section jumps from that topic to Sage notebook use.
It seems that a section heading is missing.

http://docs.cython.org/src/quickstart/cythonize.html
Typing Variables

Simply compiling this in Cython merely gives a 35% speedup.

   With what arguments? Suggest inserting "for integrate_f(0,2,.1)" or
whatever.

<same page>
Typing Functions

cdef double f(double) except? -2:
     return x**2-x

   I presume '...f(double x)...'

"The except? -2 means that an error will be checked for if -2 is
returned (though the ? indicates that -2 may also be used as a valid
return value)."

   Huh? the minimum value is -.25 at .5, so why '-2' versus -100000 or
any other impossible return?

Speedup: 150 times over pure Python.

   I presume this refers back to non-cdefed integrate_f. Perhaps change
this to "Integrate_f(...) is now 150 times as fast as the original pure
Python version.

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

It is perfectly OK do from math import sin to use Python’s sin()
function. . However, calling C’s own sin() function is substantially
faster, especially in tight loops.

   This and some of the following seem to be missing a bit of context.
My suggested expansion.

Suppose in the previous integration example we change f to return
sin(x*x) instead of x**2-x. We could do from math import Python's sin()
function. However, this reintroduces a slow Python call within the main
for loop. Calling C’s own sin() is substantially faster.

http://docs.cython.org/src/tutorial/clibraries.html
Using C libraries

As seen for the C string decoding functions above,

   ??? This is the beginning of the second sentence and the only thing
'above' is the integrate example and C sin function.

it is actually trivial to call C functions directly in the code. The
following describes what needs to be done to use an external C library
in Cython code.

This seems like it is drawing a false distinction, in that calling the C
sin function *is* use of the external math library where it resides. (Or
am I missing an important distinction?) Thus is seems to unnecessarily
disconnect what the reader already knows from the following much more
complicated example. Could

cdef extern from "math.h":
     double sin(double)

be put in a separate .pxd file, that is then imported? If so, I suggest
showing that variation of the easy example and then go to the new and
much harder example.

cimport python_exc

   This is, I believe, the first mention of 'python_exc' and there is no
explanation thereafter. My guess by analogy: it is a Cython-supplied
.pxd (like cqueue.pxd) that (re)defines the c-api to builtin Python
exceptions. (Reading further I see 'python_exc.PyErr_NoMemory()', so my
guess is not completely right.)

PYTHONPATH=. python -c 'import queue.Queue as Q; Q()'

   Is this missing a ; or something?

...the pop() method is almost identical.

         if cqueue.queue_is_empty(self._c_queue):
             raise IndexError("Queue is empty")

   If the queue had one item (0) before the pop, would not this be true
*after* the pop, and thus the exception erroneous?  Unlike with peeking,
I think you need to unconditionally check before the pop.

def __nonzero__(self):

   Adding '# __bool__ in Python 3' would be nice.

The following listing shows the complete implementation that uses cpdef
methods where possible. This feature is obviously not available for the
extend() method, as the method signature is incompatible with Python
argument types.

   I would like the example to show how to deal with this to make the
class completely usable from Python. Could not one rename 'extend' as
'extend_c' and define extend with Python types. Would

     cpdef extend(self, it):
         for i in it:
             self.append(i)

work? Would append raise an error for non ints. Or how about

     cpdef extend(self, it):
         for i in it:
             if isinstance(i,int) and lo <= i <= hi:
                 if not cqueue.queue_push_tail(self._c_queue,
                                       <void*>value):
                      python_exc.PyErr_NoMemory()
             else: raise ValueError('bad value')

with 'lo' and 'hi' replaced with appropriate expressions.

http://docs.cython.org/src/tutorial/cdef_classes.html
Extension types

Cython supports a second kind of class: extension types,

   We have, of course, just seen one -- cdef class Queue:

Since the argument is typed, we need to check whether it is None.

   I do not get why the special concern for None. It seems to me that
any object other than a Function would (likely) have an incompatible
internal structure. In other words, why is the check not

   if not isinstance(f, Function):
       raise ValueError("f must be a Function instance")

A few other details confuse me, but enough for now. My main interest at
the moment is whether Cython is a viable third method (versus swig and
ctypes) for wrapping C library code for access from Python. It seems to
sit in between somewhat.

-- 
Terry Jan Reedy

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

Reply via email to