Poll script in Python

J

Josiah Carlson

Is there a poll script available in Python?

What do you mean by "poll"?

Do you mean "poll a file for changes", "poll a file handle for
readability/writability", "poll a socket", ..., "a web poll for taking
surveys",...?

- Josiah
 
J

Jim Hefferon

(e-mail address removed) (Matthew K Jensen) wrote
When in doubt, make your own :) !!!
That seems wasteful, although I admit that finding something that
exactly suits you can be hard. I looked around for survey software a
while ago and didn't find any that worked for me, so I wrote some,
called Theodolite. If you are interested in the blurb at
http://joshua.smcvt.edu/theo, I'd be glad for beta tester feedback
(This is not a permanent address: I haven't yet had the chance to
upload the material to a Sourceforge-like site. If you mail me, I'll
tell you how to get it.)

Jim
 
D

Dan Bishop

I'm very interested in increasing the range of 'standard' CGI scripts
available in python. If you (or anyone else ?) wants help in writing a
simple one that could be used as a plugin to other webpages then
contact me and we'll work on it.

Here's some code (with organization-specific stuff replaced by
asterisks) that I wrote for polls that use ranked ballots:

# vote.cgi

#!/usr/bin/python

import os

def printError(message):
print ' <p>%s</p>\n </body>\n</html>' % message

def ordinal(n):
if n == 1:
return str(n) + 'st'
if n == 2:
return str(n) + 'nd'
if n == 3:
return str(n) + 'rd'
if n < 20:
return str(n) + 'th'
return ordinal(n % 10)

def printBallot():
candidates = file('CANDIDATES').read().splitlines()
n = len(candidates)
print ' <form method="post" action="castvote.cgi" />'
print ' <table>'
print ' <tr>'
print ' <th>Candidate</th>'
for i in xrange(1, n + 1):
print ' <th>%s choice</th>' % ordinal(i)
print ' </tr>'
for (candidateNumber, candidateName) in enumerate(candidates):
print ' <tr>'
print ' <td class="candidate">%s</td>' %
candidateName
for i in xrange(1, n + 1):
print ' <td>'
print ' <input type="radio" name="rank-%d" '
\
'value="%d"' % (candidateNumber,
i),
if i == n:
print 'checked',
print '/>'
print ' </td>'
print ' </tr>'
print ' </table>'
print ' <p class="votesubmission">'
print ' <input type="submit" value="Submit Ballot" />'
print ' </p>'
print ' </form>'
print ' </body>'
print '</html>'

print '''Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=us-ascii" />
<link rel="stylesheet" type="text/css" href="../****/style.css"
/>
<title>Vote!</title>
</head>
<body>'''
address = os.environ['REMOTE_ADDR']
if address.startswith(****):
try:
ballots = file('BALLOTS').read()
except IOError:
ballots = ''
if (address + '\n') in ballots:
printError('This isn\'t Chicago. You\'re only allowed to vote
once.')
else:
printBallot()
else:
printError('You can only vote from within ****')

# castvote.cgi

#!/usr/bin/python

import cgi
import os

address = os.environ['REMOTE_ADDR']
form = cgi.FieldStorage()
ballot = []
i = 0
while True:
try:
ballot.append(int(form['rank-%d' % i].value))
except KeyError:
break
i += 1

f = file('BALLOTS', 'a')
f.write('%r # %s\n' % (ballot, address))
f.close()

print '''Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=us-ascii" />
<link rel="stylesheet" type="text/css" href="../****/style.css"
/>
<title>Vote Submitted</title>
</head>
<body>
<p>Thank you for voting in ****'s ****. Results will be
announced at
****.</p>
<p>Click <a href="../****">here</a> to return to the
website.</p>
</body>
</html>'''
 
M

Michael Foord

Here's some code (with organization-specific stuff replaced by
asterisks) that I wrote for polls that use ranked ballots:

# vote.cgi
[snip..]

Cool - looks interesting.

Thanks

Fuzzy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top