libstringio.tex 2.12 KB
Newer Older
Fred Drake's avatar
Fred Drake committed
1
\section{\module{StringIO} ---
2
         Read and write strings as files}
3

4
\declaremodule{standard}{StringIO}
5
\modulesynopsis{Read and write strings as if they were files.}
6 7


8
This module implements a file-like class, \class{StringIO},
9
that reads and writes a string buffer (also known as \emph{memory
10 11
files}). See the description on file objects for operations (section
\ref{bltin-file-objects}).
12

13 14
\begin{classdesc}{StringIO}{\optional{buffer}}
When a \class{StringIO} object is created, it can be initialized
15
to an existing string by passing the string to the constructor.
16 17
If no string is given, the \class{StringIO} will start empty.
\end{classdesc}
18

19 20 21
The following methods of \class{StringIO} objects require special
mention:

22 23 24 25 26 27 28 29
\begin{methoddesc}{getvalue}{}
Retrieve the entire contents of the ``file'' at any time before the
\class{StringIO} object's \method{close()} method is called.
\end{methoddesc}

\begin{methoddesc}{close}{}
Free the memory buffer.
\end{methoddesc}
30 31


Fred Drake's avatar
Fred Drake committed
32
\section{\module{cStringIO} ---
33
         Faster version of \module{StringIO}}
34

35
\declaremodule{builtin}{cStringIO}
36 37 38
\modulesynopsis{Faster version of \module{StringIO}, but not
                subclassable.}
\moduleauthor{Jim Fulton}{jfulton@digicool.com}
39
\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
40 41

The module \module{cStringIO} provides an interface similar to that of
42
the \refmodule{StringIO} module.  Heavy use of \class{StringIO.StringIO}
43 44 45 46 47
objects can be made more efficient by using the function
\function{StringIO()} from this module instead.

Since this module provides a factory function which returns objects of
built-in types, there's no way to build your own version using
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
subclassing.  Use the original \refmodule{StringIO} module in that case.

The following data objects are provided as well:


\begin{datadesc}{InputType}
  The type object of the objects created by calling
  \function{StringIO} with a string parameter.
\end{datadesc}

\begin{datadesc}{OutputType}
  The type object of the objects returned by calling
  \function{StringIO} with no parameters.
\end{datadesc}


There is a C API to the module as well; refer to the module source for 
more information.