3.7 KiB
[ Index | Exercise 8.2 | Exercise 8.4 ]
Exercise 8.3
Objectives:
- Using coroutines to set up processing pipelines
Files Created: cofollow.py
,
coticker.py
Note
For this exercise the stocksim.py
program should still
be running in the background.
In link:ex8_2.html[Exercise 8.2] you wrote some code that used generators to set up a processing pipeline. A key aspect of that program was the idea of data flowing between generator functions. A very similar kind of dataflow can be set up using coroutines. The only difference is that with a coroutine, you send data into different processing elements as opposed to pulling data out with a for-loop.
(a) A coroutine example
Getting started with coroutines can be a little tricky. Here is an
example program that performs the same task as link:ex8_2.html[Exercise
8.2], but with coroutines. Take this program and copy it into a file
called cofollow.py
.
# cofollow.py
import os
import time
# Data source
def follow(filename,target):
with open(filename,'r') as f:
0,os.SEEK_END)
f.seek(while True:
= f.readline()
line if line != '':
target.send(line)else:
0.1)
time.sleep(
# Decorator for coroutine functions
from functools import wraps
def consumer(func):
@wraps(func)
def start(*args,**kwargs):
= func(*args,**kwargs)
f None)
f.send(return f
return start
# Sample coroutine
@consumer
def printer():
while True:
= yield # Receive an item sent to me
item print(item)
# Example use
if __name__ == '__main__':
'Data/stocklog.csv',printer()) follow(
Run this program and make sure produces output.. Make sure you understand how the different pieces are hooked together.
(b) Build some pipeline components
In a file coticker.py
, build a series of pipeline
components that carry out the same tasks as the ticker.py
program in link:ex8_2.html[Exercise 8.2]. Here is the implementation of
the various pieces.
# coticker.py
from structure import Structure
class Ticker(Structure):
= String()
name =Float()
price = String()
date = String()
time = Float()
change open = Float()
= Float()
high = Float()
low = Integer()
volume
from cofollow import consumer, follow
from tableformat import create_formatter
import csv
# This one is tricky. See solution for notes about it
@consumer
def to_csv(target):
def producer():
while True:
yield line
= csv.reader(producer())
reader while True:
= yield
line next(reader))
target.send(
@consumer
def create_ticker(target):
while True:
= yield
row
target.send(Ticker.from_row(row))
@consumer
def negchange(target):
while True:
= yield
record if record.change < 0:
target.send(record)
@consumer
def ticker(fmt, fields):
= create_formatter(fmt)
formatter
formatter.headings(fields)while True:
= yield
rec = [getattr(rec, name) for name in fields]
row formatter.row(row)
Your challenge: Write the main program that hooks all of these components together to generate the same stock ticker as in the previous exercise.
[ Solution | Index | Exercise 8.2 | Exercise 8.4 ]
>>>
Advanced Python Mastery
...
A course by dabeaz
...
Copyright 2007-2023
.
This work is licensed under a Creative Commons
Attribution-ShareAlike 4.0 International License