# Python Minifier
Transforms Python source code into its most compact representation.
[Try it out!](https://python-minifier.com)
python-minifier currently supports Python 2.7 and Python 3.3 to 3.13. Previous releases supported Python 2.6.
* [PyPI](https://pypi.org/project/python-minifier/)
* [Documentation](https://dflook.github.io/python-minifier/)
* [Issues](https://github.com/dflook/python-minifier/issues)
As an example, the following python source:
```python
def handler(event, context):
l.info(event)
try:
i_token = hashlib.new('md5', (event['RequestId'] + event['StackId']).encode()).hexdigest()
props = event['ResourceProperties']
if event['RequestType'] == 'Create':
event['PhysicalResourceId'] = 'None'
event['PhysicalResourceId'] = create_cert(props, i_token)
add_tags(event['PhysicalResourceId'], props)
validate(event['PhysicalResourceId'], props)
if wait_for_issuance(event['PhysicalResourceId'], context):
event['Status'] = 'SUCCESS'
return send(event)
else:
return reinvoke(event, context)
elif event['RequestType'] == 'Delete':
if event['PhysicalResourceId'] != 'None':
acm.delete_certificate(CertificateArn=event['PhysicalResourceId'])
event['Status'] = 'SUCCESS'
return send(event)
elif event['RequestType'] == 'Update':
if replace_cert(event):
event['PhysicalResourceId'] = create_cert(props, i_token)
add_tags(event['PhysicalResourceId'], props)
validate(event['PhysicalResourceId'], props)
if not wait_for_issuance(event['PhysicalResourceId'], context):
return reinvoke(event, context)
else:
if 'Tags' in event['OldResourceProperties']:
acm.remove_tags_from_certificate(CertificateArn=event['PhysicalResourceId'],
Tags=event['OldResourceProperties']['Tags'])
add_tags(event['PhysicalResourceId'], props)
event['Status'] = 'SUCCESS'
return send(event)
else:
raise RuntimeError('Unknown RequestType')
except Exception as ex:
l.exception('')
event['Status'] = 'FAILED'
event['Reason'] = str(ex)
return send(event)
```
Becomes:
```python
def handler(event,context):
L='OldResourceProperties';K='Tags';J='None';H='SUCCESS';G='RequestType';E='Status';D=context;B='PhysicalResourceId';A=event;l.info(A)
try:
F=hashlib.new('md5',(A['RequestId']+A['StackId']).encode()).hexdigest();C=A['ResourceProperties']
if A[G]=='Create':
A[B]=J;A[B]=create_cert(C,F);add_tags(A[B],C);validate(A[B],C)
if wait_for_issuance(A[B],D):A[E]=H;return send(A)
else:return reinvoke(A,D)
elif A[G]=='Delete':
if A[B]!=J:acm.delete_certificate(CertificateArn=A[B])
A[E]=H;return send(A)
elif A[G]=='Update':
if replace_cert(A):
A[B]=create_cert(C,F);add_tags(A[B],C);validate(A[B],C)
if not wait_for_issuance(A[B],D):return reinvoke(A,D)
else:
if K in A[L]:acm.remove_tags_from_certificate(CertificateArn=A[B],Tags=A[L][K])
add_tags(A[B],C)
A[E]=H;return send(A)
else:raise RuntimeError('Unknown RequestType')
except Exception as I:l.exception('');A[E]='FAILED';A['Reason']=str(I);return send(A)
```
## Why?
AWS Cloudformation templates may have AWS lambda function source code embedded in them, but only if the function is less
than 4KiB. I wrote this package so I could write python normally and still embed the module in a template.
## Installation
To install python-minifier use pip:
```bash
$ pip install python-minifier
```
Note that python-minifier depends on the python interpreter for parsing source code,
and outputs source code compatible with the version of the interpreter it is run with.
This means that if you minify code written for Python 3.11 using python-minifier running with Python 3.12,
the minified code may only run with Python 3.12.
python-minifier runs with and can minify code written for Python 2.7 and Python 3.3 to 3.13.
## Usage
To minify a source file, and write the minified module to stdout:
```bash
$ pyminify hello.py
```
There is also an API. The same example would look like:
```python
import python_minifier
with open('hello.py') as f:
print(python_minifier.minify(f.read()))
```
Documentation is available at [dflook.github.io/python-minifier/](https://dflook.github.io/python-minifier/)
## License
Available under the MIT License. Full text is in the [LICENSE](LICENSE) file.
Copyright (c) 2024 Daniel Flook
Raw data
{
"_id": null,
"home_page": "https://github.com/dflook/python-minifier",
"name": "python-minifier",
"maintainer": null,
"docs_url": null,
"requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,<3.14,>=2.7",
"maintainer_email": null,
"keywords": "minify minifier",
"author": "Daniel Flook",
"author_email": "daniel@flook.org",
"download_url": "https://files.pythonhosted.org/packages/df/e0/9521cc0b62f34ec720a88d1bfaf3c066be67739dab79ea2fb15d33e976d7/python_minifier-2.11.2.tar.gz",
"platform": null,
"description": "# Python Minifier\n\nTransforms Python source code into its most compact representation.\n\n[Try it out!](https://python-minifier.com)\n\npython-minifier currently supports Python 2.7 and Python 3.3 to 3.13. Previous releases supported Python 2.6.\n\n* [PyPI](https://pypi.org/project/python-minifier/)\n* [Documentation](https://dflook.github.io/python-minifier/)\n* [Issues](https://github.com/dflook/python-minifier/issues)\n\nAs an example, the following python source:\n\n```python\ndef handler(event, context):\n l.info(event)\n try:\n i_token = hashlib.new('md5', (event['RequestId'] + event['StackId']).encode()).hexdigest()\n props = event['ResourceProperties']\n\n if event['RequestType'] == 'Create':\n event['PhysicalResourceId'] = 'None'\n event['PhysicalResourceId'] = create_cert(props, i_token)\n add_tags(event['PhysicalResourceId'], props)\n validate(event['PhysicalResourceId'], props)\n\n if wait_for_issuance(event['PhysicalResourceId'], context):\n event['Status'] = 'SUCCESS'\n return send(event)\n else:\n return reinvoke(event, context)\n\n elif event['RequestType'] == 'Delete':\n if event['PhysicalResourceId'] != 'None':\n acm.delete_certificate(CertificateArn=event['PhysicalResourceId'])\n event['Status'] = 'SUCCESS'\n return send(event)\n\n elif event['RequestType'] == 'Update':\n\n if replace_cert(event):\n event['PhysicalResourceId'] = create_cert(props, i_token)\n add_tags(event['PhysicalResourceId'], props)\n validate(event['PhysicalResourceId'], props)\n\n if not wait_for_issuance(event['PhysicalResourceId'], context):\n return reinvoke(event, context)\n else:\n if 'Tags' in event['OldResourceProperties']:\n acm.remove_tags_from_certificate(CertificateArn=event['PhysicalResourceId'],\n Tags=event['OldResourceProperties']['Tags'])\n\n add_tags(event['PhysicalResourceId'], props)\n\n event['Status'] = 'SUCCESS'\n return send(event)\n else:\n raise RuntimeError('Unknown RequestType')\n\n except Exception as ex:\n l.exception('')\n event['Status'] = 'FAILED'\n event['Reason'] = str(ex)\n return send(event)\n```\n\nBecomes:\n\n```python\ndef handler(event,context):\n\tL='OldResourceProperties';K='Tags';J='None';H='SUCCESS';G='RequestType';E='Status';D=context;B='PhysicalResourceId';A=event;l.info(A)\n\ttry:\n\t\tF=hashlib.new('md5',(A['RequestId']+A['StackId']).encode()).hexdigest();C=A['ResourceProperties']\n\t\tif A[G]=='Create':\n\t\t\tA[B]=J;A[B]=create_cert(C,F);add_tags(A[B],C);validate(A[B],C)\n\t\t\tif wait_for_issuance(A[B],D):A[E]=H;return send(A)\n\t\t\telse:return reinvoke(A,D)\n\t\telif A[G]=='Delete':\n\t\t\tif A[B]!=J:acm.delete_certificate(CertificateArn=A[B])\n\t\t\tA[E]=H;return send(A)\n\t\telif A[G]=='Update':\n\t\t\tif replace_cert(A):\n\t\t\t\tA[B]=create_cert(C,F);add_tags(A[B],C);validate(A[B],C)\n\t\t\t\tif not wait_for_issuance(A[B],D):return reinvoke(A,D)\n\t\t\telse:\n\t\t\t\tif K in A[L]:acm.remove_tags_from_certificate(CertificateArn=A[B],Tags=A[L][K])\n\t\t\t\tadd_tags(A[B],C)\n\t\t\tA[E]=H;return send(A)\n\t\telse:raise RuntimeError('Unknown RequestType')\n\texcept Exception as I:l.exception('');A[E]='FAILED';A['Reason']=str(I);return send(A)\n```\n\n## Why?\n\nAWS Cloudformation templates may have AWS lambda function source code embedded in them, but only if the function is less \nthan 4KiB. I wrote this package so I could write python normally and still embed the module in a template.\n\n## Installation\n\nTo install python-minifier use pip:\n\n```bash\n$ pip install python-minifier\n```\n\nNote that python-minifier depends on the python interpreter for parsing source code,\nand outputs source code compatible with the version of the interpreter it is run with.\n\nThis means that if you minify code written for Python 3.11 using python-minifier running with Python 3.12,\nthe minified code may only run with Python 3.12.\n\npython-minifier runs with and can minify code written for Python 2.7 and Python 3.3 to 3.13.\n\n## Usage\n\nTo minify a source file, and write the minified module to stdout:\n\n```bash\n$ pyminify hello.py\n```\n\nThere is also an API. The same example would look like:\n\n```python\nimport python_minifier\n\nwith open('hello.py') as f:\n print(python_minifier.minify(f.read()))\n```\n\nDocumentation is available at [dflook.github.io/python-minifier/](https://dflook.github.io/python-minifier/)\n\n## License\n\nAvailable under the MIT License. Full text is in the [LICENSE](LICENSE) file.\n\nCopyright (c) 2024 Daniel Flook\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Transform Python source code into it's most compact representation",
"version": "2.11.2",
"project_urls": {
"Changelog": "https://github.com/dflook/python-minifier/blob/main/CHANGELOG.md",
"Documentation": "https://dflook.github.io/python-minifier/",
"Homepage": "https://github.com/dflook/python-minifier",
"Issues": "https://github.com/dflook/python-minifier/issues"
},
"split_keywords": [
"minify",
"minifier"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "02693bfe65d5c1433c53046fd511e8b0030f8d68b4b715164c646211f36ba5a2",
"md5": "181c58c2cddb14d46bfb79ec0d2b3858",
"sha256": "0411a2272e20823c78d71dc80290136833afd933199de5528a0fc08809788cd9"
},
"downloads": -1,
"filename": "python_minifier-2.11.2-py2-none-any.whl",
"has_sig": false,
"md5_digest": "181c58c2cddb14d46bfb79ec0d2b3858",
"packagetype": "bdist_wheel",
"python_version": "py2",
"requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,<3.14,>=2.7",
"size": 55445,
"upload_time": "2024-10-03T21:48:56",
"upload_time_iso_8601": "2024-10-03T21:48:56.886296Z",
"url": "https://files.pythonhosted.org/packages/02/69/3bfe65d5c1433c53046fd511e8b0030f8d68b4b715164c646211f36ba5a2/python_minifier-2.11.2-py2-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "daf2b4f197ef536df9bbd9fa1b131023e405d21847bd0c75e0caf62adeeaadf8",
"md5": "30ab24b7bdea575baf9b2d7136e0c166",
"sha256": "6c1f215b733025de9c8832f438f9f0c3c80497420765d4946ee136a934637f49"
},
"downloads": -1,
"filename": "python_minifier-2.11.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "30ab24b7bdea575baf9b2d7136e0c166",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,<3.14,>=2.7",
"size": 55113,
"upload_time": "2024-10-03T21:48:57",
"upload_time_iso_8601": "2024-10-03T21:48:57.904811Z",
"url": "https://files.pythonhosted.org/packages/da/f2/b4f197ef536df9bbd9fa1b131023e405d21847bd0c75e0caf62adeeaadf8/python_minifier-2.11.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "dfe09521cc0b62f34ec720a88d1bfaf3c066be67739dab79ea2fb15d33e976d7",
"md5": "395bda5121b4945eeae5fe50018145ca",
"sha256": "d933f48e3b00ab35816a7142e2c9f8130cf849500f8b968cff3f13aaaecc677b"
},
"downloads": -1,
"filename": "python_minifier-2.11.2.tar.gz",
"has_sig": false,
"md5_digest": "395bda5121b4945eeae5fe50018145ca",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,<3.14,>=2.7",
"size": 63333,
"upload_time": "2024-10-03T21:48:59",
"upload_time_iso_8601": "2024-10-03T21:48:59.480633Z",
"url": "https://files.pythonhosted.org/packages/df/e0/9521cc0b62f34ec720a88d1bfaf3c066be67739dab79ea2fb15d33e976d7/python_minifier-2.11.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-03 21:48:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dflook",
"github_project": "python-minifier",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "python-minifier"
}