-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathfollow.py
More file actions
34 lines (28 loc) · 825 Bytes
/
Copy pathfollow.py
File metadata and controls
34 lines (28 loc) · 825 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
# follow.py
#
# Watch a log file (stocks in this case)
import os
import time
def follow(filename):
f = open(filename, 'r')
f.seek(0, os.SEEK_END)
while True:
line = f.readline()
if not line:
time.sleep(0.1)
continue # Retry
yield line # Emit a line
import csv
def parse_stock_data(lines):
rows = csv.reader(lines)
types = [str, float, str, str, float, float, float, float, int]
converted = ( [func(val) for func, val in zip(types, row)] for row in rows)
return converted
lines = follow('Data/stocklog.csv')
rows = parse_stock_data(lines)
negchange = (row for row in rows if row[4] < 0)
for row in negchange:
name = row[0]
price = row[1]
change = row[4]
print('{:>10s} {:>10.2f} {:>10.2f}'.format(name, price, change))