-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCheckers.py
More file actions
136 lines (113 loc) · 4.39 KB
/
Checkers.py
File metadata and controls
136 lines (113 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import FileReader
import re
from collections import Counter
masseges = []
linenumber = 1
def magic_operator(masseges, linenumber):
result = []
operators = ['+', '-', '*', '/', '%']
for line in FileReader.lines:
for operator in operators:
if operator in line:
result = re.findall(".*[0-9]", line)
result += ['contain magic number', 'in line', linenumber]
masseges += [result]
linenumber = linenumber + 1
def magic_operation(masseges, linenumber):
result = []
statements = ["while", "for", "if"]
for line in FileReader.lines:
for operation in statements:
if operation in line:
result = re.findall(rf"{operation}.*<|>|=.*", line)
result += ['contain magic number', 'in line', linenumber]
masseges += [result]
linenumber = linenumber + 1
def print_messages(masseges):
mass_range = list(masseges)
return mass_range
def magic_call(masseges, linenumber):
result = []
parenthesis = ['(']
parenthesis1 = [')']
for line in FileReader.lines:
for n in parenthesis:
result = re.findall(rf".*{parenthesis}[0-9]{parenthesis1}", line)
for x in result:
if str(x) in line:
result += ['contain magic number', 'in line', linenumber]
masseges += [result]
linenumber = linenumber + 1
def is_float(string):
try:
float(string)
return True
except ValueError:
return False
def argument_check():
warnings = []
function_calls = []
functions_names = []
functions = []
used_functions = []
for line in FileReader.lines:
if "def" in line:
try:
functions.append((re.findall(r'def (.+?):', line))[0])
except:
break
for function in functions:
try:
functions_names.append(re.findall(r'(.+?)\(', function)[0])
except:
break
for line in FileReader.lines:
for function in functions_names:
if f'{function}(' in line:
if "def" not in line:
try:
function_calls.append(re.findall(fr'({function}.+?\))', line)[0])
except:
break
for called_function in function_calls:
function_name = re.findall(r'(.+?)\(', called_function)[0]
for function in functions:
if function_name in function:
try:
parameters = re.findall(r'\((.+?)\)', function)[0].replace(" ", "").split(',')
except:
parameters = [""]
try:
arguments = re.findall(r'\((.+?)\)', called_function)[0].replace(" ", "").split(',')
except:
arguments = [""]
if len(arguments) != len(parameters):
warnings.append(f"Invalid arguments for {called_function} at line {(FileReader.lines.index(called_function)) + 1}")
for argument in arguments:
if not argument.isdigit() and not is_float(argument) and "string" in parameters:
continue
if argument.isdigit and "int" in parameters:
continue
if is_float(argument) and "float" in parameters:
continue
else:
warnings.append(f"Invalid arguments for {called_function} at line {(FileReader.lines.index(called_function)) + 1}")
return warnings
def unreachable_code():
lines = list(filter(None, FileReader.lines))
unreachable_lines = []
for line in lines:
if "return" in line or "break" in line or "continue" in line:
spaces_count = len(line) - len(line.strip())
if lines[lines.index(line) + 1].startswith(" " * spaces_count):
unreachable_lines.append(lines[lines.index(line) + 1].strip() + " is unreachable")
return unreachable_lines
def parameter_check():
warnings = []
for line in FileReader.lines:
if "def" in line:
functions = re.findall(r'def (.+?)\:', line)
counter = functions[0].count(',')
if counter > 2:
warnings.append(f'"{functions[0]}" has more than 3 parameters')
return warnings