Skip to content

Commit 20095db

Browse files
Add files via upload
1 parent 7a3158f commit 20095db

File tree

1 file changed

+291
-0
lines changed

1 file changed

+291
-0
lines changed
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<center><h1>Debugging in Python</h1></center><br><br>"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"<h3>raise statement :</h3>"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": null,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"length = 0\n",
24+
"if(length == 0):\n",
25+
" raise Exception('This is an exception using raise statement. Length cannot be 0')"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"<br>"
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"metadata": {},
38+
"source": [
39+
"<h3>Getting Exception message as a string :</h3>\n",
40+
"Using <b>traceback</b> module..."
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 6,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"Traceback (most recent call last):\n",
53+
" File \"<ipython-input-6-7677494f2f30>\", line 3, in <module>\n",
54+
" raise Exception('This is an Exception using raise statement')\n",
55+
"Exception: This is an Exception using raise statement\n",
56+
"\n",
57+
"From the file :\n",
58+
"Traceback (most recent call last):\n",
59+
" File \"<ipython-input-6-7677494f2f30>\", line 3, in <module>\n",
60+
" raise Exception('This is an Exception using raise statement')\n",
61+
"Exception: This is an Exception using raise statement\n",
62+
"\n"
63+
]
64+
}
65+
],
66+
"source": [
67+
"import traceback\n",
68+
"try:\n",
69+
" raise Exception('This is an Exception using raise statement')\n",
70+
"except:\n",
71+
" print(traceback.format_exc())\n",
72+
" \n",
73+
" #Also writing the exception message to a file\n",
74+
" exceptionFile = open('exceptionFile.txt','a')\n",
75+
" exceptionFile.write(traceback.format_exc())\n",
76+
" exceptionFile.close()\n",
77+
" \n",
78+
" exceptionFile = open('exceptionFile.txt','r')\n",
79+
" exception = exceptionFile.read()\n",
80+
" print('From the file :')\n",
81+
" print(exception)"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"metadata": {},
87+
"source": [
88+
"<h3>assert statement :</h3>"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 3,
94+
"metadata": {},
95+
"outputs": [
96+
{
97+
"ename": "AssertionError",
98+
"evalue": "This is an exception using assert statement. Length cannot be 0",
99+
"output_type": "error",
100+
"traceback": [
101+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
102+
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
103+
"\u001b[0;32m<ipython-input-3-5ff61b4ac7e2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mlength\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mlength\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'This is an exception using assert statement. Length cannot be 0'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
104+
"\u001b[0;31mAssertionError\u001b[0m: This is an exception using assert statement. Length cannot be 0"
105+
]
106+
}
107+
],
108+
"source": [
109+
"length = 0\n",
110+
"assert length > 0,'This is an exception using assert statement. Length cannot be 0'"
111+
]
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"<br>"
118+
]
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"<h3>Logging :</h3>\n",
125+
"Using <b>logging</b> module..."
126+
]
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"metadata": {},
131+
"source": [
132+
"There are 5 levels of logs :\n",
133+
"<ul>\n",
134+
" <li>debug</li>\n",
135+
" <li>info</li>\n",
136+
" <li>warning</li>\n",
137+
" <li>error</li>\n",
138+
" <li>critical</li>\n",
139+
"</ul>\n",
140+
"They are in increasing priority."
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 8,
146+
"metadata": {},
147+
"outputs": [
148+
{
149+
"name": "stderr",
150+
"output_type": "stream",
151+
"text": [
152+
"2018-07-18 00:31:50,119 - DEBUG - Start of program...\n",
153+
"2018-07-18 00:31:50,124 - DEBUG - Start of factorial(5)...\n",
154+
"2018-07-18 00:31:50,125 - DEBUG - i = 5 and total = 1...\n",
155+
"2018-07-18 00:31:50,126 - DEBUG - i = 5 and total = 2...\n",
156+
"2018-07-18 00:31:50,129 - DEBUG - i = 5 and total = 6...\n",
157+
"2018-07-18 00:31:50,130 - DEBUG - i = 5 and total = 24...\n",
158+
"2018-07-18 00:31:50,130 - DEBUG - i = 5 and total = 120...\n",
159+
"2018-07-18 00:31:50,131 - DEBUG - Return value is 120...\n",
160+
"2018-07-18 00:31:50,132 - DEBUG - End of program...\n"
161+
]
162+
},
163+
{
164+
"name": "stdout",
165+
"output_type": "stream",
166+
"text": [
167+
"120\n"
168+
]
169+
}
170+
],
171+
"source": [
172+
"import logging\n",
173+
"#logging configuration line\n",
174+
"logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s - %(levelname)s - %(message)s')\n",
175+
"\n",
176+
"logging.debug('Start of program...')\n",
177+
"#logging.info('Start of program...')\n",
178+
"#logging.warning('Start of program...')\n",
179+
"#logging.error('Start of program...')\n",
180+
"#logging.critical('Start of program...')\n",
181+
"\n",
182+
"def factorial(number):\n",
183+
" logging.debug('Start of factorial(%s)...' %(number))\n",
184+
" total = 1\n",
185+
" for i in range(1, number + 1):\n",
186+
" total *= i\n",
187+
" logging.debug('i = %s and total = %s...' %(number, total))\n",
188+
" \n",
189+
" logging.debug('Return value is %s...' %(total))\n",
190+
" return total\n",
191+
" \n",
192+
"print(factorial(5))\n",
193+
"\n",
194+
"logging.debug('End of program...')\n",
195+
" "
196+
]
197+
},
198+
{
199+
"cell_type": "markdown",
200+
"metadata": {},
201+
"source": [
202+
"<b>To disable all the logging commands :</b> "
203+
]
204+
},
205+
{
206+
"cell_type": "code",
207+
"execution_count": 10,
208+
"metadata": {},
209+
"outputs": [],
210+
"source": [
211+
"#critical to disablea log of all priorities\n",
212+
"logging.disable(logging.CRITICAL)"
213+
]
214+
},
215+
{
216+
"cell_type": "markdown",
217+
"metadata": {},
218+
"source": [
219+
"<b>To save all the logs to a file :</b>"
220+
]
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": 12,
225+
"metadata": {},
226+
"outputs": [
227+
{
228+
"name": "stdout",
229+
"output_type": "stream",
230+
"text": [
231+
"120\n"
232+
]
233+
}
234+
],
235+
"source": [
236+
"import logging\n",
237+
"#logging configuration line\n",
238+
"#The filename argument sends all the logs to the file specified\n",
239+
"logging.basicConfig(filename = 'logs.txt', level = logging.DEBUG, format = '%(asctime)s - %(levelname)s - %(message)s')\n",
240+
"\n",
241+
"logging.debug('Start of program...')\n",
242+
"#logging.info('Start of program...')\n",
243+
"#logging.warning('Start of program...')\n",
244+
"#logging.error('Start of program...')\n",
245+
"#logging.critical('Start of program...')\n",
246+
"\n",
247+
"def factorial(number):\n",
248+
" logging.debug('Start of factorial(%s)...' %(number))\n",
249+
" total = 1\n",
250+
" for i in range(1, number + 1):\n",
251+
" total *= i\n",
252+
" logging.debug('i = %s and total = %s...' %(number, total))\n",
253+
" \n",
254+
" logging.debug('Return value is %s...' %(total))\n",
255+
" return total\n",
256+
" \n",
257+
"print(factorial(5))\n",
258+
"\n",
259+
"logging.debug('End of program...')"
260+
]
261+
},
262+
{
263+
"cell_type": "code",
264+
"execution_count": null,
265+
"metadata": {},
266+
"outputs": [],
267+
"source": []
268+
}
269+
],
270+
"metadata": {
271+
"kernelspec": {
272+
"display_name": "Python 3",
273+
"language": "python",
274+
"name": "python3"
275+
},
276+
"language_info": {
277+
"codemirror_mode": {
278+
"name": "ipython",
279+
"version": 3
280+
},
281+
"file_extension": ".py",
282+
"mimetype": "text/x-python",
283+
"name": "python",
284+
"nbconvert_exporter": "python",
285+
"pygments_lexer": "ipython3",
286+
"version": "3.6.5"
287+
}
288+
},
289+
"nbformat": 4,
290+
"nbformat_minor": 2
291+
}

0 commit comments

Comments
 (0)