Cython has moved to github.

cython-devel

view tests/run/dictcomp.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 dict
3 True
5 >>> sorted(smoketest().items())
6 [(2, 0), (4, 4), (6, 8)]
7 >>> list(typed().items())
8 [(A, 1), (A, 1), (A, 1)]
9 >>> sorted(iterdict().items())
10 [(1, 'a'), (2, 'b'), (3, 'c')]
11 """
13 def smoketest():
14 return {x+2:x*2 for x in range(5) if x % 2 == 0}
16 cdef class A:
17 def __repr__(self): return u"A"
18 def __richcmp__(one, other, op): return one is other
19 def __hash__(self): return id(self) % 65536
21 def typed():
22 cdef A obj
23 return {obj:1 for obj in [A(), A(), A()]}
25 def iterdict():
26 cdef dict d = dict(a=1,b=2,c=3)
27 return {d[key]:key for key in d}
29 def sorted(it):
30 l = list(it)
31 l.sort()
32 return l