Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit fe4c6ea

Browse files
Update README.md to enhance documentation with quickstart examples and clarify usage instructions
1 parent 4aeded7 commit fe4c6ea

File tree

4 files changed

+207
-170
lines changed

4 files changed

+207
-170
lines changed

ExceptionHandler/README.md

Lines changed: 47 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,105 +17,83 @@ providing a consistent way to manage exceptions across your codebase.
1717
- **Script Exit Control**: Control whether the script exits after handling an exception.
1818
- **Return or Print Messages**: Choose to return exception messages as strings or print them directly.
1919

20+
## Quickstart
21+
22+
```python
23+
from ExceptionHandler import Handler, Colors, Format
24+
25+
# Initialize a handler with helpful defaults
26+
handler = Handler(show_line=True, trace=True, use_timestamp=True)
27+
handler.formatter(main_color=Colors.RED, message_color=Colors.YELLOW, datetime_format="%d-%m-%Y %H:%M:%S")
28+
29+
try:
30+
1 / 0
31+
except Exception as e:
32+
handler.exception(msg=e, exit_script=False)
33+
```
34+
2035
## Installation
2136

2237
Clone the repository and ensure you have Python installed.
23-
Recommended to use Python 3.11 or higher.
38+
Recommended to use Python 3.9 or higher.
2439

2540
```bash
26-
git clone https://github.com/DefinetlyNotAI/ExceptionHandler.git
27-
cd ExceptionHandler
41+
git clone <repo-url>
42+
cd PyUtils
43+
pip install -r requirements.txt
2844
```
2945

3046
## Usage
3147

3248
### Initialize the Handler
3349

3450
```python
35-
from exception import Handler, Colors, Format
51+
from ExceptionHandler import Handler
52+
handler = Handler()
53+
```
3654

37-
# Initialize the Handler with custom settings - These are global settings
38-
# NOTE: return_string_rather_than_print has priority over exit_script both locally and globally!
39-
handler = Handler(
40-
show_line=True, # Show the line number where the exception occurred
41-
trace=True, # Include the traceback in the output
42-
use_timestamp=True, # Add a timestamp to the exception message
43-
exit_script=True, # Exit the script after handling the exception
44-
return_string_rather_than_print=True
45-
# Return the message instead of printing it,
46-
# if True it will override/ignore exit_script
47-
)
55+
### Customize formatter
4856

49-
# Customize the formatter
50-
# NOTE: Specific settings always the defaults,
51-
# example if message_color is set it will override the main_color for the message part,
52-
handler.formatter(
53-
main_color=Colors.RED, # Set the main color to red
54-
message_color=Colors.YELLOW, # Set the message color to yellow
55-
trace_color=Colors.CYAN, # Set the traceback color to cyan
56-
timestamps_color=Colors.GREEN, # Set the timestamp color to green
57-
main_format=Format.BOLD, # Set the main format to bold
58-
message_format=Format.UNDERLINE, # Underline the message text
59-
trace_format=Format.DIM, # Dim the traceback text
60-
timestamps_format=Format.BLINK, # Blink the timestamp text
61-
datetime_format="%d-%m-%Y %H:%M:%S" # Customize the datetime format
57+
```python
58+
from ExceptionHandler import Colors, Format, Handler
59+
handler = Handler(
60+
show_line=True, # Show the line number where the exception occurred
61+
trace=True, # Include the traceback in the output
62+
use_timestamp=True, # Add a timestamp to the exception message
63+
exit_script=True, # Exit the script after handling the exception
64+
return_string_rather_than_print=True
65+
# Return the message instead of printing it,
66+
# if True it will override/ignore exit_script
6267
)
68+
handler.formatter(main_color=Colors.RED, message_color=Colors.YELLOW)
6369
```
6470

6571
### Example Function
6672

6773
```python
68-
# Bare Minimum
69-
from exception import Handler
70-
71-
handler = Handler() # Use default settings
72-
73-
def divide_numbers(a, b):
74-
try:
75-
return a / b
76-
except Exception as e:
77-
# Note: You can just use handler.exception() or handler.exception(msg=e) to rely on global settings
78-
handler.exception(
79-
# These are local settings, if they are different from the global, they take priority
80-
# so they will override the global settings only for this case.
81-
msg=e, # Detailed exception message, can be a custom string
82-
exit_script=True, # Exit the script after handling the exception
83-
quit_code=1, # Exit code to use when exiting the script, defaults to 1
84-
return_string_rather_than_print=False # Return the message instead of printing it
85-
# NOTE: If return_string_rather_than_print is set to True globally,
86-
# and you want to locally quit for this singular case,
87-
# then you HAVE TO explicitly set it to False here.
88-
)
89-
90-
divide_numbers(10, 0)
74+
# Use the example script at example/ExceptionHandle.py for a full demonstration
9175
```
9276

93-
## Customization
94-
95-
- **Global Settings**: Set during `Handler` initialization.
96-
- **Local Settings**: Override global settings in the `exception` method.
97-
- **Formatter**: Customize colors and formats using the `formatter` method.
98-
99-
Check the [`example.py`](example.py) file for a proper example.
100-
101-
### Override priority
77+
## Override priority
10278

10379
- Local settings take precedence over global settings.
104-
- If both global and local settings are provided, the local settings will override the global ones.
105-
- To use the global settings, simply call `handler.exception()`/`handler.exception(msg=e)`.
106-
- Any settings you want to override for this specific use case can be passed as arguments.
10780
- `return_string_rather_than_print` has priority over `exit_script` both locally and globally.
108-
- If `return_string_rather_than_print` is set to `True` globally, and you want to locally quit for this singular case, then you HAVE TO explicitly set it to `False` in the local settings.
109-
110-
### Modify Colors, Format or Messages
11181

112-
If you want to modify the DataType for format's or colors ANSI, you can do so by modifying the `Format` and `Colors` classes in the [`exception.py`](colors.py) file.
82+
## Modify Colors, Format or Messages
11383

114-
If you want to add/remove Messages from the dictionary, you can do so by modifying the `Messages` class in the [`messages.py`](messages.py) file.
84+
- Colors and formats are defined in `ExceptionHandler/colors.py`.
85+
- Generic messages are stored in `ExceptionHandler/messages.py`.
11586

11687
## Dependencies
11788

118-
- Datetime
89+
- Datetime (standard library)
90+
- `TerminalPrint` for the default print function
91+
92+
## Notes and Safety
93+
94+
- If `exit_script` is True the program will call `sys.exit()` after printing/returning the message.
95+
- `return_string_rather_than_print` has priority over `exit_script` (if message is returned, the handler will not exit).
96+
- The default printing function is `TPrint().critical` from `TerminalPrint`. You can pass any callable that accepts a single string argument.
11997

12098
## License
12199

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# PyUtils
2+
3+
Lightweight collection of small Python utilities used across projects: a configurable exception handler, a colored terminal printing helper, and a simple Git update manager.
4+
5+
Last updated: 2026-01-17
6+
7+
## Modules
8+
9+
- `ExceptionHandler/` — Friendly, configurable exception formatting and handling. See `ExceptionHandler/README.md` for details and examples.
10+
- `TerminalPrint/` — Console printing helper with color schemes, log-to-file and timestamp support. See `TerminalPrint/README.md`.
11+
- `UpdateManager/` — Small helper to fetch and merge updates from a remote Git branch for a local repo path. See `UpdateManager/README.md`.
12+
13+
## Quickstart
14+
15+
Install requirements (if any):
16+
17+
```bash
18+
pip install -r requirements.txt
19+
```
20+
21+
A short example using the three modules together:
22+
23+
```python
24+
from TerminalPrint import TPrint
25+
from ExceptionHandler import Handler
26+
from UpdateManager import GitUpdateManager
27+
28+
# Create a terminal printer
29+
printer = TPrint(debug_mode=True, use_timestamps=True)
30+
31+
# Create an exception handler that uses the TPrint critical function
32+
handler = Handler(show_line=True, trace=True, use_timestamp=True, print_function=printer.critical)
33+
handler.formatter(main_color=printer.color_scheme['error'])
34+
35+
# Attempt an update with handling
36+
try:
37+
manager = GitUpdateManager(repo_path="../UpdateManager/", branch="main")
38+
manager.update()
39+
except Exception:
40+
handler.exception(exit_script=False)
41+
```
42+
43+
## Examples
44+
45+
Example scripts live under `example/`:
46+
- `example/TPrint.py` — demo for `TerminalPrint`.
47+
- `example/ExceptionHandle.py` — demo for `ExceptionHandler`.
48+
- `example/Update.py` — demo for `UpdateManager`.
49+
50+
## Notes & Safety
51+
52+
- `UpdateManager` calls the system `git` CLI and will `sys.exit()` on failures or when uncommitted changes are detected — ensure you have a clean working tree before running `update()`.
53+
- `ExceptionHandler` may call `sys.exit()` when `exit_script` is enabled; prefer `return_string_rather_than_print` for library usage.
54+
- Terminal coloring uses ANSI escape codes; results may vary on non-ANSI terminals (Windows terminals recent versions and WSL work well).
55+
56+
## Contributing
57+
58+
Contributions welcome — open a pull request and include tests/examples where appropriate.
59+
60+
## License
61+
62+
See the `LICENSE` file in the repository root.

0 commit comments

Comments
 (0)