Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #1/usr/bin/env python
- # this is from The Python
- # Standard Library by example
- # ISBN13: 9780321767349
- import codecs
- import sys
- from codecs_to_hex import to_hex
- text = 'abcdefghijklmnopqrstuvwxyz\n'
- repetitions = 50
- print 'Text length :', len(text)
- print 'Repetitions :', repetitions
- print 'Expected len:', len(text) * repetitions
- # Encode the text several times to build up a large amount of data
- encoder = codecs.getincrementalencoder('bz2')()
- encoded = []
- print
- print 'Encoding:',
- for i in range(repetitions):
- en_c = encoder.encode(text, final=(i == repetitions - 1))
- if en_c:
- print '\nEncoded : {} bytes'.format(len(en_c))
- encoded.append(en_c)
- else:
- sys.stdout.write('.')
- bytes = ''.join(encoded)
- print
- print 'Total encoded length:', len(bytes)
- print
- # Decode the byte string one byte at a time
- decoder = codecs.getincrementaldecoder('bz2')()
- decoded = []
- print 'Decoding:',
- for i, b in enumerate(bytes):
- final = (i + 1) == len(text)
- c = decoder.decode(b, final)
- if c:
- print '\nDecoded : {} characters'.format(len(c))
- print 'Decoding:',
- decoded.append(c)
- else:
- sys.stdout.write('.')
- print
- restored = u''.join(decoded)
- print
- print 'Total uncompressed length:', len(restored)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement