Skip to content

Commit f998b01

Browse files
committed
Chapter 11 finished
1 parent ed0e4a9 commit f998b01

11 files changed

+135
-0
lines changed

10-debug/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# raise
2+
<pre>raise Exception(msg)</pre>
3+
4+
# try...except...as...
5+
<pre>try:
6+
code block
7+
except Exception as err:
8+
error handling</pre>
9+
10+
# traceback.format_exc()
11+
* convert traceback stack info to be a string
12+
13+
# assert condition, message
14+
* Python -O option will disable assertions
15+
16+
# logging module
17+
<pre>
18+
import logging
19+
logging.disable(logging.CRITICAL)
20+
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
21+
logging.basicConfig(filename='myProgramLog.txt', level=logging.DEBUG,
22+
format='(asctime)s - %(levelname)s - %(message)s')
23+
logging.debug('Start of program')
24+
</pre>

10-debug/boxPrint.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def boxPrint(symbol, width, height):
2+
if len(symbol) != 1:
3+
raise Exception('Symbol must be a single character string.')
4+
if width <= 2:
5+
raise Exception('Width must be greater than 2.')
6+
if height <= 2:
7+
raise Exception('Height must be greater than 2.')
8+
9+
print(symbol * width)
10+
for i in range(height - 2):
11+
print(symbol + (' ' * (width - 2)) + symbol)
12+
print(symbol * width)
13+
14+
for sym, w, h in (('*', 4, 4), ('O', 20, 5), ('x', 1, 3), ('ZZ', 3, 3)):
15+
try:
16+
boxPrint(sym, w, h)
17+
except Exception as err:
18+
print('An exception happened: ' + str(err))

10-debug/buggyAddingProgram.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
print('Enter the first number to add:')
2+
first = input()
3+
print('Enter the second number to add:')
4+
second = input()
5+
print('Enter the third number to add:')
6+
third = input()
7+
print('The sum is ' + first + second + third)

10-debug/carSim.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
market_2nd = {'ns':'green', 'ew':'red'}
2+
mission_16th = {'ns':'yellow', 'ew':'green'}
3+
4+
def switchLights(stoplight):
5+
assert 'red' in stoplight.values(), 'Neither light is red! ' + str(stoplight)
6+
for key in stoplight.keys():
7+
if stoplight[key] == 'green':
8+
stoplight[key] = 'yellow'
9+
elif stoplight[key] == 'yellow':
10+
stoplight[key] = 'red'
11+
elif stoplight[key] == 'red':
12+
stoplight[key] = 'green'
13+
14+
switchLights(market_2nd)
15+
switchLights(mission_16th)

10-debug/coinFlip.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import random
2+
heads = 0
3+
for i in range(1, 1001):
4+
if random.randint(0, 1) == 1:
5+
heads = heads + 1
6+
if i == 500:
7+
print('Halfway done!')
8+
print('Heads came up ' + str(heads) + ' times.')

10-debug/errorExample.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def spam():
2+
bacon()
3+
4+
def bacon():
5+
raise Exception('This is the error message.')
6+
7+
spam()

10-debug/errorInfo.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Traceback (most recent call last):
2+
File ".\format_exc.py", line 3, in <module>
3+
raise Exception('This is the error message.')
4+
Exception: This is the error message.

10-debug/factorialLog.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import logging
2+
# logging.disable(logging.CRITICAL)
3+
#logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
4+
logging.basicConfig(filename='myProgramLog.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
5+
logging.debug('Start of program')
6+
7+
def factorial(n):
8+
logging.debug('Start of factorial(%s%%)' % (n))
9+
total = 1
10+
for i in range(1, n + 1):
11+
total *= i
12+
logging.debug('i is ' + str(i) + ', total is ' + str(total))
13+
return total
14+
logging.debug('End of factorial(%s%%)' % (n))
15+
16+
print(factorial(5))
17+
logging.debug('End of program')

10-debug/format_exc.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import traceback
2+
try:
3+
raise Exception('This is the error message.')
4+
except:
5+
errorFile = open('errorInfo.txt', 'w')
6+
errorFile.write(traceback.format_exc())
7+
errorFile.close()
8+
print('The traceback info was written to errorInfo.txt.')

10-debug/guessCoin.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import random
2+
3+
guess = ''
4+
ans = ('heads', 'tails')
5+
while guess not in ans:
6+
print('Guess the coin toss! Enter heads or tails:')
7+
guess = input()
8+
9+
toss = ans[random.randint(0, 1)]
10+
if toss == guess:
11+
print('You got it!')
12+
else:
13+
print('Nope! Guess again! Enter heads or tails:')
14+
guess = input()
15+
toss = ans[random.randint(0, 1)]
16+
if toss == guess:
17+
print('You got it!')
18+
else:
19+
print('Nope. You are really bad at this game.')

10-debug/myProgramLog.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2017-07-02 20:28:30,480 - DEBUG - Start of program
2+
2017-07-02 20:28:30,480 - DEBUG - Start of factorial(5%)
3+
2017-07-02 20:28:30,480 - DEBUG - i is 1, total is 1
4+
2017-07-02 20:28:30,480 - DEBUG - i is 2, total is 2
5+
2017-07-02 20:28:30,480 - DEBUG - i is 3, total is 6
6+
2017-07-02 20:28:30,480 - DEBUG - i is 4, total is 24
7+
2017-07-02 20:28:30,480 - DEBUG - i is 5, total is 120
8+
2017-07-02 20:28:30,480 - DEBUG - End of program

0 commit comments

Comments
 (0)