Skip to content

Commit b17a510

Browse files
committed
end of 6.4
1 parent 352cae6 commit b17a510

7 files changed

Lines changed: 95 additions & 2223 deletions

File tree

Work/Data/stocklog.csv

Lines changed: 0 additions & 2203 deletions
Large diffs are not rendered by default.

Work/exercise6.02.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,22 @@ def printtaa():
2424

2525
# printtaa()
2626

27-
def filematch(filename, substr):
28-
with open(filename, 'r') as f:
29-
for line in f:
30-
if substr in line:
31-
yield line
27+
def filematch(lines, substr):
28+
for line in lines:
29+
if substr in line:
30+
yield line
3231

33-
# for line in open('.\\Data\\portfolio.csv'):
34-
# print(line, end='')
3532

36-
f = filematch('.\\Data\\portfolio.csv', 'IBM')
37-
# print(f)
38-
# print(f.__next__(), end='')
39-
# print(f.__next__(), end='')
33+
from follow import follow
4034

41-
for line in f:
42-
print(line, end='')
35+
lines = follow('.\\Data\\stocklog.csv')
36+
# ibm = filematch(lines, 'IBM')
37+
# for line in ibm:
38+
# print(line)
4339

40+
import csv
41+
42+
rows = csv.reader(lines)
43+
for row in rows:
44+
print(row)
4445

45-
# for line in filematch('.\\Data\\portfolio.csv', 'IBM'):
46-
# print(line, end='')

Work/follow.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
1+
# follow.py
22
import os
33
import time
4-
import report
4+
55

66
def follow(filename):
7+
'''
8+
Generator function to produce data for consumers
79
10+
:param filename: file to read latest data added to the bottom
11+
'''
812
f = open(filename)
913
f.seek(0, os.SEEK_END) # Move file pointer 0 bytes from end of file
1014

@@ -19,7 +23,7 @@ def follow(filename):
1923

2024

2125
if __name__ == '__main__':
22-
26+
import report
2327
portfolio = report.read_portfolio('.\\Data\\portfolio.csv')
2428

2529
for line in follow('.\\Data\\stocklog.csv'):

Work/portfolio.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ def __getitem__(self, index): # usage: object[0], object[0:3]
1515
return self._holdings[index]
1616

1717
def __contains__(self, name): # usage: 'IBM' in object ? True
18-
return any([s.name == name for s in self._holdings])
18+
# return any([s.name == name for s in self._holdings])
19+
return any(s.name == name for s in self._holdings)
1920

2021

2122
@property
2223
def total_cost(self): # usage: object.total_cost
23-
return sum([s.shares * s.price for s in self._holdings])
24+
# return sum([s.shares * s.price for s in self._holdings])
25+
return sum(s.shares * s.price for s in self._holdings)
2426

2527

2628
def tabulate_shares(self):
@@ -29,6 +31,7 @@ def tabulate_shares(self):
2931
total_shares = Counter()
3032
for s in self._holdings:
3133
total_shares[s.name] += s.shares
34+
3235
return total_shares
3336

3437

Work/report.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def make_report(portfolio, prices):
5151
def print_report(reportdata, formatter):
5252
'''
5353
Print a nicely formatted table from a list of (name, shares, price, change) tuples.
54+
55+
:param reportdata: dictionary of data
56+
:param formatter: Description
5457
'''
5558

5659
formatter.headings(['Name','Shares','Price ($)','Change ($)'])

Work/ticker.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+

Work/using_portfolio.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
print('IBM' in portfolio)
1212
print('YIT' in portfolio)
1313

14+
print(portfolio.total_cost)
15+
print(portfolio.tabulate_shares())
16+
17+
holdings = portfolio.tabulate_shares()
18+
19+
print(holdings.most_common(2))
20+
21+
for h in holdings:
22+
print(h, holdings[h])
23+
24+
1425

1526

1627
"""

0 commit comments

Comments
 (0)