python-minifier


Namepython-minifier JSON
Version 2.11.3 PyPI version JSON
download
home_pagehttps://github.com/dflook/python-minifier
SummaryTransform Python source code into it's most compact representation
upload_time2024-11-12 16:14:46
maintainerNone
docs_urlNone
authorDaniel Flook
requires_python!=3.0.*,!=3.1.*,!=3.2.*,<3.14,>=2.7
licenseMIT
keywords minify minifier
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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/39/63/403fb2d6394b3e455e046d91f64b96072803aaf119027a26e716ed94d63c/python_minifier-2.11.3.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.3",
    "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": "01e57b3fa13c38dcc7cddb86df0da039bb0704c9cd6b326c0c3aa78ae6c31c93",
                "md5": "aac34347e8ff9263fb50c0ae24c2b7fc",
                "sha256": "75bf018e48780277ab0d40a983c013224af89ecc1e5bd59054de1638f6360944"
            },
            "downloads": -1,
            "filename": "python_minifier-2.11.3-py2-none-any.whl",
            "has_sig": false,
            "md5_digest": "aac34347e8ff9263fb50c0ae24c2b7fc",
            "packagetype": "bdist_wheel",
            "python_version": "py2",
            "requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,<3.14,>=2.7",
            "size": 56522,
            "upload_time": "2024-11-12T16:14:42",
            "upload_time_iso_8601": "2024-11-12T16:14:42.804404Z",
            "url": "https://files.pythonhosted.org/packages/01/e5/7b3fa13c38dcc7cddb86df0da039bb0704c9cd6b326c0c3aa78ae6c31c93/python_minifier-2.11.3-py2-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "533261d20860d18afb81cb7258bb02d4eaf4b09170383c2374514f6aef384fa9",
                "md5": "14b890086639483087f423e3c9d3a1c7",
                "sha256": "37e10e9e318be701eecb48764942426be73ae9f562d75bea4e29c5f66945ce97"
            },
            "downloads": -1,
            "filename": "python_minifier-2.11.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "14b890086639483087f423e3c9d3a1c7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,<3.14,>=2.7",
            "size": 56172,
            "upload_time": "2024-11-12T16:14:44",
            "upload_time_iso_8601": "2024-11-12T16:14:44.536643Z",
            "url": "https://files.pythonhosted.org/packages/53/32/61d20860d18afb81cb7258bb02d4eaf4b09170383c2374514f6aef384fa9/python_minifier-2.11.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3963403fb2d6394b3e455e046d91f64b96072803aaf119027a26e716ed94d63c",
                "md5": "47f70ee31d1fa987ce8ad707810951f3",
                "sha256": "489133b91212ec9658a7b64d243eb9eb67d7e53faf2ac5166a33301c61b3dcab"
            },
            "downloads": -1,
            "filename": "python_minifier-2.11.3.tar.gz",
            "has_sig": false,
            "md5_digest": "47f70ee31d1fa987ce8ad707810951f3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "!=3.0.*,!=3.1.*,!=3.2.*,<3.14,>=2.7",
            "size": 64438,
            "upload_time": "2024-11-12T16:14:46",
            "upload_time_iso_8601": "2024-11-12T16:14:46.269829Z",
            "url": "https://files.pythonhosted.org/packages/39/63/403fb2d6394b3e455e046d91f64b96072803aaf119027a26e716ed94d63c/python_minifier-2.11.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-12 16:14:46",
    "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"
}
        
Elapsed time: 0.42863s