Giter VIP home page Giter VIP logo

cssi-6.3-aep-logging-debugging's Introduction

#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 Mindset of Debugging

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.

cssi-6.3-aep-logging-debugging's People

Contributors

fislabstest avatar nanselmo avatar norahsolo avatar sarogers avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.