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
55 lines (40 loc) · 1.37 KB
/
ticker.py
File metadata and controls
55 lines (40 loc) · 1.37 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
from follow import follow
import csv
import report
import tableformat
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 filter_symbols(rows, names):
rows = (row for row in rows if row['name'] in names)
return rows
# for row in rows:
# if row['name'] in names:
# yield 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 ticker(pf_file, log_file, fmt):
portfolio = report.read_portfolio(pf_file)
lines = follow(log_file)
formatter = tableformat.create_formatter(fmt)
rows = parse_stock_data(lines)
rows = filter_symbols(rows, portfolio)
formatter.headings(['Name','Price ($)','Change ($)'])
for row in rows:
name = row['name']
price = row['price']
change = row['change']
rowdata = [ name, f'{price:0.2f}', f'{change:0.2f}' ]
formatter.row(rowdata)
if __name__ == '__main__':
ticker('.\\Data\\portfolio.csv', '.\\Data\\stocklog.csv', 'txt')