Python comment stripper

Alex Martelli aleax at aleax.it
Wed Sep 18 11:37:22 EDT 2002


Dennis Reinhardt wrote:

>> This doesn't seem like a good reason to me.  Why not just use .pyc files
>> all the time and be done with it?
> 
> I am looking to package Python programs as EXE files for upload/download
> over the internet.  The "good reason" is faster upload/download over
> dialup lines, not hard disk space.

Makes sense.  Let's see, what about a one-(logical)-liner (warning, 
untested!):

open(outfile, 'w').writelines( [ 
    tok[1] for tok in tokenize.generate_tokens(open(infile).readline) 
           if tok[0] != tokenize.COMMENT ] )

Probably best rewritten as a loop of a few lines, but, I think the
key idea holds -- tokenize.generate_tokens seems to be the best way.


Module gzip is probably a good idea to help you reduce size, too.
I.e., besides stripping comments, you might also use gzip for
compression -- perhaps not quite as good as the compression you
might get from bzip2, but the latter is not part of the standard
Python distribution.  What are you using now for compression?


Alex




More information about the Python-list mailing list