Skip to main content

Explains Python errors in clear English for beginners.

Project description


# Explain-Exception

**Explain-Exception** is a Python library that explains Python errors in **clear English**, making it easier for beginners and developers to understand what went wrong in their code. It provides both automatic and manual ways to handle exceptions.

---

## Features

- Explains common Python exceptions in simple English
- Provides beginner-friendly explanations
- Shows the full traceback for debugging
- Optional `safe_run` wrapper to automatically explain errors
- Works with any Python function or code block
- Manual `explain` function for detailed control

---

## Installation

### From GitHub (for development/testing)

```bash
git clone https://github.com/yourusername/explain-exception.git
cd explain-exception
pip install -e .

From PyPI

pip install explain-exception

Usage

We will use the same function examples to show both safe_run and explain.

1. Using safe_run

safe_run executes any function and automatically explains exceptions if they occur.

from explain_exception import safe_run

def test_error():
    # This will raise a ZeroDivisionError
    x = 10 / 0

safe_run(test_error)

Output:

❌ Error Type: ZeroDivisionError
💬 Message: division by zero
💡 Explanation: You tried to divide a number by zero. Division by zero is undefined.

🔍 Traceback:
Traceback (most recent call last):
  File ".../core.py", line ..., in safe_run
    func()
  File "...", line ..., in test_error
    x = 10 / 0
ZeroDivisionError: division by zero

safe_run is perfect for quick debugging. It automatically explains any exception raised by the function, giving both beginner-friendly explanation and the traceback.


2. Using explain manually

The explain function gives you full control over exceptions. Use it when you want to catch the exception yourself and handle it.

from explain_exception import explain

try:
    my_list = [1, 2, 3]
    print(my_list[5])  # Will raise IndexError
except Exception as e:
    print(explain(e))

Output:

❌ Error Type: IndexError
💬 Message: list index out of range
💡 Explanation: You tried to access a position in a list or tuple that doesn’t exist.

explain is ideal for logging, reporting, or handling exceptions in production code while still providing a clear explanation.


3. Wrapping any code block

You can also wrap any code block or lambda function with safe_run:

safe_run(lambda: open("nonexistent_file.txt"))

Output:

❌ Error Type: FileNotFoundError
💬 Message: [Errno 2] No such file or directory: 'nonexistent_file.txt'
💡 Explanation: You tried to open a file that doesn't exist or the path is incorrect.

🔍 Traceback: ...

Recommended Workflow

  1. During development: Use safe_run for quick debugging of any function.
  2. For logging or production: Use explain inside a try/except block for detailed control.
  3. Extending the library: Add more error types in core.py if needed.

Contributing

Contributions are welcome! You can:

  • Add explanations for uncommon exceptions
  • Improve formatting or message clarity
  • Suggest new features

Please follow standard Python best practices and create a pull request for review.


License

This project is licensed under the MIT License.


---

Project details


Download files

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

Source Distribution

explain_exception-0.1.3.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

explain_exception-0.1.3-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file explain_exception-0.1.3.tar.gz.

File metadata

  • Download URL: explain_exception-0.1.3.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.8

File hashes

Hashes for explain_exception-0.1.3.tar.gz
Algorithm Hash digest
SHA256 60dce25e6c25ad72423f310017f4a828f0d709d36532743472381b9c982cecc1
MD5 ca8fa369feeb4f4b2cd0d024f65df464
BLAKE2b-256 e4013aea171c87a1ce7cdd87f68d8982a91f6aa4e592738773c9f23fdca62f89

See more details on using hashes here.

File details

Details for the file explain_exception-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for explain_exception-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f03a14f4487a7cb2e8bbb49a1816f36694d9a26534f967e2988df88b341b805e
MD5 109ebf3f66c54a148b0e7217c28615dc
BLAKE2b-256 1457483f7730a9e22a2bf3c76c9a81f34bfac2926b121ef708bc41109adb734e

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