Initial commit

This commit is contained in:
David Beazley
2023-07-16 20:21:00 -05:00
parent 82e815fab2
commit 7d4b30154a
259 changed files with 600233 additions and 2 deletions

41
Solutions/8_6/cofollow.py Normal file
View File

@@ -0,0 +1,41 @@
# cofollow.py
import os
import time
import csv
def follow(filename,target):
with open(filename,"r") as f:
f.seek(0,os.SEEK_END)
while True:
line = f.readline()
if line != '':
target.send(line)
else:
time.sleep(0.1)
def receive(expected_type):
msg = yield
assert isinstance(msg, expected_type), 'Expected type %s' % (expected_type)
return msg
# Decorator for coroutines
from functools import wraps
def consumer(func):
@wraps(func)
def start(*args,**kwargs):
f = func(*args,**kwargs)
f.send(None)
return f
return start
# Sample coroutine
@consumer
def printer():
while True:
item = yield from receive(object)
print(item)
# Example use.
if __name__ == '__main__':
follow('../../Data/stocklog.csv', printer())