Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9c8047

Browse files
committed
Complete Chapter 8 exercises
1 parent 4b50a57 commit e9c8047

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

Work/fileparse.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#
33
# Exercise 3.3
44
import csv
5+
import logging
6+
7+
log = logging.getLogger(__name__)
58

69

710
def parse_csv(
@@ -10,7 +13,7 @@ def parse_csv(
1013
types=None,
1114
has_headers=True,
1215
delimiter=",",
13-
silence_errors=True,
16+
silence_errors=False,
1417
):
1518
"""Parse a CSV file into a list of records"""
1619
if select and not has_headers:
@@ -42,8 +45,8 @@ def parse_csv(
4245
row = [func(val) for func, val in zip(types, row)]
4346
except ValueError as e:
4447
if not silence_errors:
45-
print(f"Row {rownum}: Couldn't convert {row}")
46-
print(f"Row {rownum}: Reason {e}")
48+
log.warning("Row %d: Couldn't convert %s", rownum, row)
49+
log.debug("Row %d: Reason %s", rownum, e)
4750
continue
4851

4952
# Make a dictionary if there are headers, otherwise make a tuple.

Work/report.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ def main(args):
6262

6363

6464
if __name__ == "__main__":
65+
import logging
6566
import sys
6667

68+
logging.basicConfig(
69+
filename="app.log", # Name of the log file (omit to use stderr)
70+
filemode="w", # File mode (use 'a' to append)
71+
level=logging.WARNING, # Logging level (DEBUG, INFO, WARNING, ERROR, or CRITICAL)
72+
)
73+
6774
main(sys.argv)

Work/test_stock.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# test_stock.py
2+
3+
import unittest
4+
5+
import stock
6+
7+
8+
class TestStock(unittest.TestCase):
9+
def test_create(self):
10+
s = stock.Stock("GOOG", 100, 490.1)
11+
self.assertEqual(s.name, "GOOG")
12+
self.assertEqual(s.shares, 100)
13+
self.assertEqual(s.price, 490.1)
14+
15+
def test_cost(self):
16+
s = stock.Stock("GOOG", 100, 490.1)
17+
self.assertEqual(s.cost, 49010.0)
18+
19+
def test_sell(self):
20+
s = stock.Stock("GOOG", 100, 490.1)
21+
s.sell(50)
22+
self.assertEqual(s.shares, 50)
23+
24+
def test_shares_is_integer(self):
25+
s = stock.Stock("GOOG", 100, 490.1)
26+
with self.assertRaises(TypeError):
27+
s.shares = "200"
28+
29+
30+
if __name__ == "__main__":
31+
unittest.main()

0 commit comments

Comments
 (0)