Menu

[r4657]: / trunk / py4science / examples / skel / erathostenes_skel.py  Maximize  Restore  History

Download this file

40 lines (28 with data), 1.3 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
"""Simple example implementations of the Sieve of Erathostenes."""
import sys
import math
import numpy as N
def sieve(nmax):
"""Return a list of prime numbers up to nmax, using Erathostenes' sieve."""
raise NotImplementedError
if __name__ == '__main__':
# A simple test suite.
import unittest
# Make the generic test NOT be a subclass of unittest.TestCase, so that it
# doesn't get picked up automatically. Each subclass will specify an
# actual sieve function to test.
class sieveTestBase:
def test2(self):
self.assert_(self.sieve_func(2)==[2])
def test100(self):
primes100 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
self.assert_(self.sieve_func(100)==primes100)
# These subclasses define the actual sieve function to test. Note that it
# must be set as a staticmethod, so that the 'self' instance is NOT passed
# to the called sieve as first argument.
class sieveTestCase(sieveTestBase,unittest.TestCase):
sieve_func = staticmethod(sieve)
# This must be called LAST, because no code after it will be seen.
unittest.main()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.