有关这些代码用途的更多详细信息,请参阅 LearningPythonTheHardWay 练习 48。我还是新手,我知道有很多需要改进或更好的方法来做到这一点。
我在这里遇到的主要问题是nosetest的结果,代码附在lexicon.py之后。
简而言之,我有 1 个错误和 5 个失败。
错误:
NameError: global name 'ASDFADFASDF' is not defined
失败:
AssertionError: None != [('direction', 'north')]
AssertionError: None != [('verb', 'go')]
AssertionError: None != [('stop', 'the')]
AssertionError: None != [('noun', 'bear')]
AssertionError: None != [('number', 1234)]
结果是 None 而不是右侧的答案。对于这个错误,我的代码得到了类似的结果,除了与我有的 test_error 相比
[('error', 'ASDFADFASDF')]
测试结果是
[('error', ASDFADFASDF)]
至于失败,我得到的典型失败消息是:
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 << ----------------------
我不知道为什么它只运行 1 个元素,例如“go kill eat” python 只运行“go”。我仍在尝试找出逻辑以及如何解决它。任何人都可以帮我找出为什么我没有通过 5 次测试并出现 1 次错误的问题。
我花了数周时间为这个练习编写我自己版本的扫描函数,这是 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
上面是我用来测试的一个简化版本,其概念是搜索并收集/添加与输入对应的数据,并将其保存并附加到输出中作为元组,然后将它们一起打印出来。
下面是我直接从书中复制的 lexicon_tests.py 代码:
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')])
这是我保存文件的地方:
$ mkdir projectsex48
$ cd projectsex48/
$ mkdir skeleton
$ cd skeleton
$ mkdir bin
$ mkdir ex48 #(lexicon.py)
$ mkdir tests #(lexicon_tests.py)
$ mkdir doc
**更新#1:**对于错误
NameError: global name 'ASDFADFASDF' is not defined
这是因为 ASDFADFASDF 的 lexicon_tests.py 中缺少括号 '',如下所示:
assert_equal(lexicon.scan("ASDFADFASDF"), [('error', 'ASDFADFASDF')])
现在我有 6 次失败,函数返回 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')]