Skip to main content

Transform Python source code into it's most compact representation

Project description

Python Minifier

Transforms Python source code into it's most compact representation.

Try it out!

python-minifier supports Python 2.6 to 2.7 and Python 3.3 to 3.9.

As an example, the following python source:

def handler(event, context):
        i_token ='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)
                return reinvoke(event, context)

        elif event['RequestType'] == 'Delete':
            if event['PhysicalResourceId'] != 'None':
            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)
                if 'Tags' in event['OldResourceProperties']:

                add_tags(event['PhysicalResourceId'], props)

            event['Status'] = 'SUCCESS'
            return send(event)
            raise RuntimeError('Unknown RequestType')

    except Exception as ex:
        event['Status'] = 'FAILED'
        event['Reason'] = str(ex)
        return send(event)


def handler(event,context):
		if A[G]=='Create':
			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):
				if not wait_for_issuance(A[B],D):return reinvoke(A,D)
				if K in A[L]:acm.remove_tags_from_certificate(CertificateArn=A[B],Tags=A[L][K])
			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)


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.


To install python-minifier use pip:

$ 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.6 to 2.7 and Python 3.3 to 3.9.


To minify a source file, and write the minified module to stdout:

$ pyminify

There is also an API. The same example would look like:

import python_minifier

with open('') as f:

Documentation is available at


Available under the MIT License. Full text is in the LICENSE file.

Copyright (c) 2020 Daniel Flook

Download files

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

Files for python-minifier, version 2.4.1
Filename, size File type Python version Upload date Hashes
Filename, size python_minifier-2.4.1-py3-none-any.whl (37.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size python_minifier-2.4.1.tar.gz (34.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page