On 4/23/06, Jeffrey Elkner <[EMAIL PROTECTED]> wrote:
On Sat, 22 Apr 2006 18:00:27 -0500, Michael Tobis <[EMAIL PROTECTED]> wrote:
>I think I've got a realistic design for a doctest-driven beginners'
>programming courseware suitable for presenting HHTLCSpy - type
>materials that can be put together in a week or so with my level of
>skill. (Or a half-day if I let Ian B in on it... ) That week probably
>won't happen before early June, but I think I'll have something of
>immediate interest to you early enough in the summer that we can
>develop real curriculum in time for next fall.

Thanks, Michael!  I've written up a brief description of the tool I'm looking 
for here:

http://dc.ubuntu-us.org/projects/doctest-quiz.php

and a briefer launchpad spec here:

https://launchpad.net/products/cando/+spec/doctest-quiz

For those interested, you will find attached a simple app that
essentially does what Jeff described, albeit entirely on a single
computer. (Thus, if you can run Python on that machine, you can run it
via a web browser without sandboxing).

It requires CherryPy.

Here's what is required:
1. Download and install CherryPy.  http://www.cherrypy.org/
2. Save doctestquiz.py in the tutorial directory that comes with
cherrypy (so you can use the default configuration file).
3. Run doctestquiz
4. Point your browser to http://localhost:8080/index
5. Have fun!
"""
DocTestQuiz by Andre Roberge
"""

import cherrypy
import os
from tempfile import gettempdir
TMP_DIR = gettempdir()

EXAMPLES = [
"""
>>> p = Animal()
>>> p.name
''
>>> p.friends
[]
""",
"""
>>> p = Animal('Pooh')
>>> p.name
'Pooh'
>>> p.addFriend('Piglet')
>>> p.friends
['Piglet']
"""  ]

title = 'Learn Python'
instructions = 'Write just enough Python code to make '+\
               'the following DocTests pass:'
begin_output = "The result is :<pre><font color='blue'>"
end_output = "</font></pre>"
triple_quote = "\'\'\'"
example = 0
success = False

class PythonRunner(object):

    def header(self):
        return '''
            <html>
            <head>
                <title>%s</title>
            </head>
            <body>
            <h2>%s</h2>
            <p>%s</p>
            <pre><font color='blue'><b>%s</b></font></pre>
        ''' % (title, title, instructions, EXAMPLES[example])
    
    def footer(self):
        return '''
            </body>
            </html>
        '''      
    
    def index(self, code=None):
        global success, example
        count = cherrypy.session.get('count', 0) + 1
        cherrypy.session['count'] = count
        if success:
            success = False
            count = 1
            example += 1
        if code is None:
            code = ''
        output = ''
        if count > 1: 
            cherrypy.session['code'] = triple_quote + EXAMPLES[example] + \
                                       triple_quote + '\n'+ code

            tmp_filename = os.path.join(TMP_DIR, 'my_file.dat')
            f = open(tmp_filename, 'w')
            test_code = cherrypy.session['code'] + \
                        "\nimport doctest\ndoctest.testmod()"
            f.write(test_code)
            f.close()
            f_in, f_out = os.popen4("python %s"%tmp_filename)
            result = ''
            for line in f_out.readlines():
                result += line
            if result == '':
                output = "<h2><font color='red'>Success!</font></h2>" 
                button1 = ''
                button2 = '<form action="index"><input type="submit" value="Next"/></form>'
                success = True
            else:

                output = begin_output + result + end_output
                button1 = '<input type="submit" value="Run docTest"/>'
                button2 = ''
        else:
            button1 = '<input type="submit" value="Run docTest"/>'
            button2 = ''
        if example == len(EXAMPLES):
            return "<h2><font color='red'>All done!</font></h2>"
        return self.header()+'''
            Type in your Python code.
            <form action="index" method="GET">
            <textarea name="code" rows=5 cols=80>%s</textarea><br/>
            %s
            </form>
            <br/>
            %s %s
        ''' % (code, button1, output + self.footer(), button2)
    index.exposed = True

cherrypy.root = PythonRunner()
cherrypy.config.update({'session_filter.on': True})

if __name__ == '__main__':
    cherrypy.config.update(file = 'tutorial.conf')
    cherrypy.server.start()





_______________________________________________
Edu-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to