[Pyrex] Just some thoughts
Tomasz Primke
tprimke at interia.pl
Tue Apr 18 20:41:58 CEST 2006
For last few days I've been using Pyrex very extensively. Therefore I have
gained some experience with this wonderful tool. I have also become more
familiar with its limitations.
I'd like to share some thoughs about Pyrex - some sugestions, that - from my
POV - could make Pyrex even better.
1) The syntax of integer-loops: I find it too long. In my opinion, instead
of writing:
for i from lower_bound <= i < upper_bound:
...
it would be better (and easier, faster, more convenient) to write just:
for i from lower_bound <= upper_bound:
...
Rationale:
a) Why on Earth the name of the loop variable should be written twice? I
find it something, that wastes programmer's time.
b) (I think) My proposal is a simpler one, than the current Pyrex syntax.
A
simpler syntax is easier to learn and to memorize.
c) (I think) My proposal is a shorter one, that the current Pyrex syntax.
It
is easier and faster to write less code, that do the same things.
2) I think that Pyrex should offer some low-level memory managment routines.
I think of sizeof operator, as well as the C standard *alloc and free
functions. It would be also nice to have something like the g_new macro
known from the GLib library.
Rationale:
One on the reasons for writing Python extension modules in C is the need
of more speed. Pyrex also have speed in its scope. In Pyrex it is
possible to gain some speed by operating on data of declared type - and
not on Python (general) objects.
But another way to gain some speed is to code some low-level memory
management. There are, for example, some "array" routines available in
SWIG, that make developer's life much easier. In Pyrex developer has to
code them for himself (herself). Therefore I think that such a low-level
routines (perhaps even some array routines?) would be a great
improvement.
Anyway, in my opinion, writing C functions in Pyrex is still *much*
easier, that writing C functions in... pure C.
3) It would be great to have some easy way to auto-create Python wrappers
for chosen C functions and/or methods.
Rationale:
Pyrex allows to gain some speed by writing faster C functions/methods and
calling them from Pyrex's Python ones. Pyrex can be also used to write
wrappers for some C libraries (personally I find it much easier to write
such a wrappers in Pyrex than in SWIG).
Any C functions in a Pyrex module can be called from Pyrex level, but they
are inaccessible from Python level. In order to make a C function
accessible from Python, a Python-wrapper must be written. Therefore, very
often I find myself writing a lot of simple wrappers:
cdef void some_c_func( ... ):
...
def python_func( ... ):
some_c_func( ... )
I think it would be easier to achieve the same functionality with some
simpler syntax, like:
cpdef void some_c_func( ... ):
...
In this case I assumed, that there is a new "cpdef" keyword, that creates
a C function called "some_c_func", and its Python-wrapper (that might be
called "python_some_c_func", for example). Since the use of cpdef keyword
would be optional, developer could decide to use it or not.
Please, don't get me wrong. I'm not complaining on Pyrex - I find it a great
tool. I think, that all Pyrex developers (and contributors) are doing a
great job. I just would like to know, if Pyrex for me the same thing is,
that it is for other people - that's all.
For me Python is a language, that I use, because I prefere to write less
code and get the same, needed effect. I use Pyrex, because sometimes Python
code is too slow. Pyrex code runs faster, and if it's not enough, I use
pure C (and wrap it with Pyrex code). Therefore I'm looking for something,
that would allow me to write low-level code as easy, as possible. By "easy"
I mean to code as little, as possible to do the same things.
Best regards,
Tomek
More information about the Pyrex
mailing list