2012-07-12 23 views
0

我正在努力學習使用nosetests測試元組的Python 48。我已經設置了nosetest如下:Python中的Nosetests TypeError和屬性錯誤(LPTHW,練習48)

def test_directions(): 
    assert_equal(lexicon.scan("north"), [('direction', 'north')]) 

不過,我每次碰到下面的錯誤:

...line 5, in test_directions 
    assert_equal(lexicon.scan("north"), [('direction', 'north')]) 
TypeError: unbound method scan() must be called with lexicon instance 
as first argument (got str instance instead) 

如果我介紹@staticmethod只是上面的「高清掃描(個體經營)」,我得到這個錯誤,而不是:

line 24, in scan 
    words = self.sentence.split() 
AttributeError: 'str' object has no attribute 'sentence' 

而我測試它的代碼是在下面。我錯過了什麼?

class lexicon(object): 

    def __init__(self, sentence): 

     self.sentence = sentence 

     self.direction = "direction" 
     self.verb = "verb" 
     self.noun = "noun" 
     self.stop = "stop" 
     self.number = "number" 

     self.direction_words = ('north', 'south', 'east', 'west', 'up', 'down') 
     self.verb_words = ('go', 'stop', 'kill', 'eat') 
     self.noun_words = ('door', 'bear', 'princess', 'cabinet') 
     self.stop_words = ('the', 'in', 'of', 'from', 'at', 'it') 

     self.a = 0 
     self.instructions = [] 

    def scan(self): 

     words = self.sentence.split() 
     self.a = 0 

     while self.a < len(words): 
      result = words[self.a] 
      if result in self.direction_words: 
       self.instructions.append(('direction', result)) 
      elif result in self.verb_words: 
       self.instructions.append(('verb', result)) 
      elif result in self.noun_words: 
       self.instructions.append(('noun', result)) 
      elif result in self.stop_words: 
       self.instructions.append(('stop', result)) 
      elif self.test_num(result) == None: 
       self.instructions.append(('number', "Error")) 
      else: 
       self.instructions.append(('number', result)) 
      self.a += 1 

     return self.instructions 

    def test_num(self, num): 
     try: 
      return int(num) 
     except ValueError: 
      return None 

回答

3

它看起來像你需要首先用你的字符串實例化你的詞典對象,然後調用該對象的掃描。總之:

def test_directions(): 
    assert_equal(lexicon("north").scan(), [('direction', 'north')]) 

你可以看到這一點,因爲__init__方法採用sentence作爲參數,而scan方法並沒有真正的參數(只是self,表示對象的實例)。使用@staticmethod只是導致它將該句子(在本例中爲「北」)作爲lexicon類的實例處理,因爲顯而易見的原因失敗。

+0

完美,謝謝! – 2012-07-13 03:21:43

0

這是一個完整的解決問題的辦法

directions = ['north', 'south', 'east'] 
stops =["the" , "in" , "of"] 
verbs = ['go','stop','kill','eat' ] 
numbers = xrange(999999999) 
nouns=["bear" , "princess" ] 
list_of_lists=['go','stop','kill','eat','north', 'south', 'east','door',"the" , "in" , "of","bear" , "princess",xrange(999999999)] 
list99=[] 


class lexicon: 
    @staticmethod 
    def scan(d): 
     list2=d.split() 
     list1=[] 
     list3=[] 
     try: 
      for x in d.split(): 

       if int(x) in xrange(999999999): 
        a = x 

        list1.append(a) 
        list2.remove(a) 
       else: 
        print "yes" 
     except: 
      list99=[] 

     for x in d.split(): 
      #print x 
      if x in nouns: 
       z1 = ("noun" , x) 
       list3.append(z1) 

      elif x in directions: 
       z2 = ("direction" , x) 
       list3.append(z2) 

      elif x in verbs: 
       z2 = ("verb" , x) 
       list3.append(z2) 
      elif x in list1: 
       z2 = ("number" , int(x)) 
       list3.append(z2) 
      elif x in stops: 
       z2 = ("stop" , x) 
       list3.append(z2) 
      elif x in list2: 
       z2 = ("error" , x) 
       list3.append(z2) 
      else: 
       print "yes" 

     print "d:%s"%d.split() 
     print "list1:%s"%list1 
     print "list2:%s"%list2 
     print "list3:%s"%list3 
     return list3