patternpythonMinor
CGI output gzip compression module
Viewed 0 times
cgicompressionmoduleoutputgzip
Problem
Edit: How should I interpret the silence? On a scale from 0 to 10 where 0 means "Bloody awful" and 10 means "Nothing to complain about".
I'm mainly concerned about readability and things I don't seem to be aware of.
If the code is readable, I won't need to explain it.
``
crashes, you should use a browser that d
I'm mainly concerned about readability and things I don't seem to be aware of.
If the code is readable, I won't need to explain it.
``
#!/usr/bin/python
import os
import sys
import subprocess
assert __name__ != '__main__'
max_load_avg1 = 3.5
'''
compressout
===========
Simple CGI output module.
Uses gzip compression on the output stream if the client accepts it.
NOTICE: The cgitb module will write to stdout if the script crashes,
you should use a browser that does not accept gzip, when you are
testing your scripts.
NOTICE: In the beginning of this file max_load_avg1 is defined.
This is the maximum allowed load average under one minute.
If the one minute load average exceeds this value, compressout will
abort.
Functions
=========
init(write_headers=True)
------------------------
Initialize the module. This function will detect if the client
supports gzip.
If write_headers, write a 'Vary' and (if used)
'Content-Encdoing' header.
write_h(s)
----------
Write part of header.
Write s to standard output, will never go through gzip.
write_b(s)
----------
Write part of body.
gzip is supported by the client
-------------------------------
s will be appended to a local buffer
which done will compress and print.
gzip is not supported
---------------------
s will go straight to stdout.
done()
------
Done writing output.
This function will invoke gzip.
Dos and don'ts
==============
## ## ##
if __name__ == '__main__':
compressout.init()
main()
compressout.done()
## ## ##
* Never call write_h after any call to write_b
* Always call done when your done.
* Use only compressout to write output
* NOTICE: The cgitb` module will write to stdout if the scriptcrashes, you should use a browser that d
Solution
A few things I've noticed from a quick skim:
- The assert is kinda useless.
write_handwrite_b?? Documentation isn't an excuse for giving your functions useless names. Name themwrite_headerandwrite_bodyor something.
- Globals? Just no. Especially when they're defined AFTER the functions. Took me like 5 minutes to find them.
- You've copied
if os.getloadavg()[0] > max_load_avg1: os.abort()tons of times. Might be a good idea to export them to a function.
use_gzip = True or False. ?????????????????????? What ????????????????????????
Context
StackExchange Code Review Q#157158, answer score: 2
Revisions (0)
No revisions yet.