|
| 1 | +from follow import follow |
| 2 | +import csv |
| 3 | +import report |
| 4 | +import tableformat |
| 5 | + |
| 6 | +def select_columns(rows, indices): |
| 7 | + for row in rows: |
| 8 | + yield [row[index] for index in indices] |
| 9 | + |
| 10 | +def convert_types(rows, types): |
| 11 | + for row in rows: |
| 12 | + yield [func(val) for func, val in zip(types, row)] |
| 13 | + |
| 14 | +def make_dicts(rows, headers): |
| 15 | + for row in rows: |
| 16 | + yield dict(zip(headers, row)) |
| 17 | + |
| 18 | +def filter_symbols(rows, names): |
| 19 | + rows = (row for row in rows if row['name'] in names) |
| 20 | + return rows |
| 21 | + # for row in rows: |
| 22 | + # if row['name'] in names: |
| 23 | + # yield row |
| 24 | + |
| 25 | +def parse_stock_data(lines): |
| 26 | + rows = csv.reader(lines) |
| 27 | + rows = select_columns(rows, [0, 1, 4]) |
| 28 | + rows = convert_types(rows, [str, float, float]) |
| 29 | + rows = make_dicts(rows, ['name', 'price', 'change']) |
| 30 | + return rows |
| 31 | + |
| 32 | +def ticker(pf_file, log_file, fmt): |
| 33 | + portfolio = report.read_portfolio(pf_file) |
| 34 | + lines = follow(log_file) |
| 35 | + formatter = tableformat.create_formatter(fmt) |
| 36 | + |
| 37 | + rows = parse_stock_data(lines) |
| 38 | + rows = filter_symbols(rows, portfolio) |
| 39 | + formatter.headings(['Name','Price ($)','Change ($)']) |
| 40 | + |
| 41 | + for row in rows: |
| 42 | + name = row['name'] |
| 43 | + price = row['price'] |
| 44 | + change = row['change'] |
| 45 | + rowdata = [ name, f'{price:0.2f}', f'{change:0.2f}' ] |
| 46 | + formatter.row(rowdata) |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + |
| 52 | + ticker('.\\Data\\portfolio.csv', '.\\Data\\stocklog.csv', 'txt') |
| 53 | + |
| 54 | + |
| 55 | + |
0 commit comments