Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork46
Transform Python source code into its most compact representation
License
dflook/python-minifier
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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:
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)
AWS Cloudformation templates may have AWS lambda function source code embedded in them, but only if the function is lessthan 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,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.
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:
importpython_minifierwithopen('hello.py')asf:print(python_minifier.minify(f.read()))
Documentation is available atdflook.github.io/python-minifier/
Available under the MIT License. Full text is in theLICENSE file.
Copyright (c) 2024 Daniel Flook
About
Transform Python source code into its most compact representation
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.