Skip to content

kakn/python-code-minifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

python-code-minifier

A versatile command-line tool for stripping, cleaning, and minifying Python code. Useful for sending lengthy code to an LLM that has token limits.

Features

  • Remove docstrings, comments, and empty lines
  • Minify whitespace and compress code
  • Strip or preserve essential imports
  • Remove type hints, error handling, assertions, decorators
  • Replace function and class bodies with pass
  • Shorten variable names
  • Convert simple functions to one-liners
  • Compress multiline strings into a single line

Presets

  • --extreme – Apply all stripping options
  • --moderate – Clean while preserving readability
  • --structure-only – Keep only function/class signatures
  • --safe - Minimal cleanup for LLM-friendly code

Usage

You can either paste code in directly or provide an input file:

Option 1: Paste code

python python-code-minifier.py --strip-comments --minify-code

Just paste your code into the terminal and press Ctrl+D (or Ctrl+Z on Windows) to run.

Option 2: Use a file

python python-code-minifier.py --file input.py --output clean.py --extreme

Optional arguments:

  • --count – Show character count before and after
  • --output – Save to a file instead of printing to stdout

Example

Original:

def greet(name: str) -> None:
    """Say hello"""
    print(f"Hello, {name}")  # Greet user

With --extreme:

def greet(name):print(f"Hello,{name}")

About

Command line tool for minimizing lengthy Python code, to avoid LLM token limits.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages