Type in Python, Pyrex, etc...

Y

Yermat

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...

What do you think about this ?

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,kmax)
n = int
k = int
i = int
p = ArrayOf(int,1000)
result = []
if kmax > 1000:
kmax = 1000
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p <> 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)


# 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 != 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)
 
K

Kyler Laird

Yermat said:
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,kmax)


# 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
 
Y

Yermat

Kyler Laird a écrit :
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.


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



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top