pythonminify

Is there an equivalent to the Google Closure Compiler for Python?


I'm wondering if there is something that will scan through your code and remove any unnecessary functions.

Maybe the Python compiler already does this when it makes .pyc? It'd be nice if there was something that did this at the .py level too.


Solution

  • No, there's not such a software, because it would be extremely difficult to introspectively understand all the possible branches the code could execute based on different inputs (if you consider metaprogramming, things would become even more complicated).

    What you can do is to extrapolate how much / which code has (not) been executed during a test-run, and then evaluate yourself if such code can be safely removed. What you want to look for is some sort of code coverage utility.

    In python two well known ones are figleaf and coverage.

    EDIT: another approach could be that of using a profiler (and probably more specifically a call graph like this one) to check that all your functions and classes are appearing in the graph itself (and then manually checking if those not appearing...)