|
|
Documentation - Modules
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
|
|