| .. | ||
| build/lib/TaskFormatter | ||
| dist | ||
| TaskFormatter | ||
| example.py | ||
| pythonformatterexample.gif | ||
| README.md | ||
| setup.cfg | ||
| setup.py | ||
Formatter and Example
This project demonstrates the use of a Python decorator to format the output of functions, showing a running spinner, and indicating success or failure with colored and formatted text. It includes two files: formatter.py and example.py.
Install
pip install TaskFormatter
Example Output
Files
- formatter.py: This file contains the
format_outputdecorator that provides the desired formatting for function output. - example.py: This file demonstrates how to use the
format_outputdecorator by applying it to two example functions, one that succeeds and one that fails.
Features
- Spinner Animation: Displays a spinner animation while the function is running.
- Success/Failure Indication: Shows a green checkmark for successful completion and a red cross for failures.
- Exception Handling: Exceptions are displayed in red and underlined.
- Function Numbering: Each function is prefixed with a unique number.
- Text Formatting: Function names are displayed in bold and underlined.
- Separation of Outputs: Outputs from different functions are separated by a line for clarity.
Usage
1. formatter.py
The format_output decorator is defined in this file. It wraps a function to display the running spinner, success/failure message, and handle exceptions.
2. example.py
The example.py file demonstrates the use of the format_output decorator. It includes two functions, one that succeeds and one that fails, to showcase the decorator's functionality.
from TaskFormatter import format_output
from time import sleep
@format_output
def successful_function():
print("Starting")
sleep(2)
print("Function logic output")
@format_output
def failing_function():
print("Just Started")
sleep(2)
print("Running")
sleep(2)
print("Done")
raise ValueError("Custom Error")
if __name__ == "__main__":
successful_function()
try:
failing_function()
except Exception as e:
print("Caught an exception: ", e)
Running the Example
- Save formatter.py and example.py in the same directory.
- Run example.py using the command:
python example.py
You should see the spinner, success, and failure messages formatted as described, with appropriate separation and formatting for each function's output.
