Transform Python source code into it's most compact representation
Project description
Python Minifier
Transforms Python source code into it's most compact representation.
python-minifier supports Python 2.6 to 2.7 and Python 3.3 to 3.7.
As an example, the following python source:
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)
Becomes:
A='PhysicalResourceId'
B='Status'
C='RequestType'
D='SUCCESS'
E='None'
F='Tags'
G='OldResourceProperties'
def handler(event,context):
l.info(event)
try:
i_token=hashlib.new('md5',(event['RequestId']+event['StackId']).encode()).hexdigest();props=event['ResourceProperties']
if event[C]=='Create':
event[A]=E;event[A]=create_cert(props,i_token);add_tags(event[A],props);validate(event[A],props)
if wait_for_issuance(event[A],context):
event[B]=D;return send(event)
else:return reinvoke(event,context)
elif event[C]=='Delete':
if event[A]!=E:acm.delete_certificate(CertificateArn=event[A])
event[B]=D;return send(event)
elif event[C]=='Update':
if replace_cert(event):
event[A]=create_cert(props,i_token);add_tags(event[A],props);validate(event[A],props)
if not wait_for_issuance(event[A],context):return reinvoke(event,context)
else:
if F in event[G]:acm.remove_tags_from_certificate(CertificateArn=event[A],Tags=event[G][F])
add_tags(event[A],props)
event[B]=D;return send(event)
else:raise RuntimeError('Unknown RequestType')
except Exception as ex:
l.exception('');event[B]='FAILED';event['Reason']=str(ex);return send(event)
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.
Installation
To install python-minifier use pip:
$ pip install python-minifier
Note 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.6 to 2.7 and Python 3.3 to 3.7.
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:
import python_minifier
with open('hello.py') as f:
print(python_minifier.minify(f.read()))
Documentation is available at dflook.github.io/python-minifier/
License
Available under the MIT License. Full text is in the LICENSE file.
Copyright (c) 2018 Daniel Flook
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_minifier-1.1.0.tar.gz.
File metadata
- Download URL: python_minifier-1.1.0.tar.gz
- Upload date:
- Size: 23.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b930e037b3535ec979138c45cc7d45370696f0e901c37b3dbbbbefd047a7f9c
|
|
| MD5 |
512709d387616fc6873be52e6c922c0e
|
|
| BLAKE2b-256 |
ce3773a4cb86b15c8af79935b3e7145229514723b0cd46b7eacc5b7d6d071c80
|
File details
Details for the file python_minifier-1.1.0-py3-none-any.whl.
File metadata
- Download URL: python_minifier-1.1.0-py3-none-any.whl
- Upload date:
- Size: 24.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c03c52a288e94fdc8eff3a46db4084fbbb779ffd73c9b128fb1b8c1c610a0e0
|
|
| MD5 |
89a0c05bdb90b73312b4a44325f852c8
|
|
| BLAKE2b-256 |
7bb3de0f1cde64862a94baaaa1245c70fbcb0cc2e62e414a2ab39f4c526733a8
|