66 return gcd(b, a%b) |
66 return gcd(b, a%b) |
67 |
67 |
68 if __name__ == '__main__': |
68 if __name__ == '__main__': |
69 result = gcd(48, 64) |
69 result = gcd(48, 64) |
70 if result != 16: |
70 if result != 16: |
71 print "Test failed for the case a=48 and b=64. Expected 16. Obtained %d instead." % result |
71 print "Test failed" |
72 print "Test Passed" |
72 print "Test Passed" |
73 |
73 |
74 Note that we have introduced a new semantic which uses two new magic names |
74 Note that we have introduced a new semantic which uses two new magic names |
75 in Python *__name__* and *__main__*. This is a very common idiom used in |
75 in Python *__name__* and *__main__*. This is a very common idiom used in |
76 Python. Every Python code in a file can be run in two ways: Either as an |
76 Python. Every Python code in a file can be run in two ways: Either as an |
171 Also one should use. :: |
171 Also one should use. :: |
172 |
172 |
173 1.Four Space Indentation |
173 1.Four Space Indentation |
174 2.Limit to 79 characters a line, but readability should come first. |
174 2.Limit to 79 characters a line, but readability should come first. |
175 3.Functions and methods should be separated with two blank lines. |
175 3.Functions and methods should be separated with two blank lines. |
176 Class definitions with three blank lines. |
|
177 4.No inline comments, comments should be above the line they comment. |
176 4.No inline comments, comments should be above the line they comment. |
178 5.Use Docstring to explain units of code performing specific task like |
177 5.Use Docstring to explain units of code performing specific task like |
179 functions. |
178 functions. |
180 6.We should always have whitespace around operators and after punctuation. |
179 6.We should always have whitespace around operators and after punctuation. |
181 |
180 |
182 %% %% Pause and do the following exercise |
181 %% %% Pause and do the following exercise |
183 %% %% Give meaningful names to the variables in the gcd code . |
182 %% %% Give meaningful names to the variables in following |
184 |
183 code |
|
184 c=a/b |
185 |
185 |
186 |
186 |
187 This will help enormously towards making our program more readable. |
187 This will help enormously towards making our program more readable. |
188 |
188 |
189 From coding style lets move on to handling errors and exceptions. |
189 From coding style lets move on to handling errors and exceptions. |
256 |
256 |
257 In debugging process we form a hypothesis of what causes the error. |
257 In debugging process we form a hypothesis of what causes the error. |
258 Test if it is correct by changing the code. And refine the hypothesis |
258 Test if it is correct by changing the code. And refine the hypothesis |
259 on the basis of our result. |
259 on the basis of our result. |
260 |
260 |
261 {{{ Slide with code snippet }}} |
261 |
262 |
262 |
263 Lets see another example of debugging. Create a file mymodule.py and |
263 Lets see another example of debugging. Create a file mymodule.py and |
264 add the following code:: |
264 add the following code:: |
265 |
265 |
266 def test(): |
266 def test(): |
267 total=1+1 |
267 total=1+1 |
268 print spam |
268 print spam |
269 |
269 |
270 {{{ Slide with code snippet }}} |
270 |
271 |
271 |
272 Lets now try and run this code :: |
272 Lets now try and run this code :: |
273 |
273 |
274 import mymodule |
274 import mymodule |
275 mymodule.test() |
275 mymodule.test() |
|
276 |
|
277 |
|
278 {{{ Slide with idb and total being accessed }}} |
276 |
279 |
277 Interpreter gives us an error because spam is not defined |
280 Interpreter gives us an error because spam is not defined |
278 but lets now do %debug on ipython interpreter. The prompt on the shell has changed to ipdb. This is debugger here you can access variables in that code block for example 'total'unlike the normal interpreter. |
281 but lets now do %debug on ipython interpreter. The prompt on the shell has changed to ipdb. This is debugger here you can access variables in that code block for example 'total'unlike the normal interpreter. |
279 |
282 |
280 %% %% Pause and do the following exercise |
283 %% %% Pause and do the following exercise |