Skip to content

Instantly share code, notes, and snippets.

@yuribossa
Created February 18, 2010 12:21
Show Gist options
  • Save yuribossa/307608 to your computer and use it in GitHub Desktop.
Save yuribossa/307608 to your computer and use it in GitHub Desktop.
FizzBuzz on Google App Engine
# -*- coding: utf8 -*-
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import memcache
from google.appengine.api import mail
from google.appengine.api.labs import taskqueue
from google.appengine.api import users
class FizzBuzzHandler(webapp.RequestHandler):
def post(self):
mailaddress = self.request.get('mailaddress')
num = self.request.get('number')
if not mailaddress or not num:
return
key = 'fizzbuzz' + mailaddress
num = int(num)
result = memcache.get(key)
if result is None:
return
result += self.fizzbuzz(num) + '\n'
if num < 100:
memcache.set(key, result, 3600)
taskqueue.add(url='/fizzbuzz2', params={'mailaddress': mailaddress, 'number': str(num+1)})
else:
mail.send_mail('hoge@foo.com', mailaddress, 'FizzBuzz Result', result)
def fizzbuzz(self, num):
if num % 3 == 0:
if num % 5 == 0:
return 'FizzBuzz'
else:
return 'Fizz'
elif num % 5 == 0:
return 'Buzz'
else:
return str(num)
class MainHandler(webapp.RequestHandler):
def get(self):
w = self.response.out.write
w('<html><body>')
user = users.get_current_user()
if user:
memcache.set('fizzbuzz'+user.email(), '', 3600)
taskqueue.add(url='/fizzbuzz2', params={'mailaddress': user.email(), 'number': '1'})
w(('処理が完了したら %s にメールを送ります' % user.email()))
w(('<br /><a href="%s">ログアウト</a>' % users.create_logout_url('/fizzbuzz')))
else:
w((u'<a href="%s">ログインするとFizzBuzz開始</a>' % users.create_login_url('/fizzbuzz')))
w('</body></html>')
application = webapp.WSGIApplication(
[('/fizzbuzz2', FizzBuzzHandler),
('/fizzbuzz', MainHandler)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment