Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c458ae

Browse files
Style: address pep8 warnings.
1 parent 671fc20 commit 2c458ae

11 files changed

+116
-78
lines changed

tests/test_csp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from csp import * #noqa
33

4+
45
def test_backtracking_search():
56
assert (backtracking_search(australia) is not None) == True
67
assert (backtracking_search(australia, select_unassigned_variable=mrv) is not None) == True
@@ -12,14 +13,15 @@ def test_backtracking_search():
1213
assert (backtracking_search(usa, select_unassigned_variable=mrv,
1314
order_domain_values=lcv, inference=mac) is not None) == True
1415

16+
1517
def test_universal_dict():
1618
d = UniversalDict(42)
1719
assert d['life'] == 42
1820

21+
1922
def test_parse_neighbours():
2023
assert parse_neighbors('X: Y Z; Y: Z') == {'Y': ['X', 'Z'], 'X': ['Y', 'Z'], 'Z': ['X', 'Y']}
2124

2225

23-
2426
if __name__ == "__main__":
2527
pytest.main()

tests/test_games.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def gen_state(to_move='X', x_positions=[], o_positions=[], h=3, v=3, k=3):
1818
and how many consecutive X's or O's required to win, return the corresponding
1919
game state"""
2020

21-
moves = set([(x, y) for x in range(1, h+1) for y in range(1, v+1)]) \
21+
moves = set([(x, y) for x in range(1, h + 1) for y in range(1, v + 1)]) \
2222
- set(x_positions) - set(o_positions)
2323
moves = list(moves)
2424
board = {}

tests/test_learning.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import pytest
22
from learning import parse_csv, weighted_mode, weighted_replicate
33

4+
45
def test_parse_csv():
56
assert parse_csv('1, 2, 3 \n 0, 2, na') == [[1, 2, 3], [0, 2, 'na']]
67

78

89
def test_weighted_mode():
9-
assert weighted_mode('abbaa', [1,2,3,1,2]) == 'b'
10+
assert weighted_mode('abbaa', [1, 2, 3, 1, 2]) == 'b'
1011

1112

1213
def test_weighted_replicate():
13-
assert weighted_replicate('ABC', [1,2,1], 4) == ['A', 'B', 'B', 'C']
14+
assert weighted_replicate('ABC', [1, 2, 1], 4) == ['A', 'B', 'B', 'C']

tests/test_logic.py

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ def test_expr():
99
assert (expr_handle_infix_ops('P & Q ==> R & ~S')
1010
== "P & Q |'==>'| R & ~S")
1111

12+
1213
def test_extend():
1314
assert extend({x: 1}, y, 2) == {x: 1, y: 2}
1415

16+
1517
def test_PropKB():
1618
kb = PropKB()
1719
assert count(kb.ask(expr) for expr in [A, C, D, E, Q]) is 0
@@ -33,44 +35,44 @@ def test_KB_wumpus():
3335
# TODO: Let's just use P11, P12, ... = symbols('P11, P12, ...')
3436
P = {}
3537
B = {}
36-
P[1,1] = Symbol("P[1,1]")
37-
P[1,2] = Symbol("P[1,2]")
38-
P[2,1] = Symbol("P[2,1]")
39-
P[2,2] = Symbol("P[2,2]")
40-
P[3,1] = Symbol("P[3,1]")
41-
B[1,1] = Symbol("B[1,1]")
42-
B[2,1] = Symbol("B[2,1]")
43-
44-
kb_wumpus.tell(~P[1,1])
45-
kb_wumpus.tell(B[1,1] |'<=>'| ((P[1,2] | P[2,1])))
46-
kb_wumpus.tell(B[2,1] |'<=>'| ((P[1,1] | P[2,2] | P[3,1])))
47-
kb_wumpus.tell(~B[1,1])
48-
kb_wumpus.tell(B[2,1])
38+
P[1, 1] = Symbol("P[1,1]")
39+
P[1, 2] = Symbol("P[1,2]")
40+
P[2, 1] = Symbol("P[2,1]")
41+
P[2, 2] = Symbol("P[2,2]")
42+
P[3, 1] = Symbol("P[3,1]")
43+
B[1, 1] = Symbol("B[1,1]")
44+
B[2, 1] = Symbol("B[2,1]")
45+
46+
kb_wumpus.tell(~P[1, 1])
47+
kb_wumpus.tell(B[1, 1] | '<=>' | ((P[1, 2] | P[2, 1])))
48+
kb_wumpus.tell(B[2, 1] | '<=>' | ((P[1, 1] | P[2, 2] | P[3, 1])))
49+
kb_wumpus.tell(~B[1, 1])
50+
kb_wumpus.tell(B[2, 1])
4951

5052
# Statement: There is no pit in [1,1].
51-
assert kb_wumpus.ask(~P[1,1]) == {}
53+
assert kb_wumpus.ask(~P[1, 1]) == {}
5254

5355
# Statement: There is no pit in [1,2].
54-
assert kb_wumpus.ask(~P[1,2]) == {}
56+
assert kb_wumpus.ask(~P[1, 2]) == {}
5557

5658
# Statement: There is a pit in [2,2].
57-
assert kb_wumpus.ask(P[2,2]) == False
59+
assert kb_wumpus.ask(P[2, 2]) == False
5860

5961
# Statement: There is a pit in [3,1].
60-
assert kb_wumpus.ask(P[3,1]) == False
62+
assert kb_wumpus.ask(P[3, 1]) == False
6163

6264
# Statement: Neither [1,2] nor [2,1] contains a pit.
63-
assert kb_wumpus.ask(~P[1,2] & ~P[2,1]) == {}
65+
assert kb_wumpus.ask(~P[1, 2] & ~P[2, 1]) == {}
6466

6567
# Statement: There is a pit in either [2,2] or [3,1].
66-
assert kb_wumpus.ask(P[2,2] | P[3,1]) == {}
68+
assert kb_wumpus.ask(P[2, 2] | P[3, 1]) == {}
6769

6870

6971
def test_definite_clause():
70-
assert is_definite_clause(expr('A & B & C & D ==> E'))
71-
assert is_definite_clause(expr('Farmer(Mac)'))
72+
assert is_definite_clause(expr('A & B & C & D ==> E'))
73+
assert is_definite_clause(expr('Farmer(Mac)'))
7274
assert not is_definite_clause(expr('~Farmer(Mac)'))
73-
assert is_definite_clause(expr('(Farmer(f) & Rabbit(r)) ==> Hates(f, r)'))
75+
assert is_definite_clause(expr('(Farmer(f) & Rabbit(r)) ==> Hates(f, r)'))
7476
assert not is_definite_clause(expr('(Farmer(f) & ~Rabbit(r)) ==> Hates(f, r)'))
7577
assert not is_definite_clause(expr('(Farmer(f) | Rabbit(r)) ==> Hates(f, r)'))
7678

@@ -79,12 +81,13 @@ def test_pl_true():
7981
assert pl_true(P, {}) is None
8082
assert pl_true(P, {P: False}) is False
8183
assert pl_true(P | Q, {P: True}) is True
82-
assert pl_true((A|B)&(C|D), {A: False, B: True, D: True}) is True
83-
assert pl_true((A&B)&(C|D), {A: False, B: True, D: True}) is False
84-
assert pl_true((A&B)|(A&C), {A: False, B: True, C: True}) is False
85-
assert pl_true((A|B)&(C|D), {A: True, D: False}) is None
84+
assert pl_true((A | B) & (C | D), {A: False, B: True, D: True}) is True
85+
assert pl_true((A & B) & (C | D), {A: False, B: True, D: True}) is False
86+
assert pl_true((A & B) | (A & C), {A: False, B: True, C: True}) is False
87+
assert pl_true((A | B) & (C | D), {A: True, D: False}) is None
8688
assert pl_true(P | P, {}) is None
8789

90+
8891
def test_tt_true():
8992
assert tt_true(P | ~P)
9093
assert tt_true('~~P <=> P')
@@ -103,48 +106,56 @@ def test_tt_true():
103106
assert tt_true('(A & (B | C)) <=> ((A & B) | (A & C))')
104107
assert tt_true('(A | (B & C)) <=> ((A | B) & (A | C))')
105108

109+
106110
def test_dpll():
107111
assert (dpll_satisfiable(A & ~B & C & (A | ~D) & (~E | ~D) & (C | ~D) & (~A | ~F) & (E | ~F)
108112
& (~D | ~F) & (B | ~C | D) & (A | ~E | F) & (~A | E | D))
109113
== {B: False, C: True, A: True, F: False, D: True, E: False})
110-
assert dpll_satisfiable(A&~B) == {A: True, B: False}
111-
assert dpll_satisfiable(P&~P) == False
114+
assert dpll_satisfiable(A & ~B) == {A: True, B: False}
115+
assert dpll_satisfiable(P & ~P) == False
112116

113117

114118
def test_unify():
115119
assert unify(x, x, {}) == {}
116120
assert unify(x, 3, {}) == {x: 3}
117121

122+
118123
def test_pl_fc_entails():
119124
assert pl_fc_entails(horn_clauses_KB, expr('Q'))
120125
assert not pl_fc_entails(horn_clauses_KB, expr('SomethingSilly'))
121126

127+
122128
def test_tt_entails():
123129
assert tt_entails(P & Q, Q)
124130
assert not tt_entails(P | Q, Q)
125131
assert tt_entails(A & (B | C) & E & F & ~(P | Q), A & E & F & ~P & ~Q)
126132

133+
127134
def test_eliminate_implications():
128135
assert repr(eliminate_implications('A ==> (~B <== C)')) == '((~B | ~C) | ~A)'
129136
assert repr(eliminate_implications(A ^ B)) == '((A & ~B) | (~A & B))'
130137
assert repr(eliminate_implications(A & B | C & ~D)) == '((A & B) | (C & ~D))'
131138

139+
132140
def test_dissociate():
133141
assert dissociate('&', [A & B]) == [A, B]
134142
assert dissociate('|', [A, B, C & D, P | Q]) == [A, B, C & D, P, Q]
135143
assert dissociate('&', [A, B, C & D, P | Q]) == [A, B, C, D, P | Q]
136144

145+
137146
def test_associate():
138147
assert (repr(associate('&', [(A & B), (B | C), (B & C)]))
139148
== '(A & B & (B | C) & B & C)')
140149
assert (repr(associate('|', [A | (B | (C | (A & B)))]))
141150
== '(A | B | C | (A & B))')
142151

152+
143153
def test_move_not_inwards():
144154
assert repr(move_not_inwards(~(A | B))) == '(~A & ~B)'
145155
assert repr(move_not_inwards(~(A & B))) == '(~A | ~B)'
146156
assert repr(move_not_inwards(~(~(A | ~B) | ~~C))) == '((A | ~B) & ~C)'
147157

158+
148159
def test_to_cnf():
149160
assert (repr(to_cnf(wumpus_world_inference & ~expr('~P12'))) ==
150161
"((~P12 | B11) & (~P21 | B11) & (P12 | P21 | ~B11) & ~B11 & P12)")
@@ -154,36 +165,40 @@ def test_to_cnf():
154165
assert repr(to_cnf("A & (B | (D & E))")) == '(A & (D | B) & (E | B))'
155166
assert repr(to_cnf("A | (B | (C | (D & E)))")) == '((D | A | B | C) & (E | A | B | C))'
156167

168+
157169
def test_standardize_variables():
158170
e = expr('F(a, b, c) & G(c, A, 23)')
159171
assert len(variables(standardize_variables(e))) == 3
160172
#assert variables(e).intersection(variables(standardize_variables(e))) == {}
161173
assert is_variable(standardize_variables(expr('x')))
162174

175+
163176
def test_fol_bc_ask():
164177
def test_ask(query, kb=None):
165178
q = expr(query)
166179
test_variables = variables(q)
167180
answers = fol_bc_ask(kb or test_kb, q)
168181
return sorted(
169182
[dict((x, v) for x, v in list(a.items()) if x in test_variables)
170-
for a in answers], key=repr)
183+
for a in answers], key=repr)
171184
assert repr(test_ask('Farmer(x)')) == '[{x: Mac}]'
172185
assert repr(test_ask('Human(x)')) == '[{x: Mac}, {x: MrsMac}]'
173186
assert repr(test_ask('Rabbit(x)')) == '[{x: MrsRabbit}, {x: Pete}]'
174187
assert repr(test_ask('Criminal(x)', crime_kb)) == '[{x: West}]'
175188

189+
176190
def test_d():
177-
assert d(x*x - x, x) == 2*x - 1
191+
assert d(x * x - x, x) == 2 * x - 1
192+
178193

179194
def test_WalkSAT():
180-
def check_SAT(clauses, single_solution = {}):
195+
def check_SAT(clauses, single_solution={}):
181196
# Make sure the solution is correct if it is returned by WalkSat
182197
# Sometimes WalkSat may run out of flips before finding a solution
183198
soln = WalkSAT(clauses)
184199
if soln:
185200
assert all(pl_true(x, soln) for x in clauses)
186-
if single_solution: #Cross check the solution if only one exists
201+
if single_solution: # Cross check the solution if only one exists
187202
assert all(pl_true(x, single_solution) for x in clauses)
188203
assert soln == single_solution
189204
# Test WalkSat for problems with solution
@@ -195,18 +210,19 @@ def check_SAT(clauses, single_solution = {}):
195210
assert WalkSAT([A | B, ~A, ~(B | C), C | D, P | Q], 0.5, 100) is None
196211
assert WalkSAT([A | B, B & C, C | D, D & A, P, ~P], 0.5, 100) is None
197212

213+
198214
def test_SAT_plan():
199-
transition = {'A':{'Left': 'A', 'Right': 'B'},
200-
'B':{'Left': 'A', 'Right': 'C'},
201-
'C':{'Left': 'B', 'Right': 'C'}}
215+
transition = {'A': {'Left': 'A', 'Right': 'B'},
216+
'B': {'Left': 'A', 'Right': 'C'},
217+
'C': {'Left': 'B', 'Right': 'C'}}
202218
assert SAT_plan('A', transition, 'C', 2) is None
203219
assert SAT_plan('A', transition, 'B', 3) == ['Right']
204220
assert SAT_plan('C', transition, 'A', 3) == ['Left', 'Left']
205221

206-
transition = {(0, 0):{'Right': (0, 1), 'Down': (1, 0)},
207-
(0, 1):{'Left': (1, 0), 'Down': (1, 1)},
208-
(1, 0):{'Right': (1, 0), 'Up': (1, 0), 'Left': (1, 0), 'Down': (1, 0)},
209-
(1, 1):{'Left': (1, 0), 'Up': (0, 1)}}
222+
transition = {(0, 0): {'Right': (0, 1), 'Down': (1, 0)},
223+
(0, 1): {'Left': (1, 0), 'Down': (1, 1)},
224+
(1, 0): {'Right': (1, 0), 'Up': (1, 0), 'Left': (1, 0), 'Down': (1, 0)},
225+
(1, 1): {'Left': (1, 0), 'Up': (0, 1)}}
210226
assert SAT_plan((0, 0), transition, (1, 1), 4) == ['Right', 'Down']
211227

212228

tests/test_mdp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from mdp import * # noqa
33

4+
45
def test_value_iteration():
56
assert value_iteration(sequential_decision_environment, .01) == {(3, 2): 1.0, (3, 1): -1.0,
67
(3, 0): 0.12958868267972745, (0, 1): 0.39810203830605462,

tests/test_nlp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import pytest
22
from nlp import *
33

4+
45
def test_rules():
5-
assert Rules(A = "B C | D E") == {'A': [['B', 'C'], ['D', 'E']]}
6+
assert Rules(A="B C | D E") == {'A': [['B', 'C'], ['D', 'E']]}
67

78

89
def test_lexicon():
9-
assert Lexicon(Art = "the | a | an") == {'Art': ['the', 'a', 'an']}
10+
assert Lexicon(Art="the | a | an") == {'Art': ['the', 'a', 'an']}

tests/test_planning.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from utils import expr
33
from logic import FolKB
44

5+
56
def test_action():
67
precond = [[expr("P(x)"), expr("Q(y, z)")]
78
,[expr("Q(x)")]]
@@ -18,15 +19,16 @@ def test_action():
1819
assert test_kb.ask(expr("Q(B, C)")) is not False
1920
assert not a.check_precond(test_kb, args)
2021

22+
2123
def test_air_cargo():
2224
p = air_cargo()
2325
assert p.goal_test() is False
24-
solution =[expr("Load(C1 , P1, SFO)"),
25-
expr("Fly(P1, SFO, JFK)"),
26-
expr("Unload(C1, P1, JFK)"),
27-
expr("Load(C2, P2, JFK)"),
28-
expr("Fly(P2, JFK, SFO)"),
29-
expr("Unload (C2, P2, SFO)")]
26+
solution = [expr("Load(C1 , P1, SFO)"),
27+
expr("Fly(P1, SFO, JFK)"),
28+
expr("Unload(C1, P1, JFK)"),
29+
expr("Load(C2, P2, JFK)"),
30+
expr("Fly(P2, JFK, SFO)"),
31+
expr("Unload (C2, P2, SFO)")]
3032

3133
for action in solution:
3234
p.act(action)

tests/test_probability.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def test_forward_backward():
119119
assert rounder(forward_backward(umbrellaHMM, umbrella_evidence, umbrella_prior)) == [[0.5871, 0.4129],
120120
[0.7177, 0.2823], [0.2324, 0.7676], [0.6072, 0.3928], [0.2324, 0.7676], [0.7177, 0.2823]]
121121

122+
122123
def test_fixed_lag_smoothing():
123124
umbrella_evidence = [T, F, T, F, T]
124125
e_t = F

tests/test_search.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,27 @@ def test_depth_first_graph_search():
2323
solution = depth_first_graph_search(romania_problem).solution()
2424
assert solution[-1] == 'Bucharest'
2525

26+
2627
def test_iterative_deepening_search():
2728
assert iterative_deepening_search(romania_problem).solution() == ['Sibiu', 'Fagaras', 'Bucharest']
2829

30+
2931
def test_depth_limited_search():
3032
solution_3 = depth_limited_search(romania_problem, 3).solution()
3133
assert solution_3[-1] == 'Bucharest'
3234
assert depth_limited_search(romania_problem, 2) == 'cutoff'
3335
solution_50 = depth_limited_search(romania_problem).solution()
3436
assert solution_50[-1] == 'Bucharest'
3537

38+
3639
def test_astar_search():
3740
assert astar_search(romania_problem).solution() == ['Sibiu', 'Rimnicu', 'Pitesti', 'Bucharest']
3841

42+
3943
def test_recursive_best_first_search():
4044
assert recursive_best_first_search(romania_problem).solution() == ['Sibiu', 'Rimnicu', 'Pitesti', 'Bucharest']
4145

46+
4247
def test_BoggleFinder():
4348
board = list('SARTELNID')
4449
"""
@@ -50,6 +55,7 @@ def test_BoggleFinder():
5055
f = BoggleFinder(board)
5156
assert len(f) == 206
5257

58+
5359
def test_and_or_graph_search():
5460
def run_plan(state, problem, plan):
5561
if problem.goal_test(state):
@@ -61,6 +67,7 @@ def run_plan(state, problem, plan):
6167
plan = and_or_graph_search(vacumm_world)
6268
assert run_plan('State_1', vacumm_world, plan)
6369

70+
6471
def test_LRTAStarAgent():
6572
my_agent = LRTAStarAgent(LRTA_problem)
6673
assert my_agent('State_3') == 'Right'
@@ -104,7 +111,7 @@ def test_LRTAStarAgent():
104111
105112
>>> boggle_hill_climbing(list('ABCDEFGHI'), verbose=False)
106113
(['E', 'P', 'R', 'D', 'O', 'A', 'G', 'S', 'T'], 123)
107-
"""
114+
"""
108115

109116
if __name__ == '__main__':
110117
pytest.main()

0 commit comments

Comments
 (0)