Skip to main content

Minimizes Python code using Python's lexical scanning tokenize module.

Project description

python-minimizer

Minimizes Python code using Python's lexical scanning tokenize module.

Get it on pip

 pip install python-minimizer 

Command-line usage

Currently python-minimizer supports the following options:

usage: minimizer.py [-h] [-o OUT_PATH] [-b] [-c] [-d] [-s]
                    [-w WHITESPACE_CHAR] [-i INDENT_CHAR] [-r] [-v]
                    in_path

Minimizes Python code using Python's lexical scanning tokenize module.

positional arguments:
  in_path               The file to minimize

optional arguments:
  -h, --help            show this help message and exit
  -o OUT_PATH, --out-path OUT_PATH
                        When specified, minimizer will output to the path
                        instead of stdout
  -b, --keep-blank-lines
                        When set, minimizer will not remove blank lines.
  -c, --keep-comments   When set, minimizer will not remove comment lines and
                        inline comments
  -d, --keep-docstrings
                        When set, minimizer will not remove docstrings
  -s, --keep-whitespace
                        When set, minimizer will not remove extraneous
                        whitespace
  -w WHITESPACE_CHAR, --whitespace-char WHITESPACE_CHAR
                        Set the whitespace character to use. Defaults to space
                        (" ")
  -i INDENT_CHAR, --indent-char INDENT_CHAR
                        Set the indentation character to use. Defaults to tab
                        ("\t")
  -r, --recursive       Treat the in-path and --out-path as directories to
                        minimize recursively
  -v, --verbose         Explain what we are doing as we do it, higher levels
                        are useful for debugging

By default, the minimizer removes blank lines, comments, docstrings, and
extraneous whitespace. Where needed, it will insert a space (" ") for
whitespace between operators and use a tab ("\t") for indentation. Use the
command line switches to change any of the defaults.

Library usage

Of course, you can also import the minimizer module and use it as follows:

from minimizer import minimize
with open(code_file, 'r') as f:
    code = f.read()
minimized_code = minimize(code)
with open(minimized_file, 'w') as f:
    f.write(minimized_code)

By default, the minimize function will remove blank lines, comments, docstrings, and whitespace between operators and uses a space (" ") for the whitespace character and a tab ("\t") for the indent character, but accepts keyword arguments to change these options.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-minimizer-2.0.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

python_minimizer-2.0.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file python-minimizer-2.0.1.tar.gz.

File metadata

  • Download URL: python-minimizer-2.0.1.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.2.0 requests-toolbelt/0.8.0 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for python-minimizer-2.0.1.tar.gz
Algorithm Hash digest
SHA256 74aa842865cb821bfe3b2bcaf3c85a917c3234f98621fcdb7814fe2b5b42c639
MD5 e2325140840dfa62174f540629967a59
BLAKE2b-256 18c0883d5e7a44b2ddd7b98a2f2e140faaaf4f384d61b8bf91c5bb1ff8e5f2f5

See more details on using hashes here.

File details

Details for the file python_minimizer-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: python_minimizer-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.2.0 requests-toolbelt/0.8.0 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for python_minimizer-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a9f2b42e650c255e1d474f29aaff2afc0d179f88553ecbcbc19b26cde7e563fd
MD5 b4155b791b1dfcbdecdcee660ff9c27b
BLAKE2b-256 618453a5e239b4d44721eaa38f86d7ae2dc6b1f652d86a6569e23860d6923de5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page