Skip to content

Commit b160012

Browse files
authored
Merge pull request #17 from IloBe/16-correction-of-linter-messages
Style: Correct linter messages #16
2 parents 911a4ec + 8555387 commit b160012

17 files changed

Lines changed: 274 additions & 272 deletions

sanitycheck.py

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
#!/usr/bin/env -S python3 -i
22

3-
from os import path
4-
53
import argparse
64
import importlib
75
import inspect
8-
import os
96
import sys
7+
import os
8+
from os import path
109

1110
FAIL_COLOR = '\033[91m'
1211
OK_COLOR = '\033[92m'
1312
WARN_COLOR = '\033[93m'
1413

14+
1515
def run_sanity_check(test_dir):
16+
''' Udacity file to check students test cases '''
1617

17-
#assert path.isdir(test_dir), FAIL_COLOR+f"No direcotry named {test_dir} found in {os.getcwd()}"
18+
assert path.isdir(test_dir), FAIL_COLOR+f"No directory named {test_dir} found in {os.getcwd()}"
1819
print('This script will perform a sanity test to ensure your code meets the criteria in the rubric.\n')
1920
print('Please enter the path to the file that contains your test cases for the GET() and POST() methods')
2021
print('The path should be something like abc/def/test_xyz.py')
@@ -26,98 +27,93 @@ def run_sanity_check(test_dir):
2627
module_name = path.splitext(path.basename(filepath))[0]
2728
module = importlib.import_module(module_name)
2829

29-
30-
test_function_names = list(filter(lambda x: inspect.isfunction(getattr(module,x)) and not x.startswith('__'), dir(module)))
31-
32-
test_functions_for_get = list(filter(lambda x: inspect.getsource(getattr(module,x)).find('.get(') != -1 , test_function_names))
33-
test_functions_for_post = list(filter(lambda x: inspect.getsource(getattr(module,x)).find('.post(') != -1, test_function_names))
34-
30+
test_function_names = list(filter(
31+
lambda x: inspect.isfunction(getattr(module, x)) and not x.startswith('__'), dir(module)))
32+
test_functions_for_get = list(filter(
33+
lambda x: inspect.getsource(getattr(module, x)).find('.get(') != -1, test_function_names))
34+
test_functions_for_post = list(filter(
35+
lambda x: inspect.getsource(getattr(module, x)).find('.post(') != -1, test_function_names))
3536

3637
print("\n============= Sanity Check Report ===========")
3738
SANITY_TEST_PASSING = True
3839
WARNING_COUNT = 1
3940

40-
## GET()
41+
# GET()
4142
TEST_FOR_GET_METHOD_RESPONSE_CODE = False
4243
TEST_FOR_GET_METHOD_RESPONSE_BODY = False
4344
if not test_functions_for_get:
44-
print(FAIL_COLOR+f"[{WARNING_COUNT}]")
45+
print(FAIL_COLOR + f"[{WARNING_COUNT}]")
4546
WARNING_COUNT += 1
46-
print(FAIL_COLOR+"No test cases were detected for the GET() method.")
47-
print(FAIL_COLOR+"\nPlease make sure you have a test case for the GET method.\
47+
print(FAIL_COLOR + "No test cases were detected for the GET() method.")
48+
print(FAIL_COLOR + "\nPlease make sure you have a test case for the GET method.\
4849
This MUST test both the status code as well as the contents of the request object.\n")
4950
SANITY_TEST_PASSING = False
50-
5151
else:
5252
for func in test_functions_for_get:
53-
source = inspect.getsource(getattr(module,func))
53+
source = inspect.getsource(getattr(module, func))
5454
if source.find('.status_code') != -1:
5555
TEST_FOR_GET_METHOD_RESPONSE_CODE = True
5656
if (source.find('.json') != -1) or (source.find('json.loads') != -1):
57-
TEST_FOR_GET_METHOD_RESPONSE_BODY = True
58-
57+
TEST_FOR_GET_METHOD_RESPONSE_BODY = True
5958

6059
if not TEST_FOR_GET_METHOD_RESPONSE_CODE:
61-
print(FAIL_COLOR+f"[{WARNING_COUNT}]")
60+
print(FAIL_COLOR + f"[{WARNING_COUNT}]")
6261
WARNING_COUNT += 1
63-
print(FAIL_COLOR+\
62+
print(FAIL_COLOR +
6463
"Your test case for GET() does not seem to be testing the response code.\n")
65-
64+
6665
if not TEST_FOR_GET_METHOD_RESPONSE_BODY:
67-
print(FAIL_COLOR+f"[{WARNING_COUNT}]")
66+
print(FAIL_COLOR + f"[{WARNING_COUNT}]")
6867
WARNING_COUNT += 1
69-
print(FAIL_COLOR+\
68+
print(FAIL_COLOR +
7069
"Your test case for GET() does not seem to be testing the CONTENTS of the response.\n")
7170

72-
73-
74-
## POST()
71+
# POST()
7572
TEST_FOR_POST_METHOD_RESPONSE_CODE = False
7673
TEST_FOR_POST_METHOD_RESPONSE_BODY = False
7774
COUNT_POST_METHOD_TEST_FOR_INFERENCE_RESULT = 0
7875

7976
if not test_functions_for_post:
80-
print(FAIL_COLOR+f"[{WARNING_COUNT}]")
77+
print(FAIL_COLOR + f"[{WARNING_COUNT}]")
8178
WARNING_COUNT += 1
82-
print(FAIL_COLOR+"No test cases were detected for the POST() method.")
83-
print(FAIL_COLOR+"Please make sure you have TWO test cases for the POST() method."+
84-
"\nOne test case for EACH of the possible inferences (results/outputs) of the ML model.\n")
79+
print(FAIL_COLOR + "No test cases were detected for the POST() method.")
80+
print(FAIL_COLOR + "Please make sure you have TWO test cases for the POST() method." +
81+
"\nOne test case for EACH of the possible inferences (results/outputs) of the ML model.\n")
8582
SANITY_TEST_PASSING = False
8683
else:
8784
if len(test_functions_for_post) == 1:
8885
print(f"[{WARNING_COUNT}]")
8986
WARNING_COUNT += 1
90-
print(FAIL_COLOR+"Only one test case was detected for the POST() method.")
91-
print(FAIL_COLOR+"Please make sure you have two test cases for the POST() method."+
92-
"\nOne test case for EACH of the possible inferences (results/outputs) of the ML model.\n")
87+
print(FAIL_COLOR + "Only one test case was detected for the POST() method.")
88+
print(FAIL_COLOR + "Please make sure you have two test cases for the POST() method." +
89+
"\nOne test case for EACH of the possible inferences (results/outputs) of the ML model.\n")
9390
SANITY_TEST_PASSING = False
9491

9592
for func in test_functions_for_post:
96-
source = inspect.getsource(getattr(module,func))
93+
source = inspect.getsource(getattr(module, func))
9794
if source.find('.status_code') != -1:
9895
TEST_FOR_POST_METHOD_RESPONSE_CODE = True
9996
if (source.find('.json') != -1) or (source.find('json.loads') != -1):
100-
TEST_FOR_POST_METHOD_RESPONSE_BODY = True
97+
TEST_FOR_POST_METHOD_RESPONSE_BODY = True
10198
COUNT_POST_METHOD_TEST_FOR_INFERENCE_RESULT += 1
10299

103100
if not TEST_FOR_POST_METHOD_RESPONSE_CODE:
104-
print(FAIL_COLOR+f"[{WARNING_COUNT}]")
101+
print(FAIL_COLOR + f"[{WARNING_COUNT}]")
105102
WARNING_COUNT += 1
106-
print(FAIL_COLOR+\
103+
print(FAIL_COLOR +
107104
"One or more of your test cases for POST() do not seem to be testing the response code.\n")
105+
108106
if not TEST_FOR_POST_METHOD_RESPONSE_BODY:
109-
print(FAIL_COLOR+f"[{WARNING_COUNT}]")
107+
print(FAIL_COLOR + f"[{WARNING_COUNT}]")
110108
WARNING_COUNT += 1
111-
print(FAIL_COLOR+\
109+
print(FAIL_COLOR +
112110
"One or more of your test cases for POST() do not seem to be testing the contents of the response.\n")
113111

114112
if len(test_functions_for_post) >= 2 and COUNT_POST_METHOD_TEST_FOR_INFERENCE_RESULT < 2:
115-
print(FAIL_COLOR+f"[{WARNING_COUNT}]")
113+
print(FAIL_COLOR + f"[{WARNING_COUNT}]")
116114
WARNING_COUNT += 1
117-
print(FAIL_COLOR+\
118-
"You do not seem to have TWO separate test cases, one for each possible prediction that your model can make.")
119-
120-
115+
print(FAIL_COLOR + "You do not seem to have TWO separate test cases, "
116+
"one for each possible prediction that your model can make.")
121117

122118
SANITY_TEST_PASSING = SANITY_TEST_PASSING and\
123119
TEST_FOR_GET_METHOD_RESPONSE_CODE and \
@@ -127,11 +123,10 @@ def run_sanity_check(test_dir):
127123
COUNT_POST_METHOD_TEST_FOR_INFERENCE_RESULT >= 2
128124

129125
if SANITY_TEST_PASSING:
130-
print(OK_COLOR+"Your test cases look good!")
131-
132-
print(WARN_COLOR+"This is a heuristic based sanity testing and cannot guarantee the correctness of your code.")
133-
print(WARN_COLOR+"You should still check your work against the rubric to ensure you meet the criteria.")
126+
print(OK_COLOR + "Your test cases look good!")
134127

128+
print(WARN_COLOR + "This is a heuristic based sanity testing and cannot guarantee the correctness of your code.")
129+
print(WARN_COLOR + "You should still check your work against the rubric to ensure you meet the criteria.")
135130

136131

137132
if __name__ == "__main__":
@@ -143,4 +138,3 @@ def run_sanity_check(test_dir):
143138
help='Name of the directory that has test files.')
144139
args = parser.parse_args()
145140
run_sanity_check(args.test_dir)
146-

src/__init__.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
# a user can manipulate configuration from project libraries like any other Python object
2-
3-
print(f'Invoking __init__.py for {__name__}')
4-
1+
import logging
2+
from logging import Formatter, NullHandler
3+
from colorama import Fore, Style
54
from pkgutil import extend_path
65
__path__ = extend_path(__path__, __name__)
6+
print(f'Invoking __init__.py for {__name__}')
77
print(f'src package: __path__ : {__path__}')
88

9-
from src import app, config, training
10-
11-
import logging
12-
from logging import Formatter, NullHandler
13-
import colorama
14-
from colorama import Back, Fore, Style
159

1610
COLORS = {"DEBUG": Fore.BLUE,
1711
"INFO": Fore.BLACK,
1812
"WARNING": Fore.YELLOW,
1913
"ERROR": Fore.RED,
2014
"CRITICAL": Fore.MAGENTA}
2115

16+
2217
class CustomColoredFormatter(Formatter):
2318
def __init__(self, *, format, use_color):
19+
''' Initialise customized formatter class '''
2420
Formatter.__init__(self, fmt=format)
2521
self.use_color = use_color
2622

2723
def format(self, record):
24+
''' Sets message colour according log level '''
2825
msg = super().format(record)
2926
if self.use_color:
3027
levelname = record.levelname
@@ -34,9 +31,9 @@ def format(self, record):
3431
return f"{COLORS[levelname]}{msg}{Style.RESET_ALL}"
3532
return msg
3633

34+
3735
logging.getLogger(__name__).addHandler(NullHandler())
3836

3937
__version__ = '0.1.0'
4038
__author__ = 'Ilona Brinkmeier'
4139
__license__ = 'MIT'
42-

src/app/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
import logging
44
from logging import Formatter, NullHandler
5-
import colorama
6-
from colorama import Back, Fore, Style
5+
from colorama import Fore, Style
76

87
COLORS = {"DEBUG": Fore.BLUE,
98
"INFO": Fore.BLACK,
109
"WARNING": Fore.YELLOW,
1110
"ERROR": Fore.RED,
1211
"CRITICAL": Fore.MAGENTA}
1312

13+
1414
class CustomColoredFormatter(Formatter):
1515
def __init__(self, *, format, use_color):
16+
''' Initialise customized formatter class '''
1617
Formatter.__init__(self, fmt=format)
1718
self.use_color = use_color
1819

1920
def format(self, record):
21+
''' Sets message colour according log level '''
2022
msg = super().format(record)
2123
if self.use_color:
2224
levelname = record.levelname
@@ -26,5 +28,5 @@ def format(self, record):
2628
return f"{COLORS[levelname]}{msg}{Style.RESET_ALL}"
2729
return msg
2830

29-
31+
3032
logging.getLogger(__name__).addHandler(NullHandler())

src/config/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
# a user can manipulate configuration from project libraries like any other Python object
2-
print(f'Invoking __init__.py for {__name__}')
3-
4-
from src.config.config import get_config, get_data_path, get_models_path, get_project_root_path
52

63
import logging
74
from logging import Formatter, NullHandler
8-
import colorama
9-
from colorama import Back, Fore, Style
5+
from colorama import Fore, Style
6+
7+
8+
print(f'Invoking __init__.py for {__name__}')
109

1110
COLORS = {"DEBUG": Fore.BLUE,
1211
"INFO": Fore.BLACK,
1312
"WARNING": Fore.YELLOW,
1413
"ERROR": Fore.RED,
1514
"CRITICAL": Fore.MAGENTA}
1615

16+
1717
class CustomColoredFormatter(Formatter):
1818
def __init__(self, *, format, use_color):
19+
''' Initialise customized formatter class '''
1920
Formatter.__init__(self, fmt=format)
2021
self.use_color = use_color
2122

2223
def format(self, record):
24+
''' Sets message colour according log level '''
2325
msg = super().format(record)
2426
if self.use_color:
2527
levelname = record.levelname
@@ -29,5 +31,6 @@ def format(self, record):
2931
return f"{COLORS[levelname]}{msg}{Style.RESET_ALL}"
3032
return msg
3133

34+
3235
logging.getLogger(__name__).addHandler(NullHandler())
33-
config_dict={}
36+
config_dict = {}

src/config/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
###################
1010
# Imports
1111
###################
12-
import numpy as np
1312
import logging
1413
import yaml
1514
import os
@@ -23,6 +22,7 @@
2322
# info see: https://realpython.com/python-logging-source-code/
2423
logger = logging.getLogger(__name__)
2524

25+
2626
def get_project_root_path():
2727
''' Returns the absolute path to projects root. '''
2828
return os.getcwd()
@@ -41,13 +41,13 @@ def create_config():
4141
logger.info('Configuration yml file content is:\n %s', config_dict)
4242
return config_dict
4343
except yaml.YAMLError:
44-
logger.exception(f"Exit: Error parsing config.yml in config.__init__.py.")
44+
logger.exception("Exit: Error parsing config.yml in config.__init__.py.")
4545
sys.exit(1)
4646
else:
4747
logger.exception("Exit: Configuration file does not exist on path: %s", CONFIG_FILE)
4848
sys.exit(1)
4949

50-
50+
5151
def get_config():
5252
''' Returns dictionary about project configuration. '''
5353
# future toDo: create config class with _init_ to have config file once
@@ -64,7 +64,7 @@ def get_data_path():
6464
logger.info("config data_path: %s", data_path)
6565
return data_path
6666

67-
67+
6868
def get_models_path():
6969
''' Returns the models directory path. '''
7070
ROOT = get_project_root_path()

src/eda/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
import logging
44
from logging import Formatter, NullHandler
5-
import colorama
6-
from colorama import Back, Fore, Style
5+
from colorama import Fore, Style
76

87
COLORS = {"DEBUG": Fore.BLUE,
98
"INFO": Fore.BLACK,
109
"WARNING": Fore.YELLOW,
1110
"ERROR": Fore.RED,
1211
"CRITICAL": Fore.MAGENTA}
1312

13+
1414
class CustomColoredFormatter(Formatter):
1515
def __init__(self, *, format, use_color):
16+
''' Initialise customized formatter class '''
1617
Formatter.__init__(self, fmt=format)
1718
self.use_color = use_color
1819

1920
def format(self, record):
21+
''' Sets message colour according log level '''
2022
msg = super().format(record)
2123
if self.use_color:
2224
levelname = record.levelname
@@ -26,5 +28,5 @@ def format(self, record):
2628
return f"{COLORS[levelname]}{msg}{Style.RESET_ALL}"
2729
return msg
2830

29-
30-
logging.getLogger(__name__).addHandler(NullHandler())
31+
32+
logging.getLogger(__name__).addHandler(NullHandler())

src/main.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
# Imports
1111
###################
1212

13-
import os
14-
import numpy as np
1513
import logging
1614
import uvicorn
1715

@@ -31,4 +29,4 @@
3129

3230

3331
if __name__ == "__main__":
34-
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
32+
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)

0 commit comments

Comments
 (0)