Skip to main content

Python Minifier

Transforms Python source code into its most compact representation.

Try it out!

python-minifier currently supports Python 2.7 and Python 3.3 to 3.14. 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.14.

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_minifier-3.3.0.tar.gz (92.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

python_minifier-3.3.0-py3-none-any.whl (66.1 kB view details)

Uploaded Python 3

python_minifier-3.3.0-py2-none-any.whl (66.4 kB view details)

Uploaded Python 2

File details

Details for the file python_minifier-3.3.0.tar.gz.

File metadata

  • Download URL: python_minifier-3.3.0.tar.gz
  • Upload date:
  • Size: 92.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for python_minifier-3.3.0.tar.gz
Algorithm Hash digest
SHA256 5928489ed95813e519755390ee0cf2a42f02b18da936e76b9fc13046dbe9769b
MD5 946c356eb740d12b881d9929ba7cb90d
BLAKE2b-256 6d69686a2ed97f30c5baa2c7fb6e56a123cf076ed662028a2af8a65fe7e6c7d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_minifier-3.3.0.tar.gz:

Publisher: release.yaml on dflook/python-minifier

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_minifier-3.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_minifier-3.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b29e57315e346eaeb96e4548b7dd5919d9a6cfa6bd07d47f640d1b42100b8a37
MD5 99026fe7821250ce96459fdbcc70bdeb
BLAKE2b-256 a40438079a027da85f7c4b62602b469df53cb89268d7693eec1031c9b74ad8fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_minifier-3.3.0-py3-none-any.whl:

Publisher: release.yaml on dflook/python-minifier

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_minifier-3.3.0-py2-none-any.whl.

File metadata

File hashes

Hashes for python_minifier-3.3.0-py2-none-any.whl
Algorithm Hash digest
SHA256 aee7a6246d9d3e545e63a768007c588484a300d6c75124afad27237986ff15ab
MD5 9a83917f21fbff7a70b83568b3c84afa
BLAKE2b-256 7e4d2c05f8e0ffa4953ac23b8e5922cc09f2c6157f8df1c8c595d20819f254da

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_minifier-3.3.0-py2-none-any.whl:

Publisher: release.yaml on dflook/python-minifier

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

3.3.0 This release

3 files

3.2.0

3 files

3.1.1

3 files

3.1.0

3 files

3.0.0

3 files

2.11.3

3 files

2.11.2

3 files

2.11.1

2 files

2.11.0

2 files

2.10.0

2 files

2.9.0

2 files

2.8.1

2 files

2.8.0

2 files

2.7.0

2 files

2.6.0

2 files

2.5.0

2 files

2.4.2

2 files

2.4.1

2 files

2.4.0

2 files

2.3.2

2 files

2.3.1

2 files

2.3.0

2 files

2.2.1

2 files

2.1.2

2 files

2.1.1

2 files

2.1.0

2 files

2.0.0

2 files

1.1.0

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page