#Google App Engine: Logging and Debugging
##Debugging
- Here we have a main.py script with an error in it.
print 'Content-Type: text/plain'
print ''
print 'Hello, world!' - 8
If we try to run this code we will get a blank page, now what?
The process of writing code is the process of debugging. As a programmer, you will always be looking for bugs to fix.
In GoogleAppEngineLauncher, pressing the Logs button will open the Log Console window, which will show your error messages.
ERROR 2015-06-17 22:19:45,960 cgi.py:122] Traceback (most recent call last):
File "/Users/Development/appengine-practice/main.py", line 6, in module
print 'Hello, world!' - 8
TypeError: cannot concatenate 'str' and 'int' objects
- The error messages include information that tells you which file you are actually running and where it is located. Always check and be sure you are running and working on the file you think you are!
#Partner Programming Exercise 1: Copy and paste the following into main.py. It contains a couple errors! Use the console to find and fix the errors.
print 'Content-Type: text/plain'
print ''
if true:
print 'The truth will set you free.'
else
print 'How did I get here?'
#Partner Programming Exercise 2:
Partner Exercise 2: This longer program contains more than one error. Copy and paste it into main.py
Use the console to help you track them down, and fix them all.
def TalkLikeAPirate(sentence):
"""Converts a sentence to pirate-speak. Adapted from Python 3 for Absolute Beginners: http://www.google.com/books?id=sQGFIX_0xCUC&pg=PA242"""
# Strip whitespace and punctuation
sentence = sentence.strip().rstrip('.!')
# Lowercase the first letter of the sentence
sentence = sentence[0].lower() + sentence[1:]
# Piratify the text
sentence = 'Yarr, ' + sentance + ', me hearties!'
retunn sentence
print 'Content-Type: text/plain'
print ''
sentence = 'Hello, world!'
print TalkLikeAPIrate(sentence)
##Logging Logging is a means of tracking events that happen when a program runs.The logging module lets you write messages directly to the console instead of sending them to the browser.
You can use the logging module by importing it at the top of your script: import logging
It allows you to add logging statements to your script. Like this:
logging.info('Hello, doing some logging!')
Once the page is reloaded, the logging statement will appear in the log console.
#Partner Programming Exercise 3: Try copy and pasting this script into main.py:
import logging
def IsPrime(n):
"""A simple (but inefficient) check to see whether a number is prime."""
for possible_factor in range(1, n):
if n % possible_factor == 0:
return False
return True
print 'Content-Type: text/plain'
print ''
n = 100
if IsPrime(n):
print '%d is prime' % n
else:
print '%d is not prime' % n
Change n to a couple different numbers. Notice that it never thinks a number is prime, even when it should. What's the problem here?
These kinds of bugs can be tricky to track down. Fortunately, logging can help you figure out exactly why it doesn't think any numbers are prime.
- Add a logging statement in the for loop:
for possible_factor in range(1, n):
if n % possible_factor == 0:
logging.info('Found a factor: %d', possible_factor)
return False
Now try reloading the page with a different number. Check the logs: do you see the problem? How can you fix it?
#Conclusion
Patience young grasshopper. Debugging can be frustrating but you have tools that can help you fix errors in your code. Read your error messages in the console to find your next step. Use the PEP protocol. Ask for help!
View Google App Engine: Logging and Debugging on Learn.co and start learning to code for free.