Transform Python source code into it's most compact representation
Project description
Python Minifier
Transforms Python source code into its most compact representation.
python-minifier currently supports Python 2.7 and Python 3.3 to 3.13. Previous releases supported Python 2.6.
As an example, the following python source:
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:
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:
$ 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:
$ pyminify hello.py
There is also an API. The same example would look like:
import python_minifier
with open('hello.py') as f:
print(python_minifier.minify(f.read()))
Documentation is available at dflook.github.io/python-minifier/
License
Available under the MIT License. Full text is in the LICENSE file.
Copyright (c) 2024 Daniel Flook
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file python_minifier-2.11.3.tar.gz
.
File metadata
- Download URL: python_minifier-2.11.3.tar.gz
- Upload date:
- Size: 64.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 489133b91212ec9658a7b64d243eb9eb67d7e53faf2ac5166a33301c61b3dcab |
|
MD5 | 47f70ee31d1fa987ce8ad707810951f3 |
|
BLAKE2b-256 | 3963403fb2d6394b3e455e046d91f64b96072803aaf119027a26e716ed94d63c |
File details
Details for the file python_minifier-2.11.3-py3-none-any.whl
.
File metadata
- Download URL: python_minifier-2.11.3-py3-none-any.whl
- Upload date:
- Size: 56.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37e10e9e318be701eecb48764942426be73ae9f562d75bea4e29c5f66945ce97 |
|
MD5 | 14b890086639483087f423e3c9d3a1c7 |
|
BLAKE2b-256 | 533261d20860d18afb81cb7258bb02d4eaf4b09170383c2374514f6aef384fa9 |
File details
Details for the file python_minifier-2.11.3-py2-none-any.whl
.
File metadata
- Download URL: python_minifier-2.11.3-py2-none-any.whl
- Upload date:
- Size: 56.5 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75bf018e48780277ab0d40a983c013224af89ecc1e5bd59054de1638f6360944 |
|
MD5 | aac34347e8ff9263fb50c0ae24c2b7fc |
|
BLAKE2b-256 | 01e57b3fa13c38dcc7cddb86df0da039bb0704c9cd6b326c0c3aa78ae6c31c93 |