Skip to content

Commit 860579d

Browse files
committedApr 23, 2020
Python: Exceptions, Assert, Logging (AtBSwP)
1 parent c1164a1 commit 860579d

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
 

‎.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@ epi_judge_java/java_build/
2727
/bluemyria_notes/ra/.vscode
2828
/bluemyria_notes/ra/venv
2929

30+
# /bluemyria_notes/comp_progr/
31+
3032
/bluemyria_notes/comp_progr/*/*.cpp
3133
/bluemyria_notes/comp_progr/*/*.java
3234
/bluemyria_notes/comp_progr/*/*.py2
35+
/bluemyria_notes/comp_progr/*/*.py3
36+
/bluemyria_notes/comp_progr/*/*.py
3337

3438
/bluemyria_notes/ra/*.png
3539

40+
41+
42+
bluemyria_notes/PythonAutom/error_log.txt
43+
bluemyria_notes/PythonAutom/examplePhoneEmailDirectory.pdf
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
import traceback
3+
4+
#################################################
5+
# EXCEPTIONS & TRACEBACK
6+
#################################################
7+
def boxPrint(symbol, width, height):
8+
9+
try:
10+
if len(symbol) != 1:
11+
raise Exception('"symbol" must be a string of length 1')
12+
if width < 3 or height < 3:
13+
raise Exception('"width" and "height" must be greater than 2')
14+
except:
15+
errorFile = open('error_log.txt', 'a')
16+
errorFile.write(traceback.format_exc())
17+
errorFile.close()
18+
print('the traceback was written in error_log.txt')
19+
print(symbol * width)
20+
21+
for _ in range(height - 2):
22+
print(symbol + (' ' * (width - 2)) + symbol )
23+
24+
print(symbol * width)
25+
26+
boxPrint('&', 15, 8)
27+
boxPrint('O', 5, 20)
28+
boxPrint('**', 5, 20)
29+
boxPrint('*', 2, 20)
30+
boxPrint('*', 3, 2)
31+
32+
#################################################
33+
# ASSERTIONS
34+
#################################################
35+
36+
market_2nd = {'ns': 'green', 'ew':'red'}
37+
38+
def switchLights(intersection):
39+
for key in intersection.keys():
40+
if intersection[key] == 'green':
41+
intersection[key] = 'yellow'
42+
elif intersection[key] == 'yellow':
43+
intersection[key] = 'red'
44+
elif intersection[key] == 'red':
45+
intersection[key] = 'green'
46+
assert 'red' in intersection.values(), 'Neither light is red!' + str(intersection)
47+
48+
print(market_2nd)
49+
switchLights(market_2nd)
50+
print(market_2nd)
51+
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import logging
2+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
3+
# LOG in FILE
4+
# logging.basicConfig(filename='myLog.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
5+
6+
# DEBUG, INFO, WARNING, ERROR, CRITICAL
7+
#logging.disable(logging.CRITICAL)
8+
9+
logging.debug('Start of program')
10+
def factorial(n):
11+
logging.debug(f'Start of factorial {n}')
12+
13+
total = 1
14+
for i in range(1, n+1):
15+
16+
total *= i
17+
logging.debug(f'i is {i}, total is {total}')
18+
19+
logging.debug(f'Return value is {total}')
20+
return total
21+
22+
print(factorial(5))
23+
24+
logging.debug('End of program')

0 commit comments

Comments
 (0)
Please sign in to comment.