Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Transform Python source code into its most compact representation

License

NotificationsYou must be signed in to change notification settings

dflook/python-minifier

Repository files navigation

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.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)

Why?

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.

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:

importpython_minifierwithopen('hello.py')asf:print(python_minifier.minify(f.read()))

Documentation is available atdflook.github.io/python-minifier/

License

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

Stars

Watchers

Forks

Sponsor this project

 

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp