Sure.
> 2) Would I have to rename the file to use the .pyx extension if the above is
> possible?
Yes, I believe that if you use the .py extension Cython expects a
pure-mode Python file (possibly annotated with Cython types etc).
That's probably not what you want.
> 3) If 1) is not possible, would file xyz.py still work properly with the
> other .py files in the software system if I do a full conversion to Cython?
> In other words, would anything change from the other files' perspective?
You can keep xyz.py and have it import performance-critical functions
from _xyz.pyx.
> Any feedback would be highly appreciated. Thanks a lot!
Just give the Cython tutorials a go and it will be a lot clearer to you :)
> Waqas
If you didn't want to put the whole thing in a cython module, you could
refactor your code so only the bits that needed to be made faster were
in that module.
Cheers,
Henry
Well, "probably" is too strong a word here. It may or may not be what the
OP wants. Pure mode works pretty well for many things, although it has its
limits. The OP should start with that, and only switch to .pyx files in
Cython syntax where it turns out to be truly necessary.
Pure mode keeps having the major advantage of being pure Python, and thus
working in anything that can run, analyse or debug Python code.
Stefan
http://docs.cython.org/src/tutorial/pure.html
> @Mark, when you say "annotated with Cython types", do you mean a pure
> Python file with Cython static typing as described on this page here:
> http://docs.cython.org/src/quickstart/cythonize.html?
He meant the same thing that I referenced above.
> Another point I would like to add is that my simulation system (including
> this xyz.py file) makes heavy use of numpy.
Ah, in that case, you'd rather want to move the respective code into a .pyx
file, because that would allow you to interface with NumPy arrays directly
at the C level. Pure mode doesn't currently give you that.
Here are some examples:
http://docs.cython.org/src/tutorial/numpy.html
> I read in an old thread on this
> group that I could run into some problems during my Cython conversion
> because of this. What kind of problems can I run into?
You should give us a link to the thread, but off the top of my head, I
wouldn't know of any problems. Cython is very well integrated with NumPy,
simply because there is such a large user base that combine both.
Stefan
> I read in an old thread on this
> group that I could run into some problems during my Cython conversion
> because of this. What kind of problems can I run into?
You should give us a link to the thread, but off the top of my head, I
wouldn't know of any problems. Cython is very well integrated with NumPy,
simply because there is such a large user base that combine both.
Stefan
That is true, though fortunately "cythonize" is not an all or none
thing. Depending on your problem and the size of your data, the Python
call overhead in numpy.linalg may be negligible compared to the amount
of work done in numpy.linalg. (Indeed, in the extreem case where this
call dominates your runtime, there's little advantage to Cythonizing
at all). On the other hand Cython particularly shines if you're doing
any element-wise manipulations of primitive types. Most code falls
somewhere in the middle.
- Robert
The other big advantage of course is that you *can* call C libs that
might be faster.
Henry
> That is true, though fortunately "cythonize" is not an all or none
> thing. Depending on your problem and the size of your data, the Python
> call overhead in numpy.linalg may be negligible compared to the amount
> of work done in numpy.linalg. (Indeed, in the extreem case where this
> call dominates your runtime, there's little advantage to Cythonizing
> at all). On the other hand Cython particularly shines if you're doing
> any element-wise manipulations of primitive types. Most code falls
> somewhere in the middle.
One thing to remember here is that NumPy code tends to be memory bound,
whereas Cython, C, or Fortran code tend to be CPU bound. Also, NumPy and
SciPy do not always release the GIL when they should, e.g. they do not
know if the LAPACK library is reentrant.
Sturla
Just use them as you would in Python. But remember that keys and values
are always of type object. If you need something similar for C or C++
types, consider the "unordered associative containers" in the STL for
the most recent C++ standard (C++11). They correspond closely to
Python's set and dict.
http://en.wikipedia.org/wiki/Unordered_associative_containers_%28C%2B%2B%29
(These are a reason I actually consider to use C++ again.)
Sturla
Hello once again,How do I import my performance-critical functions from _xyz.pyx to xyz.py? I'm currently doing this in xyz.py:from _xyz import _performance_critical_function_1But when I try to run my top-level module using Python, it complains saying "No module named _xyz". What am I doing wrong here?Thanks,Waqas
I'm not sure - for me, the pyximport module has always been installed
simultaneously with cython, with no problems. I would guess that means
the cython installation did not work, or perhaps it is not in your
python path... Does "import cython" also fail?
Aronne
that's not an import. Try:
import cython
once at the python prompt
> This does seem to indicate that either cython is not in my python path or
> the cython installation didn't work. I modified both the 'Path' environment
> variable and the "PYTHONPATH" environment variable with C:\Cython-0.15.1 but
> I still get the same messages as above.
>
> Where am I going wrong?
PATH shoujld be pointed to somethign like:
C:\Python27\Scripts
That's where the main Cython script lies (and other scripts installed
with packages).
And depending on how Windows is set up and Cython was installed, you may need:
cython.py at the DOS prompt.
You could also try:
C:\python27\Scripts\cython.py
(I may have that a bit wrong -- no Windows box handy right now)
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
> In both cases, I get an error message about vcvarsall.bat:
vcvarsall.bat comes with Microsoft Visual Studio (Visual C, whatever
the heck else they call it). It is used to ser of the compiler
environment when invoked.
Cython, of course, requires a compiler. distutils (and I presume
pyximport) is set up (by default) to build extensions with the same
complier and settings that Python was build with (that's one of its
core nifty features).
The Python 2.6 and 2.7 binaries distributed by python.org are built
with VS2008 (I'm pretty sure). The easiest way to get set up for that
is to install the Microsoft Visual Studio 2008 express addition (It's
free on the MS web site -- it's a bit hard to find - MS tries to get
you to use the 2010 version, but that may not work with the python
builds). I've found installing VS 2008 express then "just works" with
distutils (and thus Cython).
If you want 64 bit -- it theoretically can be done, but its takes some
hacking, and I haven't gotten it to work yet (I've given up twice
after messing around or an hour or two). But maybe I just messed
something up -- there is a page in the Cython wiki about how to do it.