将 Python 源代码转换为最紧凑的表示形式
项目描述
Python 缩小器
将 Python 源代码转换为其最紧凑的表示形式。
python-minifier 目前支持 Python 2.7 和 Python 3.3 到 3.10。以前的版本支持 Python 2.6。
例如,以下 python 源代码:
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)
变成:
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)
为什么?
AWS Cloudformation 模板中可能嵌入了 AWS lambda 函数源代码,但前提是函数小于 4KiB。我写了这个包,所以我可以正常编写 python 并且仍然将模块嵌入到模板中。
安装
要安装 python-minifier,请使用 pip:
$ pip install python-minifier
请注意,python-minifier 依赖于解析源代码的 python 解释器,因此请使用适合您源代码的 python 版本进行安装。
python-minifier 运行并可以将为 Python 2.7 和 Python 3.3 编写的代码缩小到 3.10。
用法
缩小源文件,并将缩小后的模块写入标准输出:
$ pyminify hello.py
还有一个API。相同的示例如下所示:
import python_minifier
with open('hello.py') as f:
print(python_minifier.minify(f.read()))
文档位于dflook.github.io/python-minifier/
执照
在 MIT 许可下可用。全文在LICENSE文件中。
版权所有 (c) 2020 丹尼尔·弗洛克
项目详情
关
python_minifier -2.6.0.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 67ebdab1e153044ade352957d118ea78c5629700e962b57651c59ba152057f40 |
|
| MD5 | 76c88a7bd5d1d94e3572ac22f785a745 |
|
| 布莱克2-256 | b2d35419aff2d297c0351c8474c5c0737ee6b82578f81da42623a72fd57e8920 |
关
python_minifier -2.6.0-py2.py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 6db2f26d5486168495ea25515026874ca58eb8c82dc1ba5fc98159c3ed67d3e5 |
|
| MD5 | 5a3aa7c3a60f0f73d5050f18f4bcba9f |
|
| 布莱克2-256 | 56649ee623ebea172067a7e27c4866de46245ffe95e2b4bce33c852ed28a2989 |