Testing and Debugging Flashcards
Bugs
Error/flaw that leads to malfunction
Syntax error
incompatibility with syntax of programming language
Run time error
python understands what you are saying but runs into trouble when following your instructions
logic error
design error, code runs but doesn’t do what it’s supposed to
Debugging
process of systematically finding and fixing bugs
PEP8
no more than 79 characters per line
4 spaces per indent
Call stack
- one function being called on top of another
- facilitate function nesting, allow functions to communicate with each other, preserve function’s local state/namespace
- push and pop
Defensive programming
- build code bit by bit
- testing as you go again known input/output
- log each step of process
- use comments
- lookout for common gotchas
General approach to debugging
- reproduce bug
- determine exact problem
- eliminate obvious causes
- divide process, separate working and non-working parts
- reassess info, step through process again
- as you proceed, make predictions about what should happen and verify outcome
Verbosity
if verbosity statement in front of each print statement depending on each verbosity, different print statements will be executed
logging library
debug
Inspecting run state with codde
- suspend execution of code and examine variable values with code
- import code
- code.interact()
- interrupts code and throws it out into interpreter, stepping through code
Starred arguments
- = unpacked into individual arguments
* * = unpacked into individual keyword arguments
Map function
applies function to all items in input list
map(function, input list)
Common run-time exceptions
- Assertion error = assertion fails
- index error = index out of range
- key error = key not found in dict
Handle exceptions with try…except…