Differences between revisions 2 and 6 (spanning 4 versions)
Revision 2 as of 2007-04-02 22:28:18
Size: 935
Editor: cscfpc15
Comment:
Revision 6 as of 2008-11-15 13:59:59
Size: 1138
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
The {{{codecs.StreamRecoder}}} class re-encodes data between 2 character sets. Its constructor specifies the 2 character sets in different ways. The user should specify the internal character set as a pair of {{{encoder}}} and {{{decoder}}}. She should provide the external character set by supplying class definitions of a codec-specific {{{StreamReader}}} and {{{StreamWriter}}}. The {{{codecs.StreamRecoder}}} class re-encodes data between 2 character sets. Its constructor expects the information on 2 character sets in different forms. The user should specify the internal character set as a pair of {{{encoder}}} and {{{decoder}}}. She should provide the external character set by supplying class definitions of a codec-specific {{{StreamReader}}} and {{{StreamWriter}}}.
Line 8: Line 8:
    def __init__(self, stream, e, d, sr_class, sw_class):     def __init__(self, stream, e, d, class_sr, class_sw):
Line 10: Line 10:
        self.r = sr_class(stream)
        self.w = sw_class(stream)
        self.r = class_sr(stream)
        self.w = class_sw(stream)
Line 22: Line 22:
The {{{codecs}}} module defines a function {{{codecs.EncodedFile()}}} that will return an instance of {{{StreamRecoder}}} according to the supplied internal and external character sets.

The codecs.StreamRecoder class re-encodes data between 2 character sets. Its constructor expects the information on 2 character sets in different forms. The user should specify the internal character set as a pair of encoder and decoder. She should provide the external character set by supplying class definitions of a codec-specific StreamReader and StreamWriter.

Pseudocode of codecs.StreamRecoder of the Python2.5 version:

Toggle line numbers
   1 class StreamRecoder:
   2     def __init__(self, stream, e, d, class_sr, class_sw):
   3         # internal encoder, decoder^  ^external decoder, encoder
   4         self.r = class_sr(stream)
   5         self.w = class_sw(stream)
   6         ...
   7 
   8     def read(self):
   9         return self.e(self.r.read())
  10 
  11     def write(self, data):
  12         return self.w.write(self.d(data))

The codecs module defines a function codecs.EncodedFile() that will return an instance of StreamRecoder according to the supplied internal and external character sets.


See also: StreamReader, StreamWriter, StreamReaderWriter.


CategoryUnicode

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

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