forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfollow.py
More file actions
36 lines (26 loc) · 862 Bytes
/
follow.py
File metadata and controls
36 lines (26 loc) · 862 Bytes
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
# follow.py
import os
import time
def follow(filename):
'''
Generator function to produce data for consumers
:param filename: file to read latest data added to the bottom
'''
f = open(filename)
f.seek(0, os.SEEK_END) # Move file pointer 0 bytes from end of file
while True:
line = f.readline()
if line == '':
time.sleep(0.1) # Sleep briefly and retry
continue
yield line
if __name__ == '__main__':
import report
portfolio = report.read_portfolio('.\\Data\\portfolio.csv')
for line in follow('.\\Data\\stocklog.csv'):
fields = line.split(',')
name = fields[0].strip('"')
price = float(fields[1])
change = float(fields[4])
if name in portfolio:
print(f'{name:>10s} {price:>10.2f} {change:>10.2f}')