Skip to main content

minipy3

PyPI version Downloads Python versions License GitHub stars

A utility to minimize and compress Python 3 code. It reduces script size by rewriting the Abstract Syntax Tree (AST), stripping unnecessary characters, and optionally applying native compression algorithms.

Features

  • Semicolon Insertion: Automatically places semicolons where needed.
  • Numeric Optimization: Large integers are converted into mathematical powers (e.g., 100000 -> 10**5).
  • Import Consolidation: Groups sequential import statements into a single line.
  • Docstring Truncation: Strips off white spaces, dedents, and minifies docstrings.
  • Advanced Compression: Optionally compresses code using lzma, zlib, gzip, or bz2 with extreme presets and Base85 encoding to maximize payload reduction.

Installation

You can install the package via pip or poetry:

pip install minipy3

Usage

Command Line Interface (CLI)

You can use the module directly from the terminal to compress entire directories or specific files.

usage: minipy3 [-h] [-o OUT] [--no-compress] [--unparse] [--no-suffix] input

positional arguments:
  input              Input files rglob

options:
  -h, --help         show this help message and exit
  -o OUT, --out OUT  Output files rglob
  --no-compress      Don't use compression algorithms (lzma, zlib, gzip or bz2)
  --unparse          Return from compressed to standard view
  --no-suffix        Add suffix to file name (max for --unparse else min)

Example:

minipy3 --no-suffix venv\**\*.py

This will compress all .py files in the venv folder and its subdirectories. Note: --no-suffix overwrites the source files. If omitted, .min.py (or .max.py with --unparse) suffixes are appended.

Python API

You can also integrate minipy3 directly into your Python projects.

from minipy3 import minimize, AddSemicolon

def minimize(raw_code: str | bytes,
             compress: bool = True,
             compress_required: bool = False) -> str:
    """
    Minimizes the passed code. 
    Applies the best compression algorithm if it results in a smaller payload.
    """
    ...

AddSemicolon Utility

An internal utility to format code with semicolons instead of newlines.

import ast
from minipy3 import AddSemicolon

class Minimize(AddSemicolon, ignore={'Import'}):
    """Class based on ast._Unparse for code compression (see ast.unparse)"""
    ...

def add_semicolons(raw_code: str | bytes) -> str: 
    return AddSemicolon().visit(ast.parse(raw_code))

Examples

Example 1: Basic Minimization

# Before
from __future__ import annotations
import ast
import functools

class AddSemicolon(ast._Unparser):
    """Adds a semicolon where needed (see also ast.unparse)"""
    __slots__ = ()

# After minimization
from __future__ import annotations;import ast,functools
class AddSemicolon(ast._Unparser):
 'Adds a semicolon where needed (see also ast.unparse)';__slots__=()

Example 2: Compressed Output (Using lzma)

# __init__.py (Use --no-compress to disable)
i=__import__;exec(i('lzma').decompress(i('base64').b85decode(b'T>t<81poj4|NsC0...00')))

License

This project is licensed under the MIT License. See the LICENSE file for details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minipy3-1.2.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

minipy3-1.2.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file minipy3-1.2.0.tar.gz.

File metadata

  • Download URL: minipy3-1.2.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.13.14 Linux/7.1.4-zen1

File hashes

Hashes for minipy3-1.2.0.tar.gz
Algorithm Hash digest
SHA256 852bd057065292404eca2eb1addab1e4a90927abcc387a1674406fe6be4ff5ad
MD5 a9899809fc1c9650bfc196c869bd32d5
BLAKE2b-256 675d0338a38697a0a7e1cc829dee304cb1cf0dee4ea8071fdfefff7200e4b9ea

See more details on using hashes here.

File details

Details for the file minipy3-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: minipy3-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.13.14 Linux/7.1.4-zen1

File hashes

Hashes for minipy3-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3afac294fb113c61a028e48e3dfbf2f2139509e037ad6ef8f860dde705b98805
MD5 7b9d4fd4e3e4e49f8869383b29ee6912
BLAKE2b-256 f2ce76337fea7db2ffde0b49fb988cc94ca191a2d13ba33be2da5ae6279b2376

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page