473,702 Members | 2,819 Online

# Type in Python, Pyrex, etc...

Hi,

I'm playing with python and try to add type declaration in python. No,
don't leave now !

I have done it in order to stay python's syntax compatible (whereas for
example pyrex introduce a new syntax) so of course it should not be that
nice but...

The advantage is that you can debug with the python interpreter and then
compile it trough pyrex.
Of course, some other program like StarKiller could also use such
annotation...

First I though to use declare(int,obj ) (wich in the interpreter just
check type) but it cause exception if the obj is not yet initialize so I
change the notation...

Look at the following example (from pyrex) and the automated translation
in pyrex

--
Yermat

# Prime.py
from declaration import *

def primes(kmax):
declare(ListOf( int),primes)
declare(int,kma x)
n = int
k = int
i = int
p = ArrayOf(int,100 0)
result = []
if kmax > 1000:
kmax = 1000
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] <> 0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
result.append(n )
n = n + 1
return result

if __name__=="__ma in__":
print primes(10)
# Prime.pyx
# automatically generated from the previous one
def primes(int kmax):
cdef int n
cdef int k
cdef int i
cdef int p[1000]
result = []
if kmax > 1000:
kmax = 1000

k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] != 0:
i = i + 1

if i == k:
p[k] = n
k = k + 1
result.append(n )

n = n + 1

return result

if __name__ == '__main__':
print primes(10)

Jul 18 '05 #1
2 1602
Yermat <lo**@yermat.ne t1.nerim.net> writes:
I'm playing with python and try to add type declaration in python.
Is the intent to eventually incorporate it into Pyrex? I suppose it's
not necessary. It could easily become part of the makefile, converting
..py files to .pyx.
The advantage is that you can debug with the python interpreter and then
compile it trough pyrex.
I do this a lot. How 'bout making it translate "+=" too?
# Prime.py
from declaration import * def primes(kmax):
declare(ListOf( int),primes)
declare(int,kma x)

# Prime.pyx
# automatically generated from the previous one
def primes(int kmax):

Shouldn't "primes" be cdef-ed? Will there be a way to add "public" to
declarations?

Thank you.

--kyler
Jul 18 '05 #2
Kyler Laird a écrit :
Yermat <lo**@yermat.ne t1.nerim.net> writes:

I'm playing with python and try to add type declaration in python.

Is the intent to eventually incorporate it into Pyrex? I suppose it's
not necessary. It could easily become part of the makefile, converting
.py files to .pyx.

The advantage is that you can debug with the python interpreter and then
compile it trough pyrex.

I do this a lot. How 'bout making it translate "+=" too?

# Prime.py

from declaration import *

def primes(kmax):
declare(ListOf( int),primes)
declare(int,kma x)

# Prime.pyx
# automatically generated from the previous one
def primes(int kmax):

Shouldn't "primes" be cdef-ed? Will there be a way to add "public" to
declarations?

By now, this is just a proof-of-concept so it just work for this simple
example but I think you can do nearly everything. Eventually expand "+="
stuff...

In my example, "primes" come from the pyrex website and wasn't "cdef"...

All of this is just an ast manipulation. What I would like is feed-back
from python notation to add type that could serve to pyrex (like this
example), psycho, Starkiller...

Then It could become part of them !

Yermat

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 0 2153 by: Irmen de Jong | last post by: QOTW: "Confronting the Martellibot is like flirting with an encyclopedia, I'd rather not do it myself, but I respect those who do, because it produces knowledge." -- Anton Vredegoor "Python and Java are not diametrically opposed religions that must fight to the death in jihad, despite the existence of loonies favoring exactly that. They solve different problems; sometimes in similar ways, sometimes not." ... 5 2254 by: Gary | last post by: Hi- I've been searching the web for a while and I've been unable to find a way to access c data objects in python without using SWIG. I can do methods just fine but I can't access variables. It seems like PyModule_AddObject should work but it segfaults my program. There's a good chance I'm just using it wrong, or I should be using something else. Could someone give me a simple, complete example pretty please? Thanks! 58 4002 by: Svein Ove Aas | last post by: Is anyone working on a python-to-native compiler? I'd be interested in taking a look. Come to think of it, is anyone working on a sexpr-enabled version of Python, or anything similar? I really miss my macros whenever I try to use it... 6 1986 by: nell | last post by: Hi all, I've developed a testing application in python, and should supply it in a way that no one (lets say they are regular users) will understand it and edit it. The application source is all python but we expose a UI written in C# that go over all our code and expose to user functions (Indicated with a special prefix). So the problem on one hand is protecting the source and make it less accessible ond on the other hand to make it... 6 9373 by: jppx1 | last post by: Can I use python to make a regular Windows DLL that will be called from other programs? I know I can use the win32 extensions to make a COM server, but I need a straight DLL. Regards, Phillip ------------------------------------------------------------------------ Phillip Piper 7 1864 by: Georg Grabler | last post by: Hello everybody. There's a C library which i'd like to have python bindings for. I havn't known anything before about how to write python bindings for a C library. I succeeded now by using distutils to write the first bindings for functions and similar. Now, it seems as something is blocking my brain. For the library, i need "custom" types, so types defined in this library (structures), 4 2532 by: vedrandekovic | last post by: Hi, I have already install Microsoft visual studio .NET 2003 and MinGw, when I try to build a extension: python my_extension_setup.py build ( or install ) , I get an error: LINK : fatal error LNK1141: failure during build of exports file error: command '"C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\link.exe"' failed with exit status 1141.What shoud I 68 2704 by: Santiago Romero | last post by: ( Surely if this question has been asked for a zillion of times... ) ( and sorry for my english! ) I'm impressed with python. I'm very happy with the language and I find Python+Pygame a very powerful and productive way of writing 2D games. I'm not, at this moment, worried about execution speed of the small game I'm working on (it runs at full 60 fps even in an old AMD- K6 450 Laptop computer), but I continue asking me the same question:... 1 168 by: Hendrik van Rooyen | last post by: Patrick Maupin