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 & Removal: Strips whitespace, dedents, and minifies docstrings, or completely removes them (--no-docstrings).
  • Unused Literal Removal: Strips standalone literals and expressions without side effects (--remove-literals).
  • String Literal Optimization:
    • Replaces hex escapes (\xNN) with shorter octal escapes (\0–\377) where safe (avoiding collisions with following digits).
    • Converts strings with backslashes into raw strings (r'...', rb'...') when advantageous.
    • Replaces single-line strings with many \n into multiline triple-quoted strings ('''...''', """...""").
  • Advanced Compression: Optionally compresses code using lzma, zlib, gzip, or bz2 with extreme presets and Base85 encoding. Evaluates both minimized and raw source code to select the smallest payload.

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]
               [--no-docstrings] [--remove-literals] 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)
  --no-docstrings    Remove docstrings from code
  --remove-literals  Remove unused standalone literals

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, LiteralCleaner

def minimize(raw_code: str | bytes,
             compress: bool = True,
             compress_required: bool = False,
             remove_docstrings: bool = False,
             remove_literals: 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.

Metadata

Release files for minipy3 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for minipy3 1.3.0
File Size Uploaded Provenance Actions
minipy3-1.3.0.tar.gz 13.2 kB Without provenance Details for minipy3-1.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for minipy3 1.3.0
File Interpreter ABI Platform Actions
minipy3-1.3.0-py3-none-any.whl Python 3 none any Details for minipy3-1.3.0-py3-none-any.whl

Total release size: 27.3 kB

Release files / minipy3-1.3.0.tar.gz

Download URL minipy3-1.3.0.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f3384c0271509e26c66a1ff37ccb0a3a12ef9833b35f59f834608f7ec78d4bee
BLAKE2b-256 checksum
How to use checksums
c2e7f0936f95e89b12f4a013e35ced8d3da68f1e0e74561958d4c6fd74607c08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.13.17 Linux/7.2.11-zen1

Release files / minipy3-1.3.0-py3-none-any.whl

Download URL minipy3-1.3.0-py3-none-any.whl
Size 14.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77cd6e1e9c7a493a29201c5635cdef348d61b927a0a5fdef7ca9f84b5399ebd9
BLAKE2b-256 checksum
How to use checksums
872bc3e5601960c38100f4ef82ccdb2074c3ef2f38298aba0db07abbd3db384f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.13.17 Linux/7.2.11-zen1

Security for minipy3 1.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page