Skip to main content

将 Python 源代码转换为最紧凑的表示形式

项目描述

Python 缩小器

python-minifier

将 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 (41.5 kB 查看哈希

已上传 source

内置分布

python_minifier-2.6.0-py2.py3-none-any.whl (41.1 kB 查看哈希

已上传 py2 py3