[code.view]

[top] / python / PyMOTW / codecs / codecs_zlib.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Demonstrate a non-Unicode codec.
     """
     #end_pymotw_header
     
     import codecs
     from cStringIO import StringIO
     
     from codecs_to_hex import to_hex
     
     buffer = StringIO()
     stream = codecs.getwriter('zlib')(buffer)
     
     text = 'abcdefghijklmnopqrstuvwxyz\n' * 50
     
     stream.write(text)
     stream.flush()
     
     print 'Original length :', len(text)
     compressed_data = buffer.getvalue()
     print 'ZIP compressed  :', len(compressed_data)
     
     buffer = StringIO(compressed_data)
     stream = codecs.getreader('zlib')(buffer)
     
     first_line = stream.readline()
     print 'Read first line :', repr(first_line)
     
     uncompressed_data = first_line + stream.read()
     print 'Uncompressed    :', len(uncompressed_data)
     print 'Same            :', text == uncompressed_data
     

[top] / python / PyMOTW / codecs / codecs_zlib.py

contact | logmethods.com