|
| 1 | +# Python Minifier |
| 2 | + |
| 3 | +Transforms Python source code into it's most compact representation. |
| 4 | + |
| 5 | +python-minifier supports Python 2.6 to 2.7 and Python 3.3 to 3.5.. |
| 6 | + |
| 7 | +As an example, the following python source: |
| 8 | + |
| 9 | +```python |
| 10 | +import ast |
| 11 | + |
| 12 | +from python_minifier.miniprinter import MiniPrinter |
| 13 | +from python_minifier.ast_compare import AstCompare |
| 14 | + |
| 15 | +class UnstableMinification(Exception): |
| 16 | + def __init__(self, original, minified, exception): |
| 17 | + self.original = original |
| 18 | + self.minified = minified |
| 19 | + self.exception = exception |
| 20 | + |
| 21 | + def __str__(self): |
| 22 | + return str(self.exception) |
| 23 | + |
| 24 | +def minify(source): |
| 25 | + |
| 26 | + code = ast.parse(source) |
| 27 | + minifier = MiniPrinter() |
| 28 | + |
| 29 | + minifier.visit(code) |
| 30 | + |
| 31 | + try: |
| 32 | + # Check that the minified code is identical to the original |
| 33 | + minified_code = ast.parse(minifier.code) |
| 34 | + comparer = AstCompare() |
| 35 | + comparer.compare(code, minified_code) |
| 36 | + except Exception as exception: |
| 37 | + raise UnstableMinification(source, minifier.code, exception) |
| 38 | + |
| 39 | + return minifier.code |
| 40 | +``` |
| 41 | + |
| 42 | +Becomes: |
| 43 | + |
| 44 | +```python |
| 45 | +import ast |
| 46 | +from python_minifier.miniprinter import MiniPrinter |
| 47 | +from python_minifier.ast_compare import AstCompare |
| 48 | +class UnstableMinification(Exception): |
| 49 | + def __init__(self,original,minified,exception): |
| 50 | + self.original=original;self.minified=minified;self.exception=exception |
| 51 | + def __str__(self):return str(self.exception) |
| 52 | +def minify(source): |
| 53 | + code=ast.parse(source);minifier=MiniPrinter();minifier.visit(code) |
| 54 | + try: |
| 55 | + minified_code=ast.parse(minifier.code);comparer=AstCompare();comparer.compare(code,minified_code) |
| 56 | + except Exception as exception: |
| 57 | + raise UnstableMinification(source,minifier.code,exception) |
| 58 | + return minifier.code |
| 59 | +``` |
| 60 | + |
| 61 | +## Why? |
| 62 | + |
| 63 | +AWS Cloudformation templates may have AWS lambda function source code embedded in them, but only if the function is less |
| 64 | +than 4KiB. I wrote this package so I could write python normally and still embed the module in a template. |
| 65 | + |
| 66 | +## Installation |
| 67 | + |
| 68 | +To install python-minifier use pip: |
| 69 | + |
| 70 | +```bash |
| 71 | +$ pip install python-minifier |
| 72 | +``` |
| 73 | + |
| 74 | +Note that python-minifier depends on the python interpreter for parsing source code, |
| 75 | +so install using a version of python appropriate for your source. |
| 76 | + |
| 77 | +python-minifier runs with and can minify code written for Python 2.6 to 2.7 and Python 3.3 to 3.5. |
| 78 | + |
| 79 | +## Usage |
| 80 | + |
| 81 | +To minify a source file, and write the minified module to stdout: |
| 82 | + |
| 83 | +```bash |
| 84 | +$ pyminify hello.py |
| 85 | +``` |
| 86 | + |
| 87 | +There is also an API. The same example would look like: |
| 88 | + |
| 89 | +```python |
| 90 | +import python_minifier |
| 91 | + |
| 92 | +with open('hello.py') as f: |
| 93 | + print(python_minifier.minify(f.read())) |
| 94 | +``` |
| 95 | + |
| 96 | +## License |
| 97 | + |
| 98 | +Available under the MIT License. Full text is in the [LICENSE](LICENSE) file. |
| 99 | + |
| 100 | +Copyright 2018 Daniel Flook |
0 commit comments