Differences between revisions 2 and 3
Revision 2 as of 2007-04-02 22:37:07
Size: 651
Editor: cscfpc15
Comment:
Revision 3 as of 2007-04-02 22:49:46
Size: 998
Editor: cscfpc15
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
The Python2.5's {{{codecs.StreamReaderWriter}}} combines StreamReader and StreamWriter. The Python2.5's {{{codecs.StreamReaderWriter}}} combines StreamReader and StreamWritet. The wrapper will read narrow {{{str}}} strings from the underlying stream and decode them to {{{unicode}}} strings. On writing {{{unicode}}} data to the wrapper, it will encode them to narrow {{{str}}} strings.

The user of the wrapper should specify character sets by supplying class definitions for the respective stream reader and writer.

The Python2.5's codecs.StreamReaderWriter combines StreamReader and StreamWritet. The wrapper will read narrow str strings from the underlying stream and decode them to unicode strings. On writing unicode data to the wrapper, it will encode them to narrow str strings.

The user of the wrapper should specify character sets by supplying class definitions for the respective stream reader and writer.

Pseudocode:

   1 class StreamReaderWriter:
   2     def __init__(self, stream, class_sr, class_sw):
   3         self.r = class_sr(stream)
   4         self.w = class_sw(stream)
   5 
   6     def read(self):
   7         return self.r.read()
   8 
   9     def write(self, data):
  10         return self.w.write(data)

The codecs module defines a function codecs.open(name, encoding) that returns an instance of StreamReaderWriter configured with the supplied encoding.


See also: StreamReader, StreamWriter, StreamRecoder.


CategoryUnicode

StreamReaderWriter (last edited 2008-11-15 13:59:38 by localhost)

Unable to edit the page? See the FrontPage for instructions.