I have this segment in my Python course (which I wrote, see below) where I show prime number generator using an iterator written in two ways: as a generator function and as a class.
I have one other way of sharing the prime number trial by division thing: as coroutines. I owe David Beazley on this one. The coroutine decorator jumps us to the first yield so that we can send numbers in right away.
primes weeds out composites, passing survivor primes to the printing coroutine (passed in as target).
In my Tractor class, a simple object that plows a Field leaving a trail of Unicode characters (raster pattern), I used yield both to output and input (instances would gradually run low on fuel, but one could refill), but David advises against this pattern.
Trial by division, but this time the primes coroutine acts more as a filter, passing qualified candidates through to print_me, which writes to a file. """
def coroutine(func): """ Advances decorated generator function to the first yield """ def start(*args, **kwargs): cr = func(*args, **kwargs) cr.send(None) # or next(cr) or cr.__next__() return cr return start
@coroutine def print_me(file_name): with open(file_name, 'w') as file_obj: while True: to_print = (yield) file_obj.write(str(to_print)+"\n")
@coroutine def primes(target): _primes_so_far = [2] while True: candidate = (yield) for prev in _primes_so_far: if not divmod(candidate, prev)[1]: break if prev**2 > candidate: _primes_so_far.append(candidate) target.send(candidate) break
output = print_me("primes.txt") p = primes(output)
for x in range(3, 200, 2): # test odds 3-199 p.send(x)
with open("primes.txt", 'r') as file_obj: print(file_obj.read())