Skip to content

Commit 4d6a77c

Browse files
authored
Merge pull request #21 from timothyhull:timothyhull/refactor-namedtuple-maker
Refactor for PEP compliance
2 parents a319705 + 4b81d5e commit 4d6a77c

File tree

5 files changed

+59
-54
lines changed

5 files changed

+59
-54
lines changed

namedtuple_maker/namedtuple_logger.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,16 @@
4242
LOG_FILE_PATH = path.curdir
4343
LOG_FILE_NAME = 'namedtuple-log.log'
4444
LOG_FILE = path.join(LOG_FILE_PATH, LOG_FILE_NAME)
45-
LOG_LEVEL_DEFAULT = 'INFO'
45+
LOG_LEVEL_DEFAULT = 'CRITICAL'
4646
LOG_LEVELS = (
47-
'FATAL',
48-
'ERROR',
4947
'CRITICAL',
50-
'WARN',
48+
'ERROR',
5149
'WARNING',
52-
'INFO',
5350
'NOTICE',
51+
'INFO',
5452
'DEBUG',
55-
'TRACE'
53+
'TRACE',
54+
'NOTSET'
5655
)
5756

5857

@@ -158,7 +157,6 @@ def initialize_logging(
158157
)
159158

160159
else:
161-
162160
# Initialize logging to console
163161
logbook.StreamHandler(
164162
stream=stdout,

namedtuple_maker/namedtuple_maker.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,18 @@ def my_function(
9999
100100
Logging Level:
101101
The application creates a log level with a default
102-
logging level of 'INFO'. To set a different logging level,
102+
logging level of 'CRITICAL'. To set a different logging level,
103103
set an environment variable named LOG_LEVEL to one of the
104104
following case insensitive logging levels:
105105
106-
- FATAL
107-
- ERROR
108106
- CRITICAL
109-
- WARN
107+
- ERROR
110108
- WARNING
111-
- INFO
112109
- NOTICE
110+
- INFO
113111
- DEBUG
114112
- TRACE
113+
- NOTSET
115114
116115
Example custom logging usage:
117116
export LOG_LEVEL=DEBUG
@@ -130,8 +129,8 @@ def my_function(
130129
from collections import namedtuple
131130
from typing import Callable, Iterable, List, NamedTuple
132131
from functools import wraps
133-
from re import compile, VERBOSE
134132
from os import getenv
133+
import re
135134

136135
# Imports - Third-Party
137136
import logbook
@@ -159,13 +158,13 @@ def my_function(
159158

160159
# Constants
161160
# Match pattern for allowed first characters in a namedtuple attribute
162-
ATTRIBUTE_INPUT_START_CHARACTER = compile(
161+
ATTRIBUTE_INPUT_START_CHARACTER = re.compile(
163162
r'''
164163
^ # Start at the beginning of the line
165164
[^a-zA-Z] # Any non-alphabet character
166165
+ # One or more repetitions
167166
''',
168-
VERBOSE
167+
re.VERBOSE
169168
)
170169

171170
# Log entry
@@ -176,11 +175,11 @@ def my_function(
176175
)
177176

178177
# Match pattern for allowed non-first characters in a namedtuple attribute
179-
ATTRIBUTE_INPUT_INVALID_CHARACTERS = compile(
178+
ATTRIBUTE_INPUT_INVALID_CHARACTERS = re.compile(
180179
r'''
181180
[^\w\s_-] # Only allow alphabet letters, integers, _, -, and spaces
182181
''',
183-
VERBOSE
182+
re.VERBOSE
184183
)
185184

186185
# Log entry
@@ -191,11 +190,11 @@ def my_function(
191190
)
192191

193192
# Match pattern to replace space characters in a namedtuple attribute
194-
ATTRIBUTE_INPUT_SPACE_CHARACTERS = compile(
193+
ATTRIBUTE_INPUT_SPACE_CHARACTERS = re.compile(
195194
r'''
196195
\s # Any space character
197196
''',
198-
VERBOSE
197+
re.VERBOSE
199198
)
200199

201200
# Log entry

namedtuple_maker/namedtuple_utils.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
# Imports - Python Standard Library
99
from os import _exit
1010
from sys import exit, stderr
11-
from typing import AnyStr
1211

1312

1413
def graceful_exit(
15-
error_message: AnyStr = None,
14+
error_message: str = None,
1615
error_object: Exception = None
1716
):
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.
17+
""" Gracefully exit a program after catching an exception.
18+
19+
Call the graceful_exit function from within a higher-level except
20+
block. Instead of using the raise keyword to display a stack trace
21+
and exit, graceful_exit will display a friendly message, and exit
22+
the program without displaying full trace stack details.
2323
2424
The graceful_exit function first attempts to use the sys.exit
2525
function to exit the application. If sys.exit raises a
@@ -28,7 +28,7 @@ def graceful_exit(
2828
will use the os._exit function.
2929
3030
Args:
31-
error_message (AnyStr, optional):
31+
error_message (str, optional):
3232
String error message to display.
3333
3434
error_object (Exception):

tests/test_namedtuple_logger.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030

3131

3232
def test_initialize_logging_to_console(capfd) -> None:
33-
""" Test initialize_logging function for console output. Writes
34-
mock log messages to the console and verifies the log messages
35-
display correctly.
33+
""" Test initialize_logging function for console output.
34+
35+
Writes mock log messages to the console and verifies the log
36+
messages display correctly.
3637
3738
Args:
3839
capfd (pytest fixture):
@@ -63,8 +64,10 @@ def test_initialize_logging_to_console(capfd) -> None:
6364

6465

6566
# def test_initialize_logging_invalid_log_file() -> None:
66-
# """ Test initialize logging function's ability to handle an invalid
67-
# log file path level.
67+
# """ Test logging initialization.
68+
69+
# Test the initialize_logging function's ability to handle an
70+
# invalid log file path level.
6871

6972
# Args:
7073
# None.

tests/test_namedtuple_maker.py

+27-22
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ def custom_namedtuple_function(
5252
iterable_input: Iterable,
5353
auto_attribute_names: bool
5454
) -> Tuple:
55-
""" Test function that accepts an iterable object as input,
56-
and returns the iterable as a tuple object.
55+
""" Test function that accepts an iterable object as input.
56+
57+
Testing function to collect an iterable object and return the
58+
iterable as a tuple object.
5759
5860
Args:
5961
iterable_input (Iterable):
@@ -110,11 +112,12 @@ def test_named_tuple_converter(
110112
iter_return,
111113
att_return
112114
) -> None:
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.
115+
""" Test of the named_tuple_converter decorator function.
116+
117+
Determine if the decorator function accepts an iterable
118+
argument and returns a namedtuple with the original iterable
119+
data values. Collect the namedtuple field names from an
120+
iterable of names passed an argument.
118121
119122
Args:
120123
iter_input (Iterable):
@@ -158,11 +161,12 @@ def test_named_tuple_converter(
158161
side_effect=TEST_DATA.keys()
159162
)
160163
def test_named_tuple_converter_input(side_effects) -> None:
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.
164+
""" Test of the named_tuple_converter decorator function.
165+
166+
Determine if the decorator function accepts an iterable
167+
argument and returns a namedtuple with the original iterable
168+
data values. Collect the namedtuple attribute names with the
169+
input() function.
166170
167171
Args:
168172
side_effects (unittest.mock.patch):
@@ -211,13 +215,13 @@ def test_named_tuple_converter_custom_function_auto_name_attributes(
211215
iter_return,
212216
att_return
213217
) -> None:
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.
218+
""" Test of the named_tuple_converter decorator function.
219+
220+
Determine if the decorator function accepts an iterable
221+
argument and returns a namedtuple with the original iterable
222+
data values. Collect the namedtuple field names from an
223+
iterable of names passed an argument. Collect the namedtuple
224+
field names from an iterable of names passed an argument.
221225
222226
Args:
223227
iter_input (Iterable):
@@ -251,9 +255,10 @@ def test_named_tuple_converter_custom_function_auto_name_attributes(
251255

252256

253257
def test_named_tuple_converter_invalid_iterable_exception() -> None:
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.
258+
""" Test of the named_tuple_converter decorator function.
259+
260+
Tests exception handling, when the iterable_input argument
261+
contains a non-iterable object/value.
257262
258263
Args:
259264
None.

0 commit comments

Comments
 (0)