#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# rot13.py
#
# Copyright 2014 [email protected]
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
from escape_function import escape_html
import webapp2
form = """
<!DOCTYPE html>
<html>
<head>
<title>Unit 2 Rot 13</title>
</head>
<body>
<h2>Enter some text to ROT13:</h2>
<form method="post">
<textarea name="text" style="height: 100px; width: 400px;">%(text)s</textarea>
<br>
<input type="submit">
</form>
</body>
</html>
"""
class MainPage(webapp2.RequestHandler):
def write_form(self, text = ""):
self.response.out.write(form % {"text": escape_html(text)})
def get(self):
self.write_form()
def post(self):
user_text = self.rot13a(self.request.get('text'))
self.write_form(user_text)
def rot13a(self, text):
abc = list("abcdefghijklmnopqrstuvwxyz")
ABC = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
lst = list(text)
newlst = []
for i in lst:
if i in abc:
ind = abc.index(i)
ind = (ind + 13)%26
newlst.append(abc[ind])
elif i in ABC:
ind = ABC.index(i)
ind = (ind + 13)%26
newlst.append(ABC[ind])
else:
newlst.append(i)
txt = ""
text = txt.join(newlst)
return text
application = webapp2.WSGIApplication([('/', MainPage)],debug=True)
Comments