[Tutor] Online interpreter?

Michael P. Reilly arcege@speakeasy.net
Mon, 16 Jul 2001 15:20:29 -0400 (EDT)


Mike Serpa wrote
> There isn't an online interpreter of Python somewhere is there? Like
> this one I found for Ruby?  http://www.ruby.ch/en/rubymain.shtml
> 
> Sometimes I want to try some things out on Python when I'm away from my
> PC.

The easiest method, imo, would be to make a JPython applet wrapped around
an instance of code.InteractiveConsole.

Otherwise, if you just had CGI, make a picklable rexec class and wrap
the CGI around it.

class PicklableRexec(rexec.RExec):
  # keep state between CGI calls
  def __getstate__(self):
    # modules loaded
    modnames = self.modules.keys()
    # main module values, do not include modules
    mainmod = self.modules['__main__']
    mainvals = []
    mainmods = []
    for name, value in mainmod.__dict__.items():
      if not isinstance(value, types.ModuleType):
        mainvals.append( (name, value) )
      else:
        mainmods.append( name )
    return (modnames, mainvals, mainmods)
  def __setstate__(self, (modnames, mainvals, mainmods)):
    self.__init__()
    for name in modnames:
      self.r_import(name)
    for name in mainmods:
      self.r_exec('input %s' % name)
    mainmod = self.modules['__main__']
    for (name, value) in mainvals:
      setattr(mainmod, name, value)

Pickling an instance of this will create a text string that can be passed
to the next HTML form, properly quoted of course.  When submitting the
form, unpickle the string to get the current state.  The input string can
be called through the s_exec method.  May have to fix up the I/O for CGI.

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |