Package: python-minifier
Python MinifierTransforms Python source code into its most compact representation.Try it out!python-minifier currently supports Python 2.7 and Python 3.3 to 3.11. Previous releases supported Python 2.6.PyPIDocumentationIssuesAs an example, the following python source:defhandler(event,context):l.info(event)try:i_token=hashlib.new('md5',(event['RequestId']+event['StackId']).encode()).hexdigest()props=event['ResourceProperties']ifevent['RequestType']=='Create':event['PhysicalResourceId']='None'event['PhysicalResourceId']=create_cert(props,i_token)add_tags(event['PhysicalResourceId'],props)validate(event['PhysicalResourceId'],props)ifwait_for_issuance(event['PhysicalResourceId'],context):event['Status']='SUCCESS'returnsend(event)else:returnreinvoke(event,context)elifevent['RequestType']=='Delete':ifevent['PhysicalResourceId']!='None':acm.delete_certificate(CertificateArn=event['PhysicalResourceId'])event['Status']='SUCCESS'returnsend(event)elifevent['RequestType']=='Update':ifreplace_cert(event):event['PhysicalResourceId']=create_cert(props,i_token)add_tags(event['PhysicalResourceId'],props)validate(event['PhysicalResourceId'],props)ifnotwait_for_issuance(event['PhysicalResourceId'],context):returnreinvoke(event,context)else:if'Tags'inevent['OldResourceProperties']:acm.remove_tags_from_certificate(CertificateArn=event['PhysicalResourceId'],Tags=event['OldResourceProperties']['Tags'])add_tags(event['PhysicalResourceId'],props)event['Status']='SUCCESS'returnsend(event)else:raiseRuntimeError('Unknown RequestType')exceptExceptionasex:l.exception('')event['Status']='FAILED'event['Reason']=str(ex)returnsend(event)Becomes:defhandler(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']ifA[G]=='Create':A[B]=J;A[B]=create_cert(C,F);add_tags(A[B],C);validate(A[B],C)ifwait_for_issuance(A[B],D):A[E]=H;returnsend(A)else:returnreinvoke(A,D)elifA[G]=='Delete':ifA[B]!=J:acm.delete_certificate(CertificateArn=A[B])A[E]=H;returnsend(A)elifA[G]=='Update':ifreplace_cert(A):A[B]=create_cert(C,F);add_tags(A[B],C);validate(A[B],C)ifnotwait_for_issuance(A[B],D):returnreinvoke(A,D)else:ifKinA[L]:acm.remove_tags_from_certificate(CertificateArn=A[B],Tags=A[L][K])add_tags(A[B],C)A[E]=H;returnsend(A)else:raiseRuntimeError('Unknown RequestType')exceptExceptionasI:l.exception('');A[E]='FAILED';A['Reason']=str(I);returnsend(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.InstallationTo install python-minifier use pip:$pipinstallpython-minifierNote 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.UsageTo minify a source file, and write the minified module to stdout:$pyminifyhello.pyThere is also an API. The same example would look like:importpython_minifierwithopen('hello.py')asf:print(python_minifier.minify(f.read()))Documentation is available atdflook.github.io/python-minifier/LicenseAvailable under the MIT License. Full text is in theLICENSEfile.Copyright (c) 2020 Daniel Flook