# python_201_pyrex_clsprimes.pyx """An implementation of primes handling class for a demonstration of Pyrex. """ cdef class Primes: """A class containing functions for handling primes. """ def showPrimes(self, int kmax): """Show a range of primes. Use the method primes() to generate the primes. """ plist = self.primes(kmax) for p in plist: print 'prime: %d' % p def primes(self, int kmax): """Generate the primes in the range 0 - kmax. """ cdef int n, k, 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