dflook / python-minifier

Transform Python source code into its most compact representation
MIT License
553 stars 40 forks source link

Suggestion: Removable blocks #62

Closed thomasahle closed 1 year ago

thomasahle commented 1 year ago

Thank you for this amazing library!

One thing I often find myself doing is commenting out debug code, or other non-essentials, before minifying. I'm wondering if it would make sense to add some kind of "remove all of this during minify" blocks, which would be ignored when running the program normally, but deleted by minifier. For example:

x = 10
#minify-remove-start
if debug:
    print(f"Debug: {x=}")
#minify-remove-end
print(x**2)

Thanks again for this amazing work. I'm using it to greatly compress sunfish (https://github.com/thomasahle/sunfish) for the sub 4K chess engine competition :)

dflook commented 1 year ago

Hi @thomasahle, this is a good idea. I think this should hook into the builtin __debug__ constant, e.g.:

x = 10
if __debug__:
    print(f"Debug: {x=}")
print(x**2)

And that whole block would be removed if a --remove-debug option was set.

dflook commented 1 year ago

Version 2.8.0 has been released which includes --remove-debug