Skip to content

Commit 7a3158f

Browse files
Debugging
1 parent ee35b30 commit 7a3158f

7 files changed

Lines changed: 111 additions & 1 deletion

File tree

Chapter10-Debugging/boxPrint.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
"""
3+
4+
Author: Samrat Banerjee
5+
Dated: 02/07/2018
6+
Description: Raising Exceptions
7+
8+
"""
9+
10+
def boxPrint(symbol,width,height):
11+
if len(symbol)!=1:
12+
raise Exception('Symbol must be a single character string.')
13+
if width<=2:
14+
raise Exception('Width must be greater than 2.')
15+
if height<=2:
16+
raise Exception('Height must be greater than 2.')
17+
print(symbol*width)
18+
for i in range(height-2):
19+
print(symbol+(' '*(width-2))+symbol)
20+
print(symbol*width)
21+
22+
for sym,w,h in (('*',4,4),('O',20,5),('x',1,3),('ZZ',3,3)):
23+
try:
24+
boxPrint(sym,w,h)
25+
except Exception as err:
26+
print('An exception happened: '+str(err))
Lines changed: 7 additions & 0 deletions
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)

Chapter10-Debugging/coinFlip.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
"""
3+
4+
Author: Samrat Banerjee
5+
Dated: 17/07/2018
6+
Description: Breakpoints
7+
8+
"""
9+
import random
10+
heads = 0
11+
for i in range(1, 1001):
12+
if random.randint(0, 1) == 1:
13+
heads = heads + 1
14+
if i == 500:
15+
print('Halfway done!')
16+
print('Heads came up ' + str(heads) + ' times.')
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
3+
Author: Samrat Banerjee
4+
Dated: 14/07/2018
5+
Description: Getting Traceback as a String
6+
7+
"""
8+
9+
def spam():
10+
bacon()
11+
def bacon():
12+
raise Exception('This is an error message.')
13+
14+
spam()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
"""
3+
4+
Author: Samrat Banerjee
5+
Dated: 17/07/2018
6+
Description: Using the logging Module
7+
8+
"""
9+
10+
import logging
11+
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s')
12+
logging.debug('Start of program')
13+
14+
def factorial(n):
15+
logging.debug('Start of factorial(%s)' % (n))
16+
total = 1
17+
for i in range(1,n + 1):
18+
total *= i
19+
logging.debug('i is ' + str(i) + ', total is ' + str(total))
20+
logging.debug('End of factorial(%s)' % (n))
21+
return total
22+
23+
print(factorial(5))
24+
logging.debug('End of program')
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
"""
3+
4+
Author: Samrat Banerjee
5+
Dated: 16/07/2018
6+
Description: Using an Assertion in a Traffic Light Simulation
7+
8+
"""
9+
10+
market_2nd = {'ns': 'green', 'ew': 'red'} # market street and 2nd street, ns is north-south
11+
mission_16th = {'ns': 'red', 'ew': 'green'} #mission street and 16th street, ew is east-west
12+
13+
def switchLights(stoplight):
14+
for key in stoplight.keys():
15+
if stoplight[key]=='green':
16+
stoplight[key]='yellow'
17+
elif stoplight[key]=='yellow':
18+
stoplight[key]='red'
19+
elif stoplight[key]=='red':
20+
stoplight[key]='green'
21+
assert 'red' in stoplight.values(), 'Neither light is red! ' + str(stoplight)
22+
23+
switchLights(market_2nd)

Chapter9-OrganizingFiles/backupToZip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ def backupToZip(folder):
4141
backupZip.close()
4242
print("Done.")
4343

44-
backupToZip('/home/samrat/LearningAutomationwithPython')
44+
backupToZip('/home/samrat/Anything')

0 commit comments

Comments
 (0)