Chapter 13 Debugging Strategies Learning by debugging.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
Fundamentals of Python: From First Programs Through Data Structures
Python Programming Fundamentals
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
General Programming Introduction to Computing Science and Programming I.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Input, Output, and Processing
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Variables, Expressions and Statements
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
1 Week 8 Creating Simple Shell Scripts. 2 Chapter Objectives  In this chapter, you will :  Learn how to create Shell Scripts  Commenting / Making Portable.
LINGO TUTORIAL.
Development Environment
Introduction to Computing Science and Programming I
Topics Designing a Program Input, Processing, and Output
Python’s Modules Noah Black.
EECS 183 Discussion #9: It’s time to “Git” Python! November 22nd, 2016
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Introduction to Python
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Department of Computer Science,
Testing and Debugging.
Computer Programming I
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
If, else, elif.
Functions.
CSE 303 Concepts and Tools for Software Development
Exceptions and files Taken from notes by Dr. Neil Moore
Cracking the Coding Interview
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Designing and Debugging Batch and Interactive COBOL Programs
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Conditions and Ifs BIS1523 – Lecture 8.
Exceptions and files Taken from notes by Dr. Neil Moore
An Introduction to Structured Program Design in COBOL
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Multiple Selections (ELIF Statements)
The Linux Command Line Chapter 24
ERRORS AND EXCEPTIONS Errors:
Programming Errors Key Revision Points.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Python programming exercise
Topics Designing a Program Input, Processing, and Output
Week 1 – Lesson 2: Creating Shell Scripts, Linux Commands
Topics Designing a Program Input, Processing, and Output
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Introduction to Computer Science
Python Review
Topics Introduction to File Input and Output
Chapter 1 c++ structure C++ Input / Output
CHAPTER 6 Testing and Debugging.
The Linux Command Line Chapter 24
Presentation transcript:

Chapter 13 Debugging Strategies Learning by debugging

Chapter 13 Two types of errors : 1. program does not run at all → make it running 2. program runs but gives an incorrect output → check procedures Avoidance by good design Result validation

Chapter 13 General Strategies Build upon working elements 1. Think about the general approach to your problem 2. Start building towards incremental success 3. Get each element of your program working before moving on e.g. - print intermediate steps on the screen while writing the script - use a sandbox folder - use artificial or copied data

Chapter 13 General Strategies Think about your assumptions 1. Make sure you are editing the version of the program that you are actually using → editing wrong script Use which command to get the absolute path of the program 2. Save changes before re-execution 3. Check line endings Incorrect line endings in input files → program can combine data lines InFile = open(InFileName, 'rU') converts all line endings to newline (\n) characters

Chapter 13 General Strategies Think about your assumptions 1. Make sure you are editing the version of the program that you are actually using → editing wrong script Use which command to get the absolute path of the program 2. Save changes before re-execution 3. Check line endings 4. Check contents of your data file Incorrect input files can crash programs example : AGTC..., sequence file that contains - or ? Postive or negative numbers,. or,

Chapter 13 Specific debugging techniques Isolate the problem - Error report : reported line often does not contain error. Check previous steps. - Comment in/out sections with # or '''.... ''' (or """ … """ ?) triple quotes for multiple lines. # … Comment on what you write

Chapter 13 Specific debugging techniques Write verbose software - incorporate diagnostic print statements - if Debug statement, try with authors2.py import sys Debug = True # (insert program statements here) # wherever you want to give feedback, insert these lines if Debug : Print MyList # or you can use sys.stderr.write(MyList)

Chapter 13 Error messages and their meanings Common Python errors -bash : myscript.py : command not found Program not found in the folder listed in your PATH, permission not set to executable, → set PATH variable, try chmod u+x /Users/lucy/scripts/myprogram.py: line 3: import command not found Problem with python program, 1. Perhaps problems with shebang line: #! 2. misspelled built-in Python function within the program

Chapter 13 Error messages and their meanings Common Python errors bad interpreter not a directory #! has a / after /usr/bin/env/ r /usr/bin/env: bad interpreter: No such file or directory Parts in your # ! statement not found → copy in statement into the terminal, see if it launches Python Permission denied chmod u+x

Chapter 13 Error messages and their meanings Common Python errors Name 'x' is not defined - misspelled variable name in the program - variable not originally defined → Inititialize variable e.g., MyList=[], or MyString='' '' - function used, but not imported from a module first - function used without the required module name in dot notation e.g. Randint(5) instead of random.randint(5)

Chapter 13 Error messages and their meanings Common Python errors Indentation error - 4 commas vs 1 tab Attribute error Misspelling of a built-in function e.g. MyString.lowercase() instead of MyString.lower() Type error 'xx' object is not callable Want to get values from a List( ) and not List[ ], wrong interpretation as a function and not a list

Chapter 13 Error messages and their meanings Common Python errors Traceback … zero division error Division by zero ! - Function returns unexpected 0 - Input data with 0 → check user and variable input, to be not blank, non zero, … Non-ASCII character '\xe2' in file Or ävoid non ASCI characters, or type # coding : utf-8 below the #! line Invalid syntax Many things possible : Missing colon after if, else, or for statement Missing close parenthesis, brackets, = instead of ==

Chapter 13 Error messages and their meanings Shell errors Illegal byte sequence - Some command line programs cannot process Unicode characters, °, ≠, … in a file being read Improper use of \ > * < ;

Chapter 13 Making your program more efficient Optimization - sometimes everything works but just too slow / inefficient - multiple ways to do the same thing Measure time that a program needs : import time StartTime = time.time() #perform your commands here print "Elapsed : %.5f" % 9time.time() - StartTime)

Chapter 13 for Line in File: if Line [0] ==">": Name=Line.strip()[1:] # lines with > are Names else: #check for a pre-existing key if Name in Dict.keys(): Dict[Name] += Line.strip() # not a key so define else: Dict[Name] = Line.strip() for Line in File: if Line [0] ==">": Name=Line.strip()[1:] # lines with > are Names else: #check for a pre-existing key if Name in Dict.keys(): Dict[Name] += Line.strip() # not a key so define else: Dict[Name] = Line.strip() Making your program more efficient try and except to handle errors for Line in File: if Line [0] ==">": Name=Line.strip()[1:] # lines with > are Names else : try: # try to append with += # assumes Name is a key Dict[Name] += Line.strip() # oops, not a key so define except KeyError: Dict[Name] = Line.strip() TraditionalFast