Skip to content

Commit 2e3bcfc

Browse files
antmarakisnorvig
authored andcommitted
Pytest Warnings Fix + Open Data Files Update (aimacode#547)
* DataFile Update * Update learning.py * Update search.py * Update test_learning.py * Update test_text.py * Add files via upload * Update text.ipynb * Update search-4e.ipynb * Create CONTRIBUTING.md
1 parent 22e571d commit 2e3bcfc

File tree

9 files changed

+34
-34
lines changed

9 files changed

+34
-34
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,21 @@ Running the Test-Suite
7474
=====================
7575

7676
The minimal requirement for running the testsuite is ``py.test``. You can
77-
install it with::
77+
install it with:
7878

7979
pip install pytest
8080

81-
Clone this repository::
81+
Clone this repository:
8282

8383
git clone https://github.com/aimacode/aima-python.git
8484

85-
Fetch the aima-data submodule::
85+
Fetch the aima-data submodule:
8686

8787
cd aima-python
8888
git submodule init
8989
git submodule update
9090

91-
Then you can run the testsuite with::
91+
Then you can run the testsuite from the `tests` directory with:
9292

9393
py.test
9494

learning.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
removeall, unique, product, mode, argmax, argmax_random_tie, isclose, gaussian,
55
dotproduct, vector_add, scalar_vector_product, weighted_sample_with_replacement,
66
weighted_sampler, num_or_str, normalize, clip, sigmoid, print_table,
7-
DataFile, sigmoid_derivative
7+
open_data, sigmoid_derivative
88
)
99

1010
import copy
@@ -95,7 +95,7 @@ def __init__(self, examples=None, attrs=None, attrnames=None, target=-1,
9595
if isinstance(examples, str):
9696
self.examples = parse_csv(examples)
9797
elif examples is None:
98-
self.examples = parse_csv(DataFile(name + '.csv').read())
98+
self.examples = parse_csv(open_data(name + '.csv').read())
9999
else:
100100
self.examples = examples
101101
# Attrs are the indices of examples, unless otherwise stated.
@@ -949,7 +949,7 @@ def cross_validation_wrapper(learner, dataset, k=10, trials=1):
949949
err_val = []
950950
err_train = []
951951
size = 1
952-
952+
953953
while True:
954954
errT, errV = cross_validation(learner, size, dataset, k)
955955
# Check for convergence provided err_val is not empty

search-4e.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@
346346
"outputs": [],
347347
"source": [
348348
"from search import *\n",
349-
"sgb_words = DataFile(\"EN-text/sgb-words.txt\")"
349+
"sgb_words = open_data(\"EN-text/sgb-words.txt\")"
350350
]
351351
},
352352
{

search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from utils import (
88
is_in, argmin, argmax, argmax_random_tie, probability, weighted_sampler,
9-
memoize, print_table, DataFile, Stack, FIFOQueue, PriorityQueue, name,
9+
memoize, print_table, open_data, Stack, FIFOQueue, PriorityQueue, name,
1010
distance
1111
)
1212

@@ -1044,7 +1044,7 @@ class BoggleFinder:
10441044

10451045
def __init__(self, board=None):
10461046
if BoggleFinder.wordlist is None:
1047-
BoggleFinder.wordlist = Wordlist(DataFile("EN-text/wordlist.txt"))
1047+
BoggleFinder.wordlist = Wordlist(open_data("EN-text/wordlist.txt"))
10481048
self.found = {}
10491049
if board:
10501050
self.set_board(board)

tests/pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
filterwarnings =
3+
ignore::ResourceWarning

tests/test_learning.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
import math
33
import random
4-
from utils import DataFile
4+
from utils import open_data
55
from learning import *
66

77

@@ -18,27 +18,31 @@ def test_euclidean():
1818
distance = euclidean_distance([0, 0, 0], [0, 0, 0])
1919
assert distance == 0
2020

21+
2122
def test_rms_error():
2223
assert rms_error([2, 2], [2, 2]) == 0
2324
assert rms_error((0, 0), (0, 1)) == math.sqrt(0.5)
2425
assert rms_error((1, 0), (0, 1)) == 1
2526
assert rms_error((0, 0), (0, -1)) == math.sqrt(0.5)
2627
assert rms_error((0, 0.5), (0, -0.5)) == math.sqrt(0.5)
2728

29+
2830
def test_manhattan_distance():
2931
assert manhattan_distance([2, 2], [2, 2]) == 0
3032
assert manhattan_distance([0, 0], [0, 1]) == 1
3133
assert manhattan_distance([1, 0], [0, 1]) == 2
3234
assert manhattan_distance([0, 0], [0, -1]) == 1
3335
assert manhattan_distance([0, 0.5], [0, -0.5]) == 1
3436

37+
3538
def test_mean_boolean_error():
3639
assert mean_boolean_error([1, 1], [0, 0]) == 1
3740
assert mean_boolean_error([0, 1], [1, 0]) == 1
3841
assert mean_boolean_error([1, 1], [0, 1]) == 0.5
3942
assert mean_boolean_error([0, 0], [0, 0]) == 0
4043
assert mean_boolean_error([1, 1], [1, 1]) == 0
4144

45+
4246
def test_mean_error():
4347
assert mean_error([2, 2], [2, 2]) == 0
4448
assert mean_error([0, 0], [0, 1]) == 0.5
@@ -53,7 +57,7 @@ def test_exclude():
5357

5458

5559
def test_parse_csv():
56-
Iris = DataFile('iris.csv').read()
60+
Iris = open_data('iris.csv').read()
5761
assert parse_csv(Iris)[0] == [5.1, 3.5, 1.4, 0.2, 'setosa']
5862

5963

tests/test_text.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import random
44

55
from text import *
6-
from utils import isclose, DataFile
6+
from utils import isclose, open_data
77

88

99
def test_text_models():
10-
flatland = DataFile("EN-text/flatland.txt").read()
10+
flatland = open_data("EN-text/flatland.txt").read()
1111
wordseq = words(flatland)
1212
P1 = UnigramTextModel(wordseq)
1313
P2 = NgramTextModel(2, wordseq)
@@ -141,7 +141,7 @@ def test_char_models():
141141

142142

143143
def test_viterbi_segmentation():
144-
flatland = DataFile("EN-text/flatland.txt").read()
144+
flatland = open_data("EN-text/flatland.txt").read()
145145
wordseq = words(flatland)
146146
P = UnigramTextModel(wordseq)
147147
text = "itiseasytoreadwordswithoutspaces"
@@ -158,20 +158,20 @@ def test_shift_encoding():
158158

159159

160160
def test_shift_decoding():
161-
flatland = DataFile("EN-text/flatland.txt").read()
161+
flatland = open_data("EN-text/flatland.txt").read()
162162
ring = ShiftDecoder(flatland)
163163
msg = ring.decode('Kyzj zj r jvtivk dvjjrxv.')
164164

165165
assert msg == 'This is a secret message.'
166166

167167

168168
def test_permutation_decoder():
169-
gutenberg = DataFile("EN-text/gutenberg.txt").read()
170-
flatland = DataFile("EN-text/flatland.txt").read()
171-
169+
gutenberg = open_data("EN-text/gutenberg.txt").read()
170+
flatland = open_data("EN-text/flatland.txt").read()
171+
172172
pd = PermutationDecoder(canonicalize(gutenberg))
173173
assert pd.decode('aba') in ('ece', 'ete', 'tat', 'tit', 'txt')
174-
174+
175175
pd = PermutationDecoder(canonicalize(flatland))
176176
assert pd.decode('aba') in ('ded', 'did', 'ece', 'ele', 'eme', 'ere', 'eve', 'eye', 'iti', 'mom', 'ses', 'tat', 'tit')
177177

@@ -183,7 +183,7 @@ def test_rot13_encoding():
183183

184184

185185
def test_rot13_decoding():
186-
flatland = DataFile("EN-text/flatland.txt").read()
186+
flatland = open_data("EN-text/flatland.txt").read()
187187
ring = ShiftDecoder(flatland)
188188
msg = ring.decode(rot13('Hello, world!'))
189189

text.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"outputs": [],
2525
"source": [
2626
"from text import *\n",
27-
"from utils import DataFile"
27+
"from utils import open_data"
2828
]
2929
},
3030
{
@@ -84,7 +84,7 @@
8484
}
8585
],
8686
"source": [
87-
"flatland = DataFile(\"EN-text/flatland.txt\").read()\n",
87+
"flatland = open_data(\"EN-text/flatland.txt\").read()\n",
8888
"wordseq = words(flatland)\n",
8989
"\n",
9090
"P1 = UnigramTextModel(wordseq)\n",
@@ -186,7 +186,7 @@
186186
}
187187
],
188188
"source": [
189-
"flatland = DataFile(\"EN-text/flatland.txt\").read()\n",
189+
"flatland = open_data(\"EN-text/flatland.txt\").read()\n",
190190
"wordseq = words(flatland)\n",
191191
"P = UnigramTextModel(wordseq)\n",
192192
"text = \"itiseasytoreadwordswithoutspaces\"\n",
@@ -358,7 +358,7 @@
358358
}
359359
],
360360
"source": [
361-
"flatland = DataFile(\"EN-text/flatland.txt\").read()\n",
361+
"flatland = open_data(\"EN-text/flatland.txt\").read()\n",
362362
"decoder = ShiftDecoder(flatland)\n",
363363
"\n",
364364
"decoded_message = decoder.decode(ciphertext)\n",

utils.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -383,20 +383,13 @@ def print_table(table, header=None, sep=' ', numfmt='{}'):
383383
str(x), j)(size) for (j, size, x) in zip(justs, sizes, row)))
384384

385385

386-
def AIMAFile(components, mode='r'):
387-
"""Open a file based at the AIMA root directory."""
386+
def open_data(name, mode='r'):
388387
aima_root = os.path.dirname(__file__)
389-
390-
aima_file = os.path.join(aima_root, *components)
388+
aima_file = os.path.join(aima_root, *['aima-data', name])
391389

392390
return open(aima_file)
393391

394392

395-
def DataFile(name, mode='r'):
396-
"Return a file in the AIMA /aima-data directory."
397-
return AIMAFile(['aima-data', name], mode)
398-
399-
400393
# ______________________________________________________________________________
401394
# Expressions
402395

0 commit comments

Comments
 (0)