Git Product home page Git Product logo

python-minimizer's Introduction

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.

python-minimizer's People

Contributors

agroden avatar mm10ws avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

python-minimizer's Issues

2.0.1 fails install on Ubuntu 20.04

I tried installing 2.0.1 via pip3, I get an error and end up with 2.0.0 installed.

Output:

Collecting python-minimizer
    Using cached python-minimizer-2.0.1.tar.gz (6.5 kB)
    Installing build dependencies ... done
    Getting requirements to build wheel ... done
      Preparing wheel metadata ... error
      ERROR: Command errored out with exit status 1:
       command: [redacted]/vcs/git/test-minimizer/venv/bin/python3 [redacted]/vcs/git/test-minimizer/venv/lib/python3.8/site-packages/pip/_vendor/pep517/in_process/_in_process.py prepare_metadata_for_build_wheel /tmp/tmpwxc1txni
           cwd: /tmp/pip-install-_o3vpo7e/python-minimizer_dcbca966d3ea4ee1a2f2df2681149475
      Complete output (16 lines):
      Traceback (most recent call last):
        File "[redacted]/vcs/git/test-minimizer/venv/lib/python3.8/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 280, in <module>
          main()
        File "[redacted]/vcs/git/test-minimizer/venv/lib/python3.8/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 263, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "[redacted]/vcs/git/test-minimizer/venv/lib/python3.8/site-packages/pip/_vendor/pep517/in_process/_in_process.py", line 133, in prepare_metadata_for_build_wheel
          return hook(metadata_directory, config_settings)
        File "/tmp/pip-build-env-sjlvgw0n/overlay/lib/python3.8/site-packages/poetry/core/masonry/api.py", line 44, in prepare_metadata_for_build_wheel
          builder = WheelBuilder(poetry)
        File "/tmp/pip-build-env-sjlvgw0n/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 57, in __init__
          super(WheelBuilder, self).__init__(poetry, executable=executable)
        File "/tmp/pip-build-env-sjlvgw0n/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/builder.py", line 85, in __init__
          self._module = Module(
        File "/tmp/pip-build-env-sjlvgw0n/overlay/lib/python3.8/site-packages/poetry/core/masonry/utils/module.py", line 63, in __init__
          raise ModuleOrPackageNotFound(
      poetry.core.masonry.utils.module.ModuleOrPackageNotFound: No file/folder found for package python-minimizer
      ----------------------------------------
  WARNING: Discarding https://files.pythonhosted.org/packages/18/c0/883d5e7a44b2ddd7b98a2f2e140faaaf4f384d61b8bf91c5bb1ff8e5f2f5/python-minimizer-2.0.1.tar.gz#sha256=74aa842865cb821bfe3b2bcaf3c85a917c3234f98621fcdb7814fe2b5b42c639 (from https://pypi.org/simple/python-minimizer/). Command errored out with exit status 1: [redacted]/vcs/git/test-minimizer/venv/bin/python3 [redacted]/vcs/git/test-minimizer/venv/lib/python3.8/site-packages/pip/_vendor/pep517/in_process/_in_process.py prepare_metadata_for_build_wheel /tmp/tmpwxc1txni Check the logs for full command output.
    Using cached python_minimizer-2.0.0-py3-none-any.whl (6.8 kB)
  Installing collected packages: python-minimizer
  Successfully installed python-minimizer-2.0.0

And here is the output of pip freeze:

$ pip freeze
pkg-resources==0.0.0
python-minimizer==2.0.0

Sorry to be a pest & thanks in advance

Verbose checks fail when using minimizer as a module

Looks like this may be Python 3.6 only. I tried it with Python 3.8, and the imports worked.

There is another error, however.
File [redacted]/venv/lib/python3.8/site-packages/minimizer.py", line 214, in minimize
grps = group_tokens(sbuf)
File "[redacted]/venv/lib/python3.8/site-packages/minimizer.py", line 139, in group_tokens
if verbose > 1:
NameError: name 'verbose' is not defined

Looks like global verbose is not being seen.

Originally posted by @another2020githubuser in #9 (comment)

CI / tests / release pipeline

Generate a pipeline to make this more official with CI, testing, and a release pipeline to give greater operational assurance to users.

Minimizer can't import COMMENT and NL from token

Hi, it looks like minimizer needs updating for Python 3. From what I can see, COMMENT and NL have been removed from token.h in at least Python 3.6

When I run
(venv)$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

from minimizer import minimize
Traceback (most recent call last):
File "", line 1, in
File "[redacted]/minimizer.py", line 10, in
from token import (
ImportError: cannot import name 'COMMENT'

If I modify minimizer.py as follows, the import succeeds, but crashes soon after as COMMENT and NL symbols are not defined.

from token import (
#COMMENT, <-- this import fails
DEDENT,
ENDMARKER,
INDENT,
NEWLINE,
#NL, <-- this import fails
NAME,
NUMBER,
OP,
STRING,
tok_name
)

Hopefully this an easy fix. I'd like to use this in a project I'm working on.

Best regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.