Cython has moved to github.

cython-devel

view tests/run/setcomp.pyx @ 1478:b638811d14d0

implement set/dict comprehensions and set literals
author Stefan Behnel <scoder@users.berlios.de>
date Fri Dec 12 09:21:10 2008 +0100 (3 years ago)
parents
children 421a3edf1abf
line source
1 u"""
2 >>> type(smoketest()) is not list
3 True
4 >>> type(smoketest()) is _set
5 True
7 >>> sorted(smoketest())
8 [0, 4, 8]
9 >>> list(typed())
10 [A, A, A]
11 >>> sorted(iterdict())
12 [1, 2, 3]
13 """
15 # Py2.3 doesn't have the set type, but Cython does :)
16 _set = set
18 def smoketest():
19 return {x*2 for x in range(5) if x % 2 == 0}
21 cdef class A:
22 def __repr__(self): return u"A"
23 def __richcmp__(one, other, op): return one is other
24 def __hash__(self): return id(self) % 65536
26 def typed():
27 cdef A obj
28 return {obj for obj in {A(), A(), A()}}
30 def iterdict():
31 cdef dict d = dict(a=1,b=2,c=3)
32 return {d[key] for key in d}
34 def sorted(it):
35 l = list(it)
36 l.sort()
37 return l