Skip to content

Commit 3eea110

Browse files
committed
Refactor for coding best practices
1 parent ae24fb1 commit 3eea110

File tree

4 files changed

+78
-65
lines changed

4 files changed

+78
-65
lines changed

namedtuple_maker/namedtuple_logger.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import logbook
3333

3434
# Imports - Local
35-
from graceful_exit import graceful_exit
35+
from namedtuple_maker.namedtuple_utils import graceful_exit
3636

3737
# Constants
3838
LOG_FILE_PATH = path.curdir
@@ -139,9 +139,9 @@ def initialize_logging(
139139
)
140140

141141
# Display error and gracefully exit
142-
except FileNotFoundError as e:
142+
except FileNotFoundError as error:
143143
graceful_exit(
144-
error_object=e,
144+
error_object=error,
145145
)
146146

147147
else:

namedtuple_maker/namedtuple_maker.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,28 @@ def your_function() -> Iterable:
3131
3232
Logging Target:
3333
The application automatically creates a log file in the current
34-
working directory, and writes log message to that file. To write log
35-
messages to the console (STDOUT), set the LOG_TO_CONSOLE environment
36-
variable to 'True'.
34+
working directory, and writes log message to that file. To
35+
write log messages to the console (STDOUT), set the
36+
LOG_TO_CONSOLE environment variable to 'True'.
3737
3838
Example logging target usage:
3939
export LOG_TO_CONSOLE=True
4040
'''
4141

42-
# Imports
42+
# Imports - Python Standard Library
4343
from collections import namedtuple
4444
from typing import Callable, Iterable, List, NamedTuple
4545
from functools import wraps
46-
from namedtuple_maker.namedtuple_logger import initialize_logging
4746
from re import compile, VERBOSE
48-
from os import _exit, getenv
49-
from sys import exit, stderr
47+
from os import getenv
48+
49+
# Imports - Third-Party
5050
import logbook
5151

52+
# Imports - Local
53+
from namedtuple_maker.namedtuple_logger import initialize_logging
54+
from namedtuple_maker.namedtuple_utils import graceful_exit
55+
5256
# Check for LOG_LEVEL environment variable
5357
log_level = getenv('LOG_LEVEL')
5458

@@ -260,16 +264,17 @@ def convert_to_namedtuple(*args, **kwargs) -> namedtuple:
260264
261265
attribute_names (Iterable[str]):
262266
Optional kwarg, any iterable object class
263-
including, list, tuple, dict_keys, dict_values, etc.
264-
with str values.
267+
including, list, tuple, dict_keys, dict_values,
268+
etc. with str values.
265269
266270
auto_attribute_names (bool):
267271
Optional kwarg, automatically name attributes
268-
without user input or use of the attribute_names
269-
parameter. Default: False
272+
without user input or use of the
273+
attribute_names parameter. Default: False
270274
271275
Returns: named_tuple (namedtuple):
272-
Class NamedTuple instantiated from collections.namedtuple
276+
Class NamedTuple instantiated from
277+
collections.namedtuple
273278
'''
274279

275280
# Log entry
@@ -587,8 +592,8 @@ def make_named_tuple(
587592
Any iterable object to convert to a namedtuple.
588593
589594
attribute_names (Iterable[str]):
590-
Any iterable object of strings to supply field names for
591-
a namedtuple.
595+
Any iterable object of strings to supply field names
596+
for a namedtuple.
592597
593598
auto_attribute_names (bool):
594599
Optional kwarg, automatically name attributes
@@ -640,7 +645,7 @@ def make_named_tuple(
640645
f'\t{iterable_input}'
641646
)
642647

643-
except TypeError as e:
648+
except TypeError as error:
644649

645650
# Set error message value
646651
error_message = (
@@ -655,22 +660,13 @@ def make_named_tuple(
655660
f'\t"iterable_input" value: {iterable_input}'
656661
f'\t"iterable_input" type: {type(iterable_input)}'
657662
)
658-
application_log.exception(e)
659-
660-
# Write error message to STDERR
661-
print(f'\n{error_message}')
662-
print(f'{e!r}\n', file=stderr)
663+
application_log.exception(error)
663664

664-
# Graceful exit with status code
665-
try:
666-
667-
# Standard sys.exit
668-
exit(1)
669-
670-
except SystemExit:
671-
672-
# Exit from an interactive REPL shell with os._exit
673-
_exit(1)
665+
# Display the error message and gracefully exit
666+
graceful_exit(
667+
error_message=error_message,
668+
error_object=error
669+
)
674670

675671
# Log Entry
676672
# Log return of make_named_tuple function to named_tuple_converter
@@ -696,8 +692,9 @@ def run_make_named_tuple() -> NamedTuple:
696692
697693
Returns:
698694
named_tuple (NamedTuple):
699-
NamedTuple class object resulting from the make_named_tuple
700-
function decorated by the named_tuple_converter function.
695+
NamedTuple class object resulting from the
696+
make_named_tuple function decorated by the
697+
named_tuple_converter function.
701698
'''
702699

703700
# Log Entry

tests/test_namedtuple_logger.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
1414
'''
1515

16-
# Imports
17-
from namedtuple_maker.namedtuple_logger import initialize_logging
16+
# Imports - Third-Party
1817
from logbook import Logger
1918
# from pytest import raises
2019

20+
# Imports - Local
21+
from namedtuple_maker.namedtuple_logger import initialize_logging
22+
2123
# Constants
2224
LOG_FILE_INVALID = './bad_log_test_dir/log_file.log'
2325
LOG_INFO_MESSAGE = 'This is a log entry.'
@@ -27,9 +29,9 @@
2729

2830

2931
def test_initialize_logging_to_console(capfd) -> None:
30-
''' Test initialize_logging function for console output. Writes mock
31-
log messages to the console and verifies the log messages display
32-
correctly.
32+
''' Test initialize_logging function for console output. Writes
33+
mock log messages to the console and verifies the log messages
34+
display correctly.
3335
3436
Args:
3537
capfd (pytest fixture):
@@ -56,6 +58,8 @@ def test_initialize_logging_to_console(capfd) -> None:
5658
log_output = capfd.readouterr().out
5759
assert LOG_INFO_MESSAGE in log_output
5860

61+
return None
62+
5963

6064
# def test_initialize_logging_invalid_log_file() -> None:
6165
# ''' Test initialize logging function's ability to handle an invalid
@@ -72,3 +76,4 @@ def test_initialize_logging_to_console(capfd) -> None:
7276
# initialize_logging(
7377
# log_file=LOG_FILE_INVALID
7478
# )
79+
# return None

tests/test_namedtuple_maker.py

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
pytest tests/test_namedtuple_maker.py
66
'''
77

8-
# Imports
9-
from namedtuple_maker.namedtuple_maker import named_tuple_converter, \
10-
make_named_tuple, TEST_DATA
8+
# Imports - Python Standard Library
119
from collections import namedtuple
12-
from pytest import mark, raises
1310
from typing import Iterable, Tuple
1411
from unittest.mock import patch
1512

13+
# Imports - Third-Party
14+
from pytest import mark, raises
15+
16+
# Imports - Local
17+
from namedtuple_maker.namedtuple_maker import named_tuple_converter, \
18+
make_named_tuple, TEST_DATA
19+
1620
# Constants
1721
# Create a namedtuple object for synthetic testing
1822
TEST_PERSON_INFO = namedtuple(
@@ -106,20 +110,23 @@ def test_named_tuple_converter(
106110
iter_return,
107111
att_return
108112
) -> None:
109-
''' Test of the named_tuple_converter decorator function to determine if
110-
the function accepts an iterable argument and returns a namedtuple
111-
with the original iterable data values. Collect the namedtuple field
112-
names from an iterable of names passed an argument.
113+
''' Test of the named_tuple_converter decorator function to
114+
determine if the function accepts an iterable argument and
115+
returns a namedtuple with the original iterable data values.
116+
Collect the namedtuple field names from an iterable of names
117+
passed an argument.
113118
114119
Args:
115120
iter_input (Iterable):
116-
Iterable input to be converted to namedtuple attribute values.
121+
Iterable input to be converted to namedtuple attribute
122+
values.
117123
118124
att_names (Iterable):
119125
Iterable input of namedtuple attribute names.
120126
121127
iter_return (Iterable):
122-
Iterable output of expected namedtuple attribute values.
128+
Iterable output of expected namedtuple attribute
129+
values.
123130
124131
att_return (Iterable):
125132
Iterable output of expected namedtuple attribute names.
@@ -151,14 +158,16 @@ def test_named_tuple_converter(
151158
side_effect=TEST_DATA.keys()
152159
)
153160
def test_named_tuple_converter_input(side_effects) -> None:
154-
''' Test of the named_tuple_converter decorator function to determine if
155-
the function accepts an iterable argument and returns a namedtuple
156-
with the original iterable data values. Collect the namedtuple
157-
attribute names with the input() function.
161+
''' Test of the named_tuple_converter decorator function to
162+
determine if the function accepts an iterable argument and
163+
returns a namedtuple with the original iterable data values.
164+
Collect the namedtuple attribute names with the input()
165+
function.
158166
159167
Args:
160168
side_effects (unittest.mock.patch):
161-
namedtuple attribute name mock data for the input() function.
169+
namedtuple attribute name mock data for the input()
170+
function.
162171
163172
Returns:
164173
None.
@@ -202,16 +211,18 @@ def test_named_tuple_converter_custom_function_auto_name_attributes(
202211
iter_return,
203212
att_return
204213
) -> None:
205-
''' Test of the named_tuple_converter decorator function using a custom
206-
function to determine if the function accepts an iterable argument
207-
and returns a namedtuple with the original iterable data, and if the
208-
decorator function automatically names the namedtuple attributes
209-
with the auto_attribute_names parameter set to True. Collect the
210-
namedtuple field names from an iterable of names passed an argument.
214+
''' Test of the named_tuple_converter decorator function using a
215+
custom function to determine if the function accepts an
216+
iterable argument and returns a namedtuple with the original
217+
iterable data, and if the decorator function automatically
218+
names the namedtuple attributes with the auto_attribute_names
219+
parameter set to True. Collect the namedtuple field names from
220+
an iterable of names passed an argument.
211221
212222
Args:
213223
iter_input (Iterable):
214-
Iterable input to be converted to namedtuple attribute values.
224+
Iterable input to be converted to namedtuple attribute
225+
values.
215226
216227
att_names (Iterable):
217228
Iterable input of namedtuple attribute names.
@@ -240,9 +251,9 @@ def test_named_tuple_converter_custom_function_auto_name_attributes(
240251

241252

242253
def test_named_tuple_converter_invalid_iterable_exception() -> None:
243-
''' Test of the named_tuple_converter decorator function for exception
244-
handling, when the iterable_input argument contains a non-iterable
245-
object/value.
254+
''' Test of the named_tuple_converter decorator function for
255+
exception handling, when the iterable_input argument contains
256+
a non-iterable object/value.
246257
247258
Args:
248259
None.

0 commit comments

Comments
 (0)