# 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.11. 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,
so install using a version of python appropriate for your source.
python-minifier runs with and can minify code written for Python 2.7 and Python 3.3 to 3.11.
## 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) 2020 Daniel Flook
Raw data
{
"_id": null,
"home_page": "https://github.com/dflook/python-minifier",
"name": "python-minifier",
"maintainer": "",
"docs_url": null,
"requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, <3.12",
"maintainer_email": "",
"keywords": "minify minifier",
"author": "Daniel Flook",
"author_email": "daniel@flook.org",
"download_url": "https://files.pythonhosted.org/packages/ec/f5/d4ea0e1fdf25e020237811d300f899c9cccbbeb7fb64200d44efc1c1606d/python_minifier-2.8.1.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.11. 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, \nso install using a version of python appropriate for your source.\n\npython-minifier runs with and can minify code written for Python 2.7 and Python 3.3 to 3.11.\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) 2020 Daniel Flook\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Transform Python source code into it's most compact representation",
"version": "2.8.1",
"split_keywords": [
"minify",
"minifier"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8de2268e6d39cb6c6c9f90be37cebcb87d46b082de064077921f7e5100bf57ff",
"md5": "7d2f15f4507dd68e65c9b159abcdf365",
"sha256": "2a228480e3690fb36aafa99892831b1b5479b44d2a4bc17736754a0378f06141"
},
"downloads": -1,
"filename": "python_minifier-2.8.1-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "7d2f15f4507dd68e65c9b159abcdf365",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, <3.12",
"size": 43155,
"upload_time": "2023-03-15T23:55:04",
"upload_time_iso_8601": "2023-03-15T23:55:04.994239Z",
"url": "https://files.pythonhosted.org/packages/8d/e2/268e6d39cb6c6c9f90be37cebcb87d46b082de064077921f7e5100bf57ff/python_minifier-2.8.1-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ecf5d4ea0e1fdf25e020237811d300f899c9cccbbeb7fb64200d44efc1c1606d",
"md5": "f3a661d153c1f52a5a04ea004703d0be",
"sha256": "a20e6c101c5b9eae943c52d2f175e3ce749b65870dcdac85904fc6f0720fc079"
},
"downloads": -1,
"filename": "python_minifier-2.8.1.tar.gz",
"has_sig": false,
"md5_digest": "f3a661d153c1f52a5a04ea004703d0be",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, <3.12",
"size": 43888,
"upload_time": "2023-03-15T23:55:06",
"upload_time_iso_8601": "2023-03-15T23:55:06.991460Z",
"url": "https://files.pythonhosted.org/packages/ec/f5/d4ea0e1fdf25e020237811d300f899c9cccbbeb7fb64200d44efc1c1606d/python_minifier-2.8.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-15 23:55:06",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "dflook",
"github_project": "python-minifier",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "python-minifier"
}