By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,545 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,545 IT Pros & Developers. It's quick & easy.

Type in Python, Pyrex, etc...

P: n/a
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[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)
# 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
Share this Question
Share on Google+
2 Replies


P: n/a
Yermat <lo**@yermat.net1.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,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
Jul 18 '05 #2

P: n/a
Kyler Laird a écrit :
Yermat <lo**@yermat.net1.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,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?


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 discussion thread is closed

Replies have been disabled for this discussion.