Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing objects with nose
    primarykey
    data
    text
    <p>I'm working my way through Learn Python the Hard way Exercise 49 (<a href="http://learnpythonthehardway.org/book/ex49.html" rel="nofollow">http://learnpythonthehardway.org/book/ex49.html</a>) and I need to compare two different objects. This is the code I need to test:</p> <pre><code>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) </code></pre> <p>My problem is with the function <code>parse_sentence</code> 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:</p> <pre><code>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) </code></pre> <p>But I keep receiving this traceback error:</p> <pre><code>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: &lt;ex49.parser.Sentence object at 0x02651C50&gt; != &lt;ex49.parser.Sentence object at 0x02651C30&gt; </code></pre> <p>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</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload