笨方法学Python练习48:为什么我执行nosetests 命令之后,命令行卡住了?

nosetests 命令卡死
笨方法学Python练习48:nosetests 命令卡死
(初次发帖,如果有什么做的不好的地方,还请指出。)

在笨方法学Python的ex48中,我编写的lexicon类使得测试命令nosetests卡死了。
而我自己将test中的每个函数在控制台人肉run过一遍,并没有出现卡死的情况。
I need help...

运行nosetests之后:
图片描述

CTRL+C之后 :
图片描述

运行很久之后 CTRL+C :

图片描述
lexicon 代码如下:

class Lexicon( object ) :

def __init__( self ) :

    self.directions = [ 'north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back' ]
    self.verbs = [ 'go', 'stop', 'kill', 'eat' ]
    self.stops= [ 'the', 'in', 'of', 'form', 'at', 'it' ]
    self.nouns = [ 'door', 'bear', 'princess', 'cabinet' ]
    self.numbers = []


def lexicon( self ) :

    stuff = raw_input( "> " )
    words = stuff.split(" ")
    result = self.scan( words )        
    print result


def isNumber ( self, word ) :

    try :
        int( word )
        return True
    except ValueError :
        return False



def scan( self, words ) :

    result = []
    for word in words :
        if word in self.directions :
            result.append( ('direction', word) )
        elif word in self.verbs :
            result.append( ('verb', word) )
        elif word in self.verbs :
            result.append( ('stop', word ) )
        elif word in self.nouns :
            result.append( ('noun', word ) )
        elif word in self.stops :
            result.append( ('stop', word) )
        elif self.isNumber( word ) :
            result.append( ('number', int( word ) ) )
        else :
            result.append( ( 'error' , word ) ) 

    return result

单元测试代码如下 :

from nose.tools import*
from ex48.ex48 import Lexicon

def test_directions() :
    assert_equal( Lexicon().scan("north"), [( 'direction', 'north' )] )
    result = Lexicon().scan( "north south east" )
    assert_equal( result, [ ('direction', 'north'),
                            ('direction', 'south'), 
                            ('direction', 'east') ] )

def test_verbs() :
    assert_equal( Lexicon().scan("go"), ['verb', 'go'] )
    result = Lexicon().scan( "go kill eat" )
    assert_equal( result, [ ('verb', 'go' ),
                            ('verb', 'kill'),
                            ('verb', 'eat') ] )

def test_stops() :
    assert_equal( Lexicon().scan("the"), [ ('stop', 'the') ] )
    result = Lexicon().scan( "the in of" )
    assert_equal( result, [ ('stop', 'the' ),
                            ('stop', 'in'),
                            ('stop', 'of') ] )

def test_nouns() :
    assert_equal( Lexicon().scan("bear"), [ ('noun', 'bear') ] )
    result = Lexicon().scan( "bear princess" )
    assert_equal ( result, [ ('noun', 'bear'),
                             ('noun', 'princess') ] )

def test_numbers() :
    assert_equal ( Lexicon().scan("1234"), [('number', 1234) ] )
    result = Lexicon().scan( "3 91234" )
    assert_equal( result, [('number', 3 ),
                           ('number', 91234 ) ] )

def test_errors() :
    assert_equal( Lexicon().scan("asdf"), [('error', 'asdf')] )
    result = Lexicon().scan( "bear IAS princess" )
    assert_equal( result, [('noun', 'bear'),
                           ('error', 'IAS'),
                           ('noun', 'princess') ] )

测试代码出处 : http://www.kancloud.cn/kancloud/learn-python-hard-way/49926

实在是找不到解决方法了,特来求助。
不论如何,感谢浏览。

阅读 6.3k
3 个回答

Ubuntu 14.04 亲测没有卡死,但是测试没有通过

from ex48.ex48 import Lexicon ==> from ex48 import Lexicon

新手上路,请多包涵

运行 scan 的时候没有把 words 分解成word

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏