How can I strip comments and doc strings from pyth

2019-03-28 18:04发布

Is there a program which I can run like this:

py2py.py < orig.py > smaller.py

Where orig.py contains python source code with comments and doc strings, and smaller.py contains identical, runnable source code but without the comments and doc strings?

Code which originally looked like this:

#/usr/bin/python
"""Do something
blah blah...
"""

# Beware the frubnitz!
def foo(it):
    """Foo it!"""
    print it  # hmm?

Would then look like this:

def foo(it):
    print it

2条回答
Fickle 薄情
2楼-- · 2019-03-28 18:56

This Python minifier looks like it does what you need.

查看更多
Summer. ? 凉城
3楼-- · 2019-03-28 18:58

I recommend minipy. The most compelling reason is that it does proper analysis of the source code abstract syntax tree so the minified code is much more accurate. I've found that the more well known pyminifier tends to generate code with undefined symbol errors, misinterpreted tuples, etc. I also got a few percent better compression results with minipy. A minor benefit of minipy is that it's less than half the code size of pyminifier. It's also easier to manage and integrate into a build pipeline because it's a single standalone python file.

查看更多
登录 后发表回答