forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticker.py
More file actions
61 lines (46 loc) · 1.29 KB
/
Copy pathticker.py
File metadata and controls
61 lines (46 loc) · 1.29 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ticker.py
import sys
import os
excdir = os.getcwd()
excdir += r'\work'
sys.path
sys.path.append(excdir)
from follow import follow
import csv
from report import read_portfolio_6_2
def select_columns(rows, indices):
for row in rows:
yield[ row[index] for index in indices]
def convert_types(rows, types):
for row in rows:
yield [func(val) for func, val in zip(types, row)]
def make_dicts(rows, headers):
for row in rows:
yield dict(zip(headers, row))
def parse_stock_data(lines):
rows = csv.reader(lines)
rows = select_columns(rows, [0,1,4])
rows = convert_types(rows, [str, float, float])
rows = make_dicts(rows, ['name', 'price', 'change'])
return rows
def filter_symbols(rows, names):
for row in rows:
if row['name'] in names:
yield row
''' TODO: -- ticker method
'''
def ticker(portfile, logfile):
import report
portfolio = report.read_portfolio_2_6(portfile)
rows = parse_stock_data(follow(logfile))
rows = filter_symbols(rows, portfolio)
for row in rows:
print(row)
ticker('data/portfolio.csv','data/stocklog.csv')
if __name__ == '__main__':
lines = follow('work\data\stocklog.csv')
rows = parse_stock_data(lines)
for row in rows:
print(row)
## 6.4 more generators
i