开发者

How can I strip comments and doc strings from python source code? [closed]

开发者 https://www.devze.com 2022-12-09 15:35 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
开发者_Python百科

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 4 years ago.

Improve this question

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


This Python minifier looks like it does what you need.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消