【问题标题】:Nosetests TypeError and Attribute Error in Python (LPTHW, exercise 48)Python 中的 Nosetests TypeError 和 Attribute Error(LPTHW,练习 48)
【发布时间】:2016-06-06 00:49:55
【问题描述】:

我正在努力学习 Python,练习 48,使用鼻子测试来测试元组。我设置的nosetest如下:

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)

如果我在“def scan(self):”上方引入@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

【问题讨论】:

    标签: python nosetests


    【解决方案1】:

    看起来您需要先用您的字符串实例化您的词典对象,然后对该对象调用扫描。简而言之:

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

    您可以看到这一点,因为__init__ 方法将sentence 作为参数,而scan 方法没有真正的参数(只有self,它表示对象的一个​​实例)。使用@staticmethod 只是导致它把句子(在本例中为“north”)视为lexicon 类的一个实例,这显然是失败的原因。

    【讨论】:

      【解决方案2】:

      这是问题的完整解决方案

      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
      

      【讨论】:

        猜你喜欢
        • 2016-03-31
        • 1970-01-01
        • 2018-05-31
        • 1970-01-01
        • 1970-01-01
        • 2015-05-01
        • 1970-01-01
        • 2013-05-16
        • 2013-11-06
        相关资源
        最近更新 更多