mirror of
https://github.com/python/cpython.git
synced 2025-12-09 10:37:17 +00:00
Put code in a main() function; loosen up the spacing to match current code style
This commit is contained in:
parent
3725dea9c3
commit
3550613502
1 changed files with 39 additions and 26 deletions
|
|
@ -1,10 +1,19 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
# Takes an optional filename, defaulting to this file itself.
|
||||
# Reads the file and compresses the content using level 1 and level 9
|
||||
# compression, printing a summary of the results.
|
||||
|
||||
import zlib, sys
|
||||
if len(sys.argv)>1: filename=sys.argv[1]
|
||||
else: filename='zlibdemo.py'
|
||||
|
||||
def main():
|
||||
if len(sys.argv) > 1:
|
||||
filename = sys.argv[1]
|
||||
else:
|
||||
filename = sys.argv[0]
|
||||
print 'Reading', filename
|
||||
f=open(filename, 'r') # Get the data to compress
|
||||
|
||||
f = open(filename, 'rb') # Get the data to compress
|
||||
s = f.read()
|
||||
f.close()
|
||||
|
||||
|
|
@ -24,7 +33,8 @@ decompressor=zlib.decompressobj()
|
|||
comptext = decomp = ''
|
||||
for i in range(0, len(s), chunk):
|
||||
comptext = comptext+compressor.compress(s[i:i+chunk])
|
||||
comptext=comptext+compressor.flush() # Don't forget to call flush()!!
|
||||
# Don't forget to call flush()!!
|
||||
comptext = comptext + compressor.flush()
|
||||
|
||||
for i in range(0, len(comptext), chunk):
|
||||
decomp = decomp + decompressor.decompress(comptext[i:i+chunk])
|
||||
|
|
@ -33,3 +43,6 @@ decomp=decomp+decompressor.flush()
|
|||
print 'Progressive compression (level 9):'
|
||||
print ' Original:', len(s), 'Compressed:', len(comptext),
|
||||
print 'Uncompressed:', len(decomp)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue