Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{StringIO} --- |
Fred Drake | 543e19d | 1999-04-21 18:15:22 +0000 | [diff] [blame] | 2 | Read and write strings as files} |
| 3 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{StringIO} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Read and write strings as if they were files.} |
| 6 | |
Guido van Rossum | cda3d7d | 1997-03-03 16:01:21 +0000 | [diff] [blame] | 7 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 8 | This module implements a file-like class, \class{StringIO}, |
Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 9 | that reads and writes a string buffer (also known as \emph{memory |
Fred Drake | 543e19d | 1999-04-21 18:15:22 +0000 | [diff] [blame] | 10 | files}). See the description on file objects for operations (section |
| 11 | \ref{bltin-file-objects}). |
Guido van Rossum | cda3d7d | 1997-03-03 16:01:21 +0000 | [diff] [blame] | 12 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 13 | \begin{classdesc}{StringIO}{\optional{buffer}} |
| 14 | When a \class{StringIO} object is created, it can be initialized |
Guido van Rossum | cda3d7d | 1997-03-03 16:01:21 +0000 | [diff] [blame] | 15 | to an existing string by passing the string to the constructor. |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 16 | If no string is given, the \class{StringIO} will start empty. |
| 17 | \end{classdesc} |
Guido van Rossum | cda3d7d | 1997-03-03 16:01:21 +0000 | [diff] [blame] | 18 | |
Fred Drake | 7e9383a | 1998-04-11 18:05:24 +0000 | [diff] [blame] | 19 | The following methods of \class{StringIO} objects require special |
| 20 | mention: |
| 21 | |
Fred Drake | 8fe533e | 1998-03-27 05:27:08 +0000 | [diff] [blame] | 22 | \begin{methoddesc}{getvalue}{} |
| 23 | Retrieve the entire contents of the ``file'' at any time before the |
| 24 | \class{StringIO} object's \method{close()} method is called. |
| 25 | \end{methoddesc} |
| 26 | |
| 27 | \begin{methoddesc}{close}{} |
| 28 | Free the memory buffer. |
| 29 | \end{methoddesc} |
Fred Drake | 9463de2 | 1998-04-11 20:05:43 +0000 | [diff] [blame] | 30 | |
| 31 | |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 32 | \section{\module{cStringIO} --- |
Fred Drake | 543e19d | 1999-04-21 18:15:22 +0000 | [diff] [blame] | 33 | Faster version of \module{StringIO}} |
Fred Drake | cd71aa2 | 1999-02-18 21:13:03 +0000 | [diff] [blame] | 34 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 35 | \declaremodule{builtin}{cStringIO} |
Fred Drake | 543e19d | 1999-04-21 18:15:22 +0000 | [diff] [blame] | 36 | \modulesynopsis{Faster version of \module{StringIO}, but not |
| 37 | subclassable.} |
| 38 | \moduleauthor{Jim Fulton}{jfulton@digicool.com} |
Fred Drake | cd71aa2 | 1999-02-18 21:13:03 +0000 | [diff] [blame] | 39 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
Fred Drake | 9463de2 | 1998-04-11 20:05:43 +0000 | [diff] [blame] | 40 | |
| 41 | The module \module{cStringIO} provides an interface similar to that of |
Fred Drake | 543e19d | 1999-04-21 18:15:22 +0000 | [diff] [blame] | 42 | the \refmodule{StringIO} module. Heavy use of \class{StringIO.StringIO} |
Fred Drake | 9463de2 | 1998-04-11 20:05:43 +0000 | [diff] [blame] | 43 | objects can be made more efficient by using the function |
| 44 | \function{StringIO()} from this module instead. |
| 45 | |
| 46 | Since this module provides a factory function which returns objects of |
| 47 | built-in types, there's no way to build your own version using |
Fred Drake | 543e19d | 1999-04-21 18:15:22 +0000 | [diff] [blame] | 48 | subclassing. Use the original \refmodule{StringIO} module in that case. |
| 49 | |
| 50 | The following data objects are provided as well: |
| 51 | |
| 52 | |
| 53 | \begin{datadesc}{InputType} |
| 54 | The type object of the objects created by calling |
| 55 | \function{StringIO} with a string parameter. |
| 56 | \end{datadesc} |
| 57 | |
| 58 | \begin{datadesc}{OutputType} |
| 59 | The type object of the objects returned by calling |
| 60 | \function{StringIO} with no parameters. |
| 61 | \end{datadesc} |
| 62 | |
| 63 | |
| 64 | There is a C API to the module as well; refer to the module source for |
| 65 | more information. |