Please refer to LearningPythonTheHardWay exercise48 for more details on the purpose of these code. I am still new, I know there are a lot to be improved or better ways to do them.
The main issue I have encounter here is that the result from the nosetest, the codes is attached below after lexicon.py.
In short, I had 1 error and 5 failures.
Error:
NameError: global name 'ASDFADFASDF' is not defined
Failure(s):
AssertionError: None != [('direction', 'north')]
AssertionError: None != [('verb', 'go')]
AssertionError: None != [('stop', 'the')]
AssertionError: None != [('noun', 'bear')]
AssertionError: None != [('number', 1234)]
Where the results was None instead of the answer at the right hand side. For the error I had a similar result from my codes except compares to the test_error, where I had
[('error', 'ASDFADFASDF')]
the test result is
[('error', ASDFADFASDF)]
As for the failure the typical failure message I got is:
File "C:\xxx\xxxxxx\learningpython\projectex48\skeleton\tests\lexicon_tests.py", line 13, in test_verb
assert_equal(lexicon.scan("go"),[('verb', 'go')])
AssertionError: None != [('verb', 'go')]
-------------------- >> begin captured stdout << ---------------------
[('verb', 'go')]
--------------------- >> end captured stdout << ----------------------
I don't know why it only run for 1 element, example "go kill eat" python only runs "go". I am still trying to figure out the logic and how to fix it. Can any one please help me to identify problem of why I failed the 5 test(s) and had 1 error.
I have spend weeks to code my own version of scan function for this exercise, this is the coding of lexicon.py:
direction = [('direction', 'north'),
('direction', 'south'),
('direction', 'east'),
('direction', 'west'),
('direction', 'up'),
('direction', 'down'),
('direction', 'left'),
('direction', 'right'),
('direction', 'back')
]
verbs = [('verb', 'go'),
('verb', 'stop'),
('verb', 'kill'),
('verb', 'eat')
]
stop_words = [('stop', 'the'),
('stop', 'in'),
('stop', 'of'),
('stop', 'from'),
('stop', 'at'),
('stop', 'it')
]
nouns = [('noun', 'door'),
('noun', 'bear'),
('noun', 'princess'),
('noun', 'cabinet')
]
library = tuple(nouns + stop_words + verbs + direction)
def convert_number(x):
try:
return int(x)
except ValueError:
return None
def scan(input):
element = input.split()
data = library
i = 0
z = 0
output = []
temp = True
while (i == 0) or (not (element[(i-1)] == element[-1])):
try:
j = 0
while (j == 0) or (not data[(j-1)][1] == data [-1][1]):
matching = data[j][1]
if (matching == element[i]):
output.append(data[j])
j += 1
z += 1
temp = False
else:
while (data[j][1] == data [-1][1]) and (temp == True):
convert = convert_number(element[i])
a = tuple(['number', convert])
b = tuple(['error', element[i]])
if convert == a[1] and not(convert == None):
output.append(a)
temp = False
else:
output.append(b)
temp = False
j += 1
i += 1
temp = True
except ValueError:
return None
else:
pass
print output
The above was a simplified version that I used to put into test, the concept is to search and gather/add the data corresponding to the input and save and append into the output as tuple, then finally print them out all together.
Below is the lexicon_tests.py codes which I directly copy from the book:
from nose.tools import *
from ex48 import lexicon
def test_direction():
assert_equal(lexicon.scan("north"), [('direction', 'north')])
result = lexicon.scan("north south east")
assert_equal(result, [('direction', 'north'),
('direction', 'south'),
('direction', 'east')])
def test_verb():
assert_equal(lexicon.scan("go"),[('verb', 'go')])
result = lexicon.scan("go kill eat")
assert_equal(result, [('verb', 'go'),
('verb', 'kill'),
('verb', 'eat')])
def test_stops():
assert_equal(lexicon.scan("the"), [('stop', 'the')])
result = lexicon.scan("the in of")
assert_equal(result, [('stop', 'the'),
('stop', 'in'),
('stop', 'of')])
def test_nouns():
assert_equal(lexicon.scan("bear"), [('noun', 'bear')])
result = lexicon.scan("bear princess")
assert_equal(result, [('noun', 'bear'),
('noun', 'princess')])
def test_numbers():
assert_equal(lexicon.scan("1234"), [('number', 1234)])
result = lexicon.scan("3 91234")
assert_equal(result, [('number', 3),
('number', 91234)])
def test_errors():
assert_equal(lexicon.scan("ASDFADFASDF"), [('error', ASDFADFASDF)]) #refer to Update#1
result = lexicon.scan("bear IAS princess")
assert_equal(result, [('noun', 'bear'),
('error', 'IAS'),
('noun', 'princess')])
This is where I saved the files in:
$ mkdir projectsex48
$ cd projectsex48/
$ mkdir skeleton
$ cd skeleton
$ mkdir bin
$ mkdir ex48 #(lexicon.py)
$ mkdir tests #(lexicon_tests.py)
$ mkdir doc
**Update #1:**For the Error
NameError: global name 'ASDFADFASDF' is not defined
It was because bracket '' was missing from lexicon_tests.py for ASDFADFASDF as below:
assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])
Now I have 6 failure where the function returns None.
AssertionError: None != [('direction', 'north')]
AssertionError: None != [('verb', 'go')]
AssertionError: None != [('stop', 'the')]
AssertionError: None != [('noun', 'bear')]
AssertionError: None != [('number', 1234)]
AssertionError: None != [('error', 'ASDFADFASDF')]
Yes!! I have finally solved it.
I made a mistake by ending the scan function with
I corrected it with
So the final corrected code is:
output is a tuple that I used to store the searching results.