spyce
home
license
community
download
examples
resources
wishlist
contrib (@sf)
documentation
intro
lang
runtime
modules
tags
install
exits
sourceforge
statistics
freshmeat

Documentation - Modules
[[ Spyce ]]
Python Server Pages
by Rimon Barr

Prev: 4.3 - Error Up: 4 - Modules Next: 4.5 - Spylambda

4.4. Stdout

The stdout module is loaded implicitly and redirects Python's sys.stdout (in a thread-safe manner) to the appropriate response object for the duration of Spyce processing. This allows one to use print, without having to write print >> response, .... The stdout module provides a variable stdout.stdout, which refers to the original stream, but is unlikely to be needed. It may also be useful to know that sys.stderr is, under many configurations, connected to the webserver error log.

In addition, the stdout module provides the following functions for capturing or redirecting output:

  • push( [filename] ):
    Begin capturing output. Namely, the current output stream is pushed onto the stack and replaced with a memory buffer. An optional filename may be associated with this operation (see pop() method below).
  • pop():
    Close current output buffer, and return the captured output as a string. If a filename was associated with the push(), then the string will also be written to that file.
  • capture(f, [*args], [**kwargs] ):
    Push the current stream, call the given function f with any supplied arguments *args and keyword arguments **kwargs, and then pop it back. Capture returns a tuple (r,s), where r is the result returned by f and s is a string of its output.

The example below show how the module is used:

examples/stdout.spy
<html><body>
  [[ print '''Using the stdout module redirects 
    stdout to the response object, so you can use
    <b>print</b>!''']]<br>
  redirecting stdout can be used to...
  [[stdout.push()]]
  [[print 'capture']] out[[='put']]
  [[cached = stdout.pop()]]
  ... for later: <br>
  [[=cached]]
</body></html>
Run this code.
(requires Spyce-enabled web server)


Prev: 4.3 - Error Up: 4 - Modules Next: 4.5 - Spylambda


© 2002-07 Rimon Barr
email: rimon@acm.org
Spyce Powered SourceForge Logo [[ Spyce ]]
Python Server Pages
version 1.3.13