compression.py - For compressing Python code

compression.py - A module providing functions to turn a python script into a self-executing archive in a few different formats...

gz_pack format:

  • Typically provides better compression than bzip2 (for Python scripts).
  • Scripts compressed via this method can still be imported as modules.
  • The resulting binary data is base64-encoded which isn’t optimal compression.

bz2_pack format:

  • In some cases may provide better compression than gzip.
  • Scripts compressed via this method can still be imported as modules.
  • The resulting binary data is base64-encoded which isn’t optimal compression.

lzma_pack format:

  • In some cases may provide better compression than bzip2.
  • Scripts compressed via this method can still be imported as modules.
  • The resulting binary data is base64-encoded which isn’t optimal compression.

The gz_pack, bz2_pack, and lzma_pack formats only work on individual .py files. To pack a number of files at once using this method use the --destdir command line option:

zip_pack format:

  • Provides the best compression of Python scripts.
  • Resulting script cannot be imported as a module.
  • Any required modules that are local (implied path) will be automatically included in the archive.
pyminifier.compression.bz2_pack(source)[source]

Returns ‘source’ as a bzip2-compressed, self-extracting python script.

Note

This method uses up more space than the zip_pack method but it has the advantage in that the resulting .py file can still be imported into a python program.

pyminifier.compression.gz_pack(source)[source]

Returns ‘source’ as a gzip-compressed, self-extracting python script.

Note

This method uses up more space than the zip_pack method but it has the advantage in that the resulting .py file can still be imported into a python program.

pyminifier.compression.lzma_pack(source)[source]

Returns ‘source’ as a lzma-compressed, self-extracting python script.

Note

This method uses up more space than the zip_pack method but it has the advantage in that the resulting .py file can still be imported into a python program.

pyminifier.compression.prepend(line, path)[source]

Appends line to the _beginning_ of the file at the given path.

If line doesn’t end in a newline one will be appended to the end of it.

pyminifier.compression.zip_pack(filepath, options)[source]

Creates a zip archive containing the script at filepath along with all imported modules that are local to filepath as a self-extracting python script. A shebang will be appended to the beginning of the resulting zip archive which will allow it to

If being run inside Python 3 and the lzma module is available the resulting ‘pyz’ file will use ZIP_LZMA compression to maximize compression.

options is expected to be the the same options parsed from pyminifier.py on the command line.

Note

  • The file resulting from this method cannot be imported as a module into another python program (command line execution only).
  • Any required local (implied path) modules will be automatically included (well, it does its best).
  • The result will be saved as a .pyz file (which is an extension I invented for this format).