Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. #
  4. # Copyright (c) 2010 Doug Hellmann. All rights reserved.
  5. #
  6. """Using the incremental reader/writer interfaces.
  7. """
  8. #end_pymotw_header
  9.  
  10. import codecs
  11. import sys
  12.  
  13. from codecs_to_hex import to_hex
  14.  
  15. text = 'abcdefghijklmnopqrstuvwxyz\n'
  16. repetitions = 50
  17.  
  18. print 'Text length :', len(text)
  19. print 'Repetitions :', repetitions
  20. print 'Expected len:', len(text) * repetitions
  21.  
  22. # Encode the text several times to build up a large amount of data
  23. encoder = codecs.getincrementalencoder('bz2')()
  24. encoded = []
  25.  
  26. print
  27. print 'Encoding:',
  28. for i in range(repetitions):
  29. en_c = encoder.encode(text, final = (i==repetitions-1))
  30. if en_c:
  31. print '\nEncoded : {} bytes'.format(len(en_c))
  32. encoded.append(en_c)
  33. else:
  34. sys.stdout.write('.')
  35.  
  36. bytes = ''.join(encoded)
  37. print
  38. print 'Total encoded length:', len(bytes)
  39. print
  40.  
  41. # Decode the byte string one byte at a time
  42. decoder = codecs.getincrementaldecoder('bz2')()
  43. decoded = []
  44.  
  45. print 'Decoding:',
  46. for i, b in enumerate(bytes):
  47. final= (i+1) == len(text)
  48. c = decoder.decode(b, final)
  49. if c:
  50. print '\nDecoded : {} characters'.format(len(c))
  51. print 'Decoding:',
  52. decoded.append(c)
  53. else:
  54. sys.stdout.write('.')
  55. print
  56.  
  57. restored = u''.join(decoded)
  58.  
  59. print
  60. print 'Total uncompressed length:', len(restored)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement