Created
February 18, 2010 12:21
-
-
Save yuribossa/307608 to your computer and use it in GitHub Desktop.
FizzBuzz on Google App Engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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('[email protected]', 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