summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew M. Kuchling <amk@amk.ca>2006-06-03 23:15:56 (GMT)
committerAndrew M. Kuchling <amk@amk.ca>2006-06-03 23:15:56 (GMT)
commit3550613502c6a9cd28ed1ad0b5cde1194cd27139 (patch)
tree1ed970b1c7edd4e8701da84b9d1901e123a9d25e
parent3725dea9c39b3e56532fe4be26545c84a4402f46 (diff)
downloadcpython-3550613502c6a9cd28ed1ad0b5cde1194cd27139.zip
cpython-3550613502c6a9cd28ed1ad0b5cde1194cd27139.tar.gz
cpython-3550613502c6a9cd28ed1ad0b5cde1194cd27139.tar.bz2
Put code in a main() function; loosen up the spacing to match current code style
-rwxr-xr-xDemo/zlib/zlibdemo.py77
1 files changed, 45 insertions, 32 deletions
diff --git a/Demo/zlib/zlibdemo.py b/Demo/zlib/zlibdemo.py
index 5a0ab63..b449c19 100755
--- a/Demo/zlib/zlibdemo.py
+++ b/Demo/zlib/zlibdemo.py
@@ -1,35 +1,48 @@
#!/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'
-print 'Reading', filename
-f=open(filename, 'r') # Get the data to compress
-s=f.read()
-f.close()
-
-# First, we'll compress the string in one step
-comptext=zlib.compress(s, 1)
-decomp=zlib.decompress(comptext)
-
-print '1-step compression: (level 1)'
-print ' Original:', len(s), 'Compressed:', len(comptext),
-print 'Uncompressed:', len(decomp)
-
-# Now, let's compress the string in stages; set chunk to work in smaller steps
-
-chunk=256
-compressor=zlib.compressobj(9)
-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()!!
-
-for i in range(0, len(comptext), chunk):
- decomp=decomp+decompressor.decompress(comptext[i:i+chunk])
-decomp=decomp+decompressor.flush()
-
-print 'Progressive compression (level 9):'
-print ' Original:', len(s), 'Compressed:', len(comptext),
-print 'Uncompressed:', len(decomp)
+
+def main():
+ if len(sys.argv) > 1:
+ filename = sys.argv[1]
+ else:
+ filename = sys.argv[0]
+ print 'Reading', filename
+
+ f = open(filename, 'rb') # Get the data to compress
+ s = f.read()
+ f.close()
+
+ # First, we'll compress the string in one step
+ comptext = zlib.compress(s, 1)
+ decomp = zlib.decompress(comptext)
+
+ print '1-step compression: (level 1)'
+ print ' Original:', len(s), 'Compressed:', len(comptext),
+ print 'Uncompressed:', len(decomp)
+
+ # Now, let's compress the string in stages; set chunk to work in smaller steps
+
+ chunk = 256
+ compressor = zlib.compressobj(9)
+ decompressor = zlib.decompressobj()
+ comptext = decomp = ''
+ for i in range(0, len(s), chunk):
+ comptext = comptext+compressor.compress(s[i:i+chunk])
+ # 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])
+ decomp=decomp+decompressor.flush()
+
+ print 'Progressive compression (level 9):'
+ print ' Original:', len(s), 'Compressed:', len(comptext),
+ print 'Uncompressed:', len(decomp)
+
+if __name__ == '__main__':
+ main()