Metadata-Version: 2.4
Name: minipy3
Version: 1.2.0
Summary: Minimizes Python 3 code
License-Expression: MIT
License-File: LICENSE
Keywords: minipy,minimize,code,.min.py,compress
Author: Nikita Denissov
Author-email: n.denissov@proton.me
Requires-Python: >=3.9,<3.14
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Code Generators
Classifier: Topic :: System :: Archiving :: Compression
Project-URL: Homepage, https://github.com/ndenissov/minipy3
Project-URL: Repository, https://github.com/ndenissov/minipy3
Description-Content-Type: text/markdown

# minipy3

[![PyPI version](https://img.shields.io/pypi/v/minipy3.svg)](https://pypi.org/project/minipy3/)
[![Downloads](https://static.pepy.tech/badge/minipy3)](https://pepy.tech/project/minipy3)
[![Python versions](https://img.shields.io/pypi/pyversions/minipy3.svg)](https://pypi.org/project/minipy3/)
[![License](https://img.shields.io/pypi/l/minipy3.svg)](https://github.com/ndenissov/minipy3/blob/main/LICENSE)
[![GitHub stars](https://img.shields.io/github/stars/ndenissov/minipy3)](https://github.com/ndenissov/minipy3/stargazers)

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`:

```bash
pip install minipy3
```

## Usage

### Command Line Interface (CLI)

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

```bash
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:**
```bash
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.

```python
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.

```python
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**
```python
# 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)**
```python
# __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](LICENSE) file for details.

