-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode.py
More file actions
46 lines (39 loc) · 1.02 KB
/
code.py
File metadata and controls
46 lines (39 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import web
from web import form
render = web.template.render('templates/')
urls = (
'/calc', 'calc',
'/(.*)', 'hello',
)
app = web.application(urls, globals())
calc_input = form.Form(
form.Textbox('value 1',
form.notnull,
form.regexp('\d+', 'Must be a number.')),
form.Dropdown('operator', ['+', '-', '*', '/']),
form.Textbox('value 2'),
)
class calc:
def GET(self):
form = calc_input()
return render.formtest(form)
def POST(self):
form = calc_input()
if not form.validates():
return render.formtest(form)
else:
val1 = int(form['value 1'].value)
val2 = int(form['value 2'].value)
op = form['operator'].value
answer = {
'+': lambda x,y: x+y,
'-': lambda x,y: x-y,
'*': lambda x,y: x*y,
'/': lambda x,y: x/y,
}[op](val1, val2)
return '%d %s %d = %s' % (val1, op, val2, answer)
class hello:
def GET(self, name):
return render.index(name)
if __name__ == "__main__":
app.run()