Skip to content

Commit ae24fb1

Browse files
committed
Refactor for scalability
Update docstrings
1 parent 2a26cdb commit ae24fb1

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

namedtuple_maker/graceful_exit.py renamed to namedtuple_maker/namedtuple_utils.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
#!/usr/bin/env python3
2-
''' Gracefully exit after catching an exception. Call the
3-
graceful_exit function from within an except block that catches
4-
an error for which the intent is to exit the program without
5-
displaying full trace stack details.
2+
''' Utility module with functions for the namedtuple-maker application.
3+
4+
Usage:
5+
See individual function docstrings for usage instructions.
6+
'''
7+
8+
# Imports - Python Standard Library
9+
from os import _exit
10+
from sys import exit, stderr
11+
from typing import AnyStr
12+
13+
14+
def graceful_exit(
15+
error_message: AnyStr = None,
16+
error_object: Exception = None
17+
):
18+
''' Gracefully exit a program after catching an exception. Call the
19+
graceful_exit function from within a higher-level except block.
20+
Instead of using the raise keyword to display a stack trace and
21+
exit, graceful_exit will display a friendly message, and exit the
22+
program without displaying full trace stack details.
623
724
The graceful_exit function first attempts to use the sys.exit
825
function to exit the application. If sys.exit raises a
926
SystemExit exception, usually as the result of running a program
1027
within an interactive shell (IDLE, iPython, etc.), graceful_exit
1128
will use the os._exit function.
1229
30+
Args:
31+
error_message (AnyStr, optional):
32+
String error message to display.
33+
34+
error_object (Exception):
35+
Exception object from the source except block. Write
36+
the output to STDERR.
37+
1338
Usage:
1439
# Step 1, import the graceful_exit() function into your
1540
# application.
@@ -27,30 +52,10 @@
2752
try:
2853
my_function()
2954
except NameError as error:
30-
graceful_exit(error)
31-
'''
32-
33-
# Imports - Python Standard Library
34-
from os import _exit
35-
from sys import exit, stderr
36-
from typing import AnyStr
37-
38-
39-
def graceful_exit(
40-
error_message: AnyStr = None,
41-
error_object: Exception = None
42-
):
43-
''' Gracefully exit after catching an exception exception. Display
44-
a friendly exception message with the shorthand for repr(), and
45-
exit the application without displaying a full stack trace.
46-
Optionally write a custom error message to STDERR.
47-
48-
Args:
49-
error_message (AnyStr, optional):
50-
String error message to display.
51-
52-
error_object (Exception):
53-
Exception object from the source except block.
55+
graceful_exit(
56+
error_message='An error occurred',
57+
error_object=error
58+
)
5459
5560
Returns:
5661
N/A.

0 commit comments

Comments
 (0)