> Finally, if I have in my .pyx a function :
>
> def count():
> <do something with variables a,b,c>
>
> does it change anything to write the following in the .pxd? :
>
> cpdef inline count():
> cdef:
> int a
> int b
> int c
This specific example will definitely not work as expected, but providing
inline functions in .pxd files generally does work. However, I'd rather use
the @cython.inline and @cython.ccall decorators inside of the .py file.
BTW, the current "pure Python mode" docs aren't particularly well
accessible. They are more like a reference than a howto guide. If someone
feels like writing up a little smoother document here, I'm sure it would
make a lot of people out there happy.
In that case, you should declare it as an extension type in the .pxd file
(and drop the @locals()):
cdef class C:
cdef int start
cdef int end
BTW, the current "pure Python mode" docs aren't particularly well
accessible. They are more like a reference than a howto guide. If someone
feels like writing up a little smoother document here, I'm sure it would
make a lot of people out there happy.
I totally agree. Unless the pure Python mode itself is discouraged... but it makes things much easier to test and maintain, and more understandable by other participants to the project.
--
---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I think this is a place where "newbies" can help a lot -- you know better than the core developers what initial explanation is needed. Perhaps you could write up a tutorial following your process in "cythonizing" your code in pure-python mode.Of course, it would require review by the "experts", but I find we're a lot more likley to get helpful comments and review than getting one of them to write the whole thing from scratch!
How is cythonize more limited? The primary reason the docs do
otherwise is that it didn't exist before.
> * Then two methods:
>
> a) Create A.pxd, that is not meant for it but can be used nonetheless to
> override python methods by cython ones. It has limitations - to be listed -
> over method b).
> Do not rename to A.pyx because it would ignore A.pxd at compilation time.
> To override functions, one must use "cpdef inline".
Huh?
> To my opinion the doc lacks an example of a full class with __init__,
> __cinit__, arguments to __init__ and their default values.
Please contribute!
- Robert
--
Yes, as a draft tutorial for the pure Python mode, which documentation is not very clear atm. I struggled a lot with it as a usual Python developer. That is, how to not touch the Python script but create a corresponding .pxd file that adds the highest speed boost after compilation.
--[No idea why there is a "cythonize" function but other examples with "Extension", doing the same while "cythonize" is more limited]--
a) Create A.pxd, that is not meant for it but can be used nonetheless to override python methods by cython ones.
It has limitations - to be listed - over method b).
Do not rename to A.pyx because it would ignore A.pxd at compilation time.
To override functions, one must use "cpdef inline".
To override classes, one must usecdef class <name>:
cpdef <type> <future attribute>
For instance, the python classclass C(object):
def __init__(self, a=0, b=0):
self.a = a
self.b = b
can be overriden by specifying the following in the .pxd:
cdef class C:
cpdef int a,b
so that it is in the end equivalent to the folowing Cython code:cdef class C(object):
cdef int a,b
def __init__(self, int a=0, int b=0):
self.a = a
self.b = b
--[Btw. no idea if it is necessary to specify the type at "cdef int a,b" as well as in the __init__ arguments, in the Cython code]--
b) [Not really full Python but useful if some parts are not subject to changes]
Write B.pyx. Move some functions that need optimization from A.py to B.pyx. Rewrite functions in B.pyx using Cython's syntax.
Also, "to make them accessible from Python code, you need to declare them as "public" or "readonly" (source) [uh? I think "cpdef" does that]. Compile B.pyx with a separate setupB.py. Then in A.py import (not cimport because it is only when there is a .pxd) functions and classes from B. Finally, compile A.py with setupA.py.
* How to debug (still trying).
To my opinion the doc lacks an example of a full class with __init__, __cinit__, arguments to __init__ and their default values.
On Thu, May 29, 2014 at 8:43 AM, Julien Delafontaine <mura...@gmail.com> wrote:
Yes, as a draft tutorial for the pure Python mode, which documentation is not very clear atm. I struggled a lot with it as a usual Python developer. That is, how to not touch the Python script but create a corresponding .pxd file that adds the highest speed boost after compilation.OK -- I think this is a worthwhile contribution, and it does change a bit the workflow I'd suggest.Perhaps best would be for you to fork the repo, add this page, then we can all work on it in that repo, and you can submit a pull request when we think it's ready.
But in the meantime:* From A.py, how to create the setupA.py, compile and see how you gain 20% speed.
I'd suggest a simple setup.py example, and I'd have it build an Extension object, then use cythonize on that extension object - it's a bit more clear that way. Though now that I think about it -- I'm not sure how to build an extension in pure python mode -- have you got that working?
you can do pure python mode with the special functions and decorators. It may be better to start off that way, rather than the *.pxd approach. OR jump straight ot the *.pxd -- perhaps someone else can suggest the best approach for newbies, and/or provide a little advice on why one might use one method or the other.
Do not rename to A.pyx because it would ignore A.pxd at compilation time.
I'm not sure I understand this -- though I haven't used pure python mode beyond a little experimenting, ...
* How to debug (still trying).well, debugging is a challenge!But one stage you should highlight is calling cython -a to get an html annotated version, then looking for the yellow to see where a lot of python calls are being made -- that will tell you what still needs to be augmented for full Cython performance.
Perhaps best would be for you to fork the repo, add this page, then we can all work on it in that repo, and you can submit a pull request when we think it's ready.
Ok, I will do that.
At first I created and extension and did "ext_modules = [extensions]", ran it and it worked... Now I do the same with "cythonize([extensions])" and it works, too, although I have no idea why and what it does.
Here I can say already, that @locals has very limited use because it does not allow to type class attributes. Because of that @cclass is almost useless. Only @locals can speed up functions that are not class methods. Plus why add the "import cython" dependency to the python script if one can obtain the same from a .pxd file?
But one stage you should highlight is calling cython -a to get an html annotated version, then looking for the yellow to see where a lot of python calls are being made -- that will tell you what still needs to be augmented for full Cython performance.
Yes, this one is very interesting and I am often surprised by the result! Definitely to include.
post here when you've got that going...