testing-debugging/script.rst
changeset 487 cb3974daced5
parent 401 abf092be95ef
child 488 85f049b5ec08
equal deleted inserted replaced
486:591369704df0 487:cb3974daced5
    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.  
   215 
   215 
   216 
   216 
   217 
   217 
   218 Lets see why and how we can use Exception in our programs.
   218 Lets see why and how we can use Exception in our programs.
   219 
   219 
   220 {{{ Slide with code snippet }}}
   220 
   221 
   221 
   222 Type on your interpreter::
   222 Type on your interpreter::
   223      
   223 
   224      a = raw_input("Enter a number:")
   224      a = raw_input("Enter a number:")
   225      num = int(a) 
   225      num = int(a) 
   226 
   226 
   227 {{{ Run this code on interpreter with a character input }}}
   227 {{{ Run this code on interpreter with a character input }}}
   228 
   228 
   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