$ stat ./projects/watcher.md

Title: Watcher

Date: 8/4/2024

Description: Watcher is a lightweight Python package designed for developers who prefer hot-reloading files from the terminal.

Watcher

Watcher is a lightweight Python package designed for developers who prefer hot-reloading files from the terminal. I'm sure there are pre-made solutions available with many more features so you should probably try those first. It uses the entr package with Python's subprocess module to watch files for changes and automatically execute them, displaying real-time output and tracebacks directly in the terminal. Unlike pytest, Watcher provides a minimal setup for running and debugging your scripts in real-time.

Features

  • Hot-reloading: Automatically re-execute your script whenever changes are detected.
  • Real-time feedback: See output and tracebacks immediately in the terminal.
  • Lightweight: No need for complex configurations or dependencies like pytest.

Installation

  1. Install entr: Ensure that the entr utility is installed on your system:

    # On Debian/Ubuntu
    sudo apt-get install entr
    
    # I use Arch, btw
    yay entr
    
  2. Install Watcher: You can install the Watcher package using pip by navigating to your cloned package dir and run:

    pip install
    

Usage

Once installed, you can use the watch command to monitor and execute a script whenever it changes:

watch /path/to/your/script.py

This command will monitor the specified file and automatically re-run it each time you save changes, providing real-time feedback in the terminal.

Example

If you have a Python script called example.py:

# example.py
print("Hello, World!")

Run the following command to start watching the file:

watch example.py

Now, every time you edit and save example.py, Watcher will re-run it, and you’ll immediately see the output in your terminal.

Why Watcher?

  • Simplify debugging: Skip the overhead of test frameworks when you just need quick feedback.
  • Terminal-friendly: Stay in your terminal environment and avoid switching contexts.
  • Minimal setup: Just install and start using it—no configuration required
  • But really I just thought it would be fun

$ grep -r "related" ./projects/

Finding related projects...

$ cd .. && ./projects.sh

← Back to all projects