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
- Learn Python the Hard Way ex48
- learn python the hard way 的ex48
- learn python the hard way---ex48
- Learn Python The Hard Way
- Learn Python The Hard Way
- 学《learn python the hard way》(3)
- Learn Python The Hard Way-Ecercise1~5
- Learn Python The Hard Way-Ecercise6~10
- Learn Python The Hard Way-Ecercise11~15
- Learn Python The Hard Way-Ecercise16~20
- Learn Python The Hard Way-Ecercise21~25
- Learn Python The Hard Way-Ecercise26~30
- Learn Python The Hard Way-Ecercise31~35
- Learn Python The Hard Way-Ecercise36~40
- Learn Python The Hard Way exercise 42
- Learn Python The Hard Way Ex40翻译
- 《Learn Python the hard way》学习记录
- Learn Python the Hard Way Ex41记录
- 深入JVM锁机制-reentrantlock
- HDU-1261 字串数
- Intellij IDEA 中无法编译lombok的解决方法
- winscp安装
- Codeforces 675C Money Transfers【贪心】【新年快乐】T T窝要去打联盟咯!
- learn python the hard way---ex48
- 代理模式
- JDBC与JNDI这两种连接方式的区别
- 斐波那契堆(Fibonacci heaps)
- 小猴子下落
- volley
- 观察者模式
- 洛谷 p2320 鬼子的钱袋
- 序言-- UI基础