learn python the hard way---ex48

来源:互联网 发布:淘宝账户限制登录 编辑:程序博客网 时间:2024/05/21 07:05
#https://learnpythonthehardway.org/book/ex46.html
ex48_skeleton/        ex48/         __init__.py         lexicon.py     bin/     docs/     tests/         NAME_tests.py         __init__.py         lexicon_test.py     setup.py

lexicon_test.py

from nose.tools import *from ex48 import lexicon                def test_direction():    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("ASDFADFASDF"),[('error','ASDFADFASDF')])    result=lexicon.scan("bear IAS princess")    assert_equal(result,[('noun','bear'),                                    ('error','IAS'),                                    ('noun','princess')])def teardown():    print"TEAR DOWN!"def test_basic():    print"I RAN!"

lexicon.py

import redef convert_number(s):    try:        return int(s)    except ValueError:        return Nonedef scan(input_str):        words=input_str.split(' ')  #split sentence        parttern=re.compile(r'\d+')  #regular expression,\d是匹配数字字符[0-9],+匹配一个或多个        direction_list=['north','south','west','east','down','up','left','right','back']        verb_list=['go','kill','stop','eat']        stop_list=['the','in','of','from','at','it']        noun_list=['door','bear','princess','cabinet']        sentence_list=[]     #initialize        for word in words:            bool=parttern.match(word)            if bool:                sentence=('number',convert_number(word))                sentence_list.append(sentence)            elif word in direction_list:                sentence=('direction',word)                sentence_list.append(sentence)            elif word in verb_list:                sentence=('verb',word)                sentence_list.append(sentence)            elif word in stop_list:                sentence=('stop',word)                sentence_list.append(sentence)            elif word in noun_list:                sentence=('noun',word)                sentence_list.append(sentence)            else:                sentence=('error',word)                sentence_list.append(sentence)              return sentence_list
cd ex48_skeletonnosetests
1 0
原创粉丝点击