Passed
Push — codeStyle ( 8e72b5...8a4642 )
by Jan
02:08
created

graph.evaluate   F

Complexity

Total Complexity 100

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Test Coverage

Coverage 99.22%

Importance

Changes 0
Metric Value
wmc 100
eloc 167
dl 0
loc 234
ccs 127
cts 128
cp 0.9922
rs 2
c 0
b 0
f 0

23 Functions

Rating   Name   Duplication   Size   Complexity  
A oval_operator_and() 0 16 5
A error_unknown_eq_noteval_greater_zero() 0 6 4
A and_or_eq_zero() 0 6 3
A eq_zero_unknown_noteval_notappl() 0 6 4
B one_is_unknown() 0 8 6
C oval_operator_xor() 0 24 11
A one_is_false() 0 7 5
A one_is_false1() 0 6 4
A one_is_error() 0 7 5
A greater_zero() 0 4 2
B error_unknown_noteval_for_operators_and_or() 0 14 7
A eq_or_greater_zero() 0 4 2
A eq_or_greater_zero_unknown_noteval_notappl() 0 6 4
A error_unknown_eq_zero() 0 5 3
A eq_or_greater_zero_duo() 0 4 3
A smaller_than_two() 0 4 2
A one_is_noteval() 0 7 5
A eq_zero() 0 4 2
A one_is_true() 0 6 4
A error_unknown_noteval_eq_zero() 0 6 4
A oval_operator_or() 0 16 5
B oval_operator_one() 0 21 7
A eq_zero_duo() 0 4 3

How to fix   Complexity   

Complexity

Complex classes like graph.evaluate often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
"""
2
    Function for evaluate oval operators.
3
"""
4
5
6 1
def oval_operator_and(result):
7
    """
8
        The AND operator produces a true result if every argument is true. If one or more arguments
9
        are false, the result of the AND is false. If one or more of the arguments are unknown, and
10
        if none of the arguments are false, then the AND operator produces a result of unknown.
11
    """
12 1
    out_result = None
13 1
    if eq_zero(result, 'false_cnt')\
14
            and greater_zero(result, 'true_cnt')\
15
            and error_unknown_noteval_eq_zero(result):
16 1
        out_result = 'true'
17 1
    elif greater_zero(result, 'false_cnt'):
18 1
        out_result = 'false'
19
    else:
20 1
        out_result = error_unknown_noteval_for_operators_and_or(result, 'and')
21 1
    return out_result
22
23
24 1
def oval_operator_one(result):
25
    """
26
        The ONE operator produces a true result if one and only one argument is true. If there are
27
        more than argument is true (or if there are no true arguments), the result of the ONE
28
        is false. If one or more of the arguments are unknown, then the ONE operator produces
29
        a result of unknown.
30
    """
31 1
    out_result = None
32 1
    if one_is_true(result):
33 1
        out_result = 'true'
34 1
    elif one_is_false(result) or one_is_false1(result):
35 1
        out_result = 'false'
36 1
    elif one_is_error(result):
37 1
        out_result = 'error'
38 1
    elif one_is_unknown(result):
39 1
        out_result = 'unknown'
40 1
    elif one_is_noteval(result):
41 1
        out_result = 'noteval'
42
    else:
43 1
        out_result = None
44 1
    return out_result
45
46
47 1
def oval_operator_or(result):
48
    """
49
        The OR operator produces a true result if one or more arguments is true. If every argument
50
        is false, the result of the OR is false. If one or more of the arguments are unknown and
51
        if none of arguments are true, then the OR operator produces a result of unknown.
52
    """
53 1
    out_result = None
54 1
    if greater_zero(result, 'true_cnt'):
55 1
        out_result = 'true'
56 1
    elif eq_zero(result, 'true_cnt')\
57
            and greater_zero(result, 'false_cnt')\
58
            and error_unknown_noteval_eq_zero(result):
59 1
        out_result = 'false'
60
    else:
61 1
        out_result = error_unknown_noteval_for_operators_and_or(result, 'or')
62 1
    return out_result
63
64
65 1
def oval_operator_xor(result):
66
    """
67
        XOR is defined to be true if an odd number of its arguments are true, and false otherwise.
68
        If any of the arguments are unknown, then the XOR operator produces a result of unknown.
69
    """
70 1
    out_result = None
71 1
    if (result['true_cnt'] % 2) == 1\
72
            and eq_zero_unknown_noteval_notappl(result):
73 1
        out_result = 'true'
74 1
    elif (result['true_cnt'] % 2) == 0\
75
            and eq_zero_unknown_noteval_notappl(result):
76 1
        out_result = 'false'
77 1
    elif greater_zero(result, 'error_cnt'):
78 1
        out_result = 'error'
79 1
    elif eq_zero(result, 'error_cnt')\
80
            and greater_zero(result, 'unknown_cnt'):
81 1
        out_result = 'unknown'
82 1
    elif eq_zero(result, 'error_cnt')\
83
            and eq_zero(result, 'unknown_cnt')\
84
            and greater_zero(result, 'noteval_cnt'):
85 1
        out_result = 'noteval'
86
    else:
87 1
        out_result = None
88 1
    return out_result
89
90
91 1
def error_unknown_noteval_for_operators_and_or(result, operator):
92 1
    out_result = None
93 1
    if and_or_eq_zero(operator, result)\
94
            and greater_zero(result, 'error_cnt'):
95 1
        out_result = 'error'
96 1
    elif and_or_eq_zero(operator, result)\
97
            and error_unknown_eq_zero(result):
98 1
        out_result = 'unknown'
99 1
    elif and_or_eq_zero(operator, result)\
100
            and error_unknown_eq_noteval_greater_zero(result):
101 1
        out_result = 'noteval'
102
    else:
103 1
        out_result = None
104 1
    return out_result
105
106
107 1
def and_or_eq_zero(operator, result):
108 1
    if operator == 'and':
109 1
        return eq_zero(result, 'false_cnt')
110 1
    if operator == 'or':
111 1
        return eq_zero(result, 'true_cnt')
112 1
    return None
113
114
115 1
def eq_zero(result, cnt):
116 1
    if result[cnt] == 0:
117 1
        return True
118 1
    return False
119
120
121 1
def eq_zero_duo(result, cnt0, cnt1):
122 1
    if result[cnt0] == 0 and result[cnt1] == 0:
123 1
        return True
124
    return False
125
126
127 1
def greater_zero(result, cnt):
128 1
    if result[cnt] > 0:
129 1
        return True
130 1
    return False
131
132
133 1
def eq_or_greater_zero(result, cnt):
134 1
    if result[cnt] >= 0:
135 1
        return True
136 1
    return False
137
138 1
def eq_or_greater_zero_duo(result, cnt0, cnt1):
139 1
    if result[cnt0] >= 0 and result[cnt1] >= 0:
140 1
        return True
141 1
    return False
142
143
144
145 1
def smaller_than_two(result, cnt):
146 1
    if result[cnt] < 2:
147 1
        return True
148 1
    return False
149
150 1
def one_is_noteval(result):
151 1
    if smaller_than_two(result, 'true_cnt')\
152
            and eq_or_greater_zero_duo(result, 'false_cnt','notappl_cnt')\
153
            and eq_zero_duo(result, 'error_cnt', 'unknown_cnt')\
154
            and greater_zero(result, 'noteval_cnt'):
155 1
        return True
156 1
    return False
157
158 1
def one_is_unknown(result):
159 1
    if smaller_than_two(result, 'true_cnt')\
160
            and eq_or_greater_zero(result, 'false_cnt')\
161
            and eq_zero(result, 'error_cnt')\
162
            and result['unknown_cnt'] >= 1\
163
            and eq_or_greater_zero_duo(result, 'noteval_cnt','notappl_cnt'):
164 1
        return True
165 1
    return False
166
167 1
def one_is_error(result):
168 1
    if smaller_than_two(result, 'true_cnt')\
169
            and eq_or_greater_zero(result, 'false_cnt')\
170
            and greater_zero(result, 'error_cnt')\
171
            and eq_or_greater_zero_unknown_noteval_notappl(result):
172 1
        return True
173 1
    return False
174
175 1
def one_is_false(result):
176 1
    if eq_zero(result, 'true_cnt')\
177
            and eq_or_greater_zero(result, 'false_cnt')\
178
            and error_unknown_noteval_eq_zero(result)\
179
            and result['notappl_cnt'] >= 0:
180 1
        return True
181 1
    return False
182
183 1
def one_is_false1(result):
184 1
    if result['true_cnt'] >= 2\
185
            and eq_or_greater_zero_duo(result, 'false_cnt', 'error_cnt')\
186
            and eq_or_greater_zero_unknown_noteval_notappl(result):
187 1
        return True
188 1
    return False
189
190 1
def one_is_true(result):
191 1
    if result['true_cnt'] == 1\
192
            and eq_or_greater_zero_duo(result,'false_cnt','notappl_cnt')\
193
            and error_unknown_noteval_eq_zero(result):
194 1
        return True
195 1
    return False
196
197 1
def eq_or_greater_zero_unknown_noteval_notappl(result):
198 1
    if eq_or_greater_zero(result, 'unknown_cnt')\
199
            and eq_or_greater_zero(result, 'noteval_cnt')\
200
            and eq_or_greater_zero(result, 'notappl_cnt'):
201 1
        return True
202 1
    return False
203
204
205 1
def eq_zero_unknown_noteval_notappl(result):
206 1
    if eq_zero(result, 'error_cnt')\
207
            and eq_zero(result, 'unknown_cnt')\
208
            and eq_zero(result, 'noteval_cnt'):
209 1
        return True
210 1
    return False
211
212
213 1
def error_unknown_noteval_eq_zero(result):
214 1
    if eq_zero(result, 'error_cnt')\
215
            and eq_zero(result, 'unknown_cnt')\
216
            and eq_zero(result, 'noteval_cnt'):
217 1
        return True
218 1
    return False
219
220
221 1
def error_unknown_eq_noteval_greater_zero(result):
222 1
    if eq_zero(result, 'error_cnt')\
223
            and eq_zero(result, 'unknown_cnt')\
224
            and greater_zero(result, 'noteval_cnt'):
225 1
        return True
226 1
    return False
227
228
229 1
def error_unknown_eq_zero(result):
230 1
    if eq_zero(result, 'error_cnt')\
231
            and greater_zero(result, 'unknown_cnt'):
232 1
        return True
233
    return False
234