# -*- coding:utf-8 -*-from os import chdirhtml = """<html><head><title>LU</title><style>body {font-family: 'Dejavu Sans Mono';}</style></head><body>%s</form></body></html>"""noms = 'dou² smokadezign vachy malacath margeta lord_kellmar tetlis kitni zell'.split()durees = [0, 2, 4, 8, 16]def results(): "show results" f = open('stats.txt', 'r') stathtml = '<table>\n' c = 'o' while True: l = f.readline().strip().split() if not l: break l[1] = int(l[1]) stats = [l[0], '<b>'+(c*l[1])+'</b>'+(c*(durees[-1]-l[1])), l[1]] stathtml += "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n" % (stats[0], stats[1], stats[2]) stathtml += '</table>' f.close() return stathtmldef index(req): chdir('/home/www/py/randpoll') req.content_type = 'text/html' terrorists = req.form.items() # on check si il envoit ses rep ou si il demande les choix try: z = terrorists[0][0] z = 1 except: z = 0 if z: try: # on met les anciennes stats dans olstats[] (si y'en a) stats = open('stats.txt', 'r') olstats = [] while True: l = stats.readline().strip() if not l: break olstats.append(l.split()) olstats[-1][1] = int(olstats[-1][1]) stats.close() # si le fichier existe pas .. except: olstats = [] # convertit les reponses en int nestats = [] for i in range(len(terrorists)): nestats.append([terrorists[i][0], int(terrorists[i][1])]) # merge/fait la moyenne entre les news et les old stats = [] for t in nestats: ok = 0 for e in olstats: if e[0] == t[0]: stats.append([e[0], (e[1]+t[1])/2]) ok = 1 break if not ok: # si c'est la 1ere fois qu'on le voit stats.append(t) # pour ceux qui étaient la avant mais plus now for e in olstats: if not e[0] in str(stats): stats.append(e) # on les récrit f = open('stats.txt', 'w') for s in stats: f.write('%s %s\n' % (s[0], s[1])) f.close() return html % results() # si le visiteur arrive tout juste else: form = '<form action="poll.py" method="post">\n' for n in noms: form += '<p><b>%s</b>\n' % n for d in durees: form += '<input type="radio" name="%s" value=%s /> %sj\n' % (n, d, d) form += '</p>\n\n' form += '<input type="submit">' return html % form