nosetests, python -
i trying learn python, guide following asking me write simple 'game', utilizing tuples, lists, , classes.
when running 'nosetests' command, following error:
e. ====================================================================== error: tests.lexicon_tests.test_directions ---------------------------------------------------------------------- traceback (most recent call last): file "/library/python/2.6/site-packages/nose/case.py", line 187, in runtest self.test(*self.arg) file "/users/vb/documents/svn/programming/python/projects/lexicon/tests/lexicon_tests.py", line 6, in test_directions assert_equal(lexicon.scan("north"), [('directions', 'north')]) typeerror: unbound method scan() must called lexicon instance first argument (got str instance instead) ---------------------------------------------------------------------- ran 2 tests in 0.011s failed (errors=1) vb mp > ./lexicon.py > north [(), ('directions', 'north')] vb mp > ./lexicon.py > north south east [[[(), ('directions', 'north')], ('directions', 'south')], ('directions', 'east')] vb mp main file:
#!/usr/bin/env python # encoding: utf-8 import sys import os lexicon.game import lexicon def main(): stuff = raw_input('> ') lex = lexicon (stuff) split_array = lex.scan(stuff) print split_array #me = lex.tockens(split_array) #print me if __name__ == '__main__': main() class #!/usr/bin/env python # encoding: utf-8 """ import sys import os class lexicon (object): def __init__(self,data): self.direction = data #self.words = data.split() def scan(self,data): split_data = data.split() lex = lexicon (data) tocken = lex.tockens(split_data) return tocken def tockens(self,splitdata): sentence = splitdata verbs = ['go','swim','open','punch','fly','stop','kill','eat'] objekts = ['door','bear','face','princess','cabinet'] directions = ['north','south','east','west','down','up','left','right','back'] stopwords = ['the','in','of','from','at','it'] # setep 1, open array bit = () byte = () x in sentence: # step 2, match against verbs v in verbs: try: if (x == v): bit = ('verb',x) byte = [byte, bit] except valueerror: print "ooops!" o in objekts: try: if (x == o): bit = ('objekt',x) byte = [byte, bit] except valueerror: print "ooops!" d in directions: try: if (x == d): bit = ('directions',x) byte = [byte, bit] except valueerror: print "ooops!" s in stopwords: try: if (x == s): bit = ('stopwords',x) byte = [byte, bit] except valueerror: print "ooops!" return byte test
from nose.tools import * #import lexicon lexicon.game import lexicon def test_directions(): assert_equal(lexicon.scan("north"), [('directions', 'north')]) result = lexicon.scan("north south east") assert_equal(result, [('directions', 'north'), ('directions', 'south'), ('directions', 'east')]) thanks!
the way methods called in python object called on passed first argument, , arguments supplied each pushed down 1. when call static method (lexicon.scan) instead of instance method (lex.scan) first argument not supplied.
the method lexicon.scan requires first argument "lexicon" type object, want in test create lexicon object (lex = lexicon(stuff)) , call scan object (lex.scan("north")). is, calling scan("north"), while want call scan(lex, "north").
Comments
Post a Comment