This is failing because you are comparing two instantiated Sentence objects, not their individual attributes.
Check "Elegant ways to support equivalence ("equality") in Python classes" for a method that can compare the attributes.
Frage
I'm working my way through Learn Python the Hard way Exercise 49 (http://learnpythonthehardway.org/book/ex49.html) and I need to compare two different objects. This is the code I need to test:
class ParserError(Exception):
pass
class Sentence(object):
def __init__(self, subject, verb, object):
# remember we take ('noun','princess') tuples and convert them
self.subject = subject[1]
self.verb = verb[1]
self.object = object[1]
def peek(word_list):
if word_list:
word = word_list[0]
return word[0]
else:
return None
def match(word_list, expecting):
if word_list:
word = word_list.pop(0) #Here it's returning 'noun' from the noun/princess tuple
if word[0] == expecting: #Here it's testing whether or not the new item in the 0 position ('noun' in this case) = expecting
return word
else:
return None
else:
return None
def skip(word_list, word_type):
while peek(word_list) == word_type:
match(word_list, word_type)
def parse_verb(word_list):
skip(word_list, 'stop')
if peek(word_list) == 'verb':
return match(word_list, 'verb')
else:
raise ParserError("Expected a verb next.")
def parse_object(word_list):
skip(word_list, 'stop')
next = peek(word_list)
if next == 'noun':
return match(word_list, 'noun')
if next == 'direction':
return match(word_list, 'direction')
else:
raise ParserError("Expected a noun or direction next.")
def parse_subject(word_list, subj):
verb = parse_verb(word_list)
obj = parse_object(word_list)
return Sentence(subj, verb, obj)
def parse_sentence(word_list):
skip(word_list, 'stop')
start = peek(word_list)
if start == 'noun':
subj = match(word_list, 'noun')
return parse_subject(word_list, subj)
elif start == 'verb':
# assume the subject is the player then
return parse_subject(word_list, ('noun', 'player'))
else:
raise ParserError("Must start with subject, object, or verb not: %s" % start)
My problem is with the function parse_sentence
where I'm supposed to create a sentence object. So I need to create another sentence object in my test code and make sure it is identical:
def test_parse_subject():
word_list = [('verb', 'kill'), ('direction', 'north')]
subj = ('noun', 'princess')
verb = ('verb', 'kill')
obj = ('direction', 'north')
obj_sent = Sentence(subj, verb, obj)
assert_equal(parser.parse_subject(word_list, subj), obj_sent)
But I keep receiving this traceback error:
Traceback (most recent call last):
File "G:\Python27\lib\site-packages\nose\case.py", line 197, in runTest
self.test(*self.arg)
File "G:\Users\Charles\dropbox\programming\parsing_test\skeleton\tests\parser_tests.py", line 45, in test_parse_subjec
t
assert_equal(parser.parse_subject(word_list, subj), obj_sent)
AssertionError: <ex49.parser.Sentence object at 0x02651C50> != <ex49.parser.Sentence object at 0x02651C30>
So it's not returning the objects as the same, but I'm pretty sure they are. I gave them the right arguments. If the objects are the same, how do I confirm this? Thanks in advance
Lösung
This is failing because you are comparing two instantiated Sentence objects, not their individual attributes.
Check "Elegant ways to support equivalence ("equality") in Python classes" for a method that can compare the attributes.