Skip to content

Commit a75ea55

Browse files
committed
make package
1 parent 3ca4c72 commit a75ea55

20 files changed

Lines changed: 190 additions & 129 deletions

Work/pcost.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,6 @@
1-
# pcost.py
2-
#
3-
# Exercise 1.33
4-
5-
from portfolio import Portfolio
6-
import report
7-
8-
9-
def portfolio_cost(fn):
10-
"""Computes the total cost (shares*price) of a portfolio file"""
11-
portfolio = report.read_portfolio(fn)
12-
return portfolio.total_cost
13-
14-
15-
def main(argv):
16-
if len(argv) == 2:
17-
filename = argv[1]
18-
else:
19-
filename = "Data/portfolio.csv"
20-
21-
cost = portfolio_cost(filename)
22-
23-
print("Total cost:", cost)
24-
25-
26-
if __name__ == "__main__":
27-
import sys
28-
29-
main(sys.argv)
1+
#!/usr/bin/env python3
2+
# pcost.py
3+
import porty.pcost
4+
import sys
5+
6+
porty.pcost.main(sys.argv)

Work/portfolio.csv

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name,shares,price
2+
"AA",100,32.20
3+
"IBM",50,91.10
4+
"CAT",150,83.44
5+
"MSFT",200,51.23
6+
"GE",95,40.37
7+
"MSFT",50,65.10
8+
"IBM",100,70.44
File renamed without changes.

Work/porty/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .pcost import portfolio_cost
2+
from .report import portfolio_report
File renamed without changes.
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# Exercise 3.3
44
# from typing import Iterable
55
import csv
6+
import logging
7+
8+
log = logging.getLogger(__name__)
69

710

811
def parse_csv(
@@ -39,8 +42,10 @@ def parse_csv(
3942
row = [func(val) for func, val in zip(types, row)]
4043
except ValueError as e:
4144
if not silence_errors:
42-
print(f"Row {idx}: Couldn't convert {row}")
43-
print(f"Row {idx}: Reason {e}")
45+
# print(f"Row {idx}: Couldn't convert {row}")
46+
# print(f"Row {idx}: Reason {e}")
47+
log.warning("Row %d: Couldn't convert %s", idx, row)
48+
log.debug("Row %d: Reason %s", idx, e)
4449
# Make a dictionary
4550
record = dict(zip(headers, row))
4651
records.append(record)
@@ -56,8 +61,10 @@ def parse_csv(
5661
row = tuple([func(val) for func, val in zip(types, row)]) # type: ignore
5762
except ValueError as e:
5863
if not silence_errors:
59-
print(f"Row {idx}: Couldn't convert {row}")
60-
print(f"Row {idx}: Reason {e}")
64+
# print(f"Row {idx}: Couldn't convert {row}")
65+
# print(f"Row {idx}: Reason {e}")
66+
log.warning("Row %d: Couldn't convert %s", idx, row)
67+
log.debug("Row %d: Reason %s", idx, e)
6168
records.append(row) # type: ignore
6269

6370
return records
File renamed without changes.
File renamed without changes.
File renamed without changes.

Work/porty/pcost.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# pcost.py
2+
#
3+
# Exercise 1.33
4+
5+
# from .portfolio import Portfolio
6+
from . import report
7+
8+
9+
def portfolio_cost(fn):
10+
"""Computes the total cost (shares*price) of a portfolio file"""
11+
portfolio = report.read_portfolio(fn)
12+
return portfolio.total_cost
13+
14+
15+
def main(argv):
16+
if len(argv) == 2:
17+
filename = argv[1]
18+
else:
19+
filename = "Data/portfolio.csv"
20+
21+
cost = portfolio_cost(filename)
22+
23+
print("Total cost:", cost)
24+
25+
26+
if __name__ == "__main__":
27+
import sys
28+
29+
main(sys.argv)

0 commit comments

Comments
 (0)