笨辦法學python 49 建立句子

2021-07-24 06:10:27 字數 3177 閱讀 6376

為習題49寫測試方案

assert_raises()用來測試異常。第乙個變數為異常的類名(python所有的異常都繼承自exception類),第二個變數為要測試的函式名,第三個變數為函式輸入。

from nose.tools import *

from ex49 import parser

def test_sentence():

parse = parser.sentence(('noun', 'bear'), ('verb', 'go'), ('direction', 'north'))

assert_equal(parse.subject, 'bear')

assert_equal(parse.verb, 'go')

assert_equal(parse.object, 'north')

def test_peek():

assert_equal(parser.peek([('direction', 'north')]), 'direction')

assert_equal(parser.peek([('verb', 'go'), ('direction', 'north')]), 'verb')

assert_equal(parser.peek([('stop', 'of'), ('number', 1234), ('noun', 'princess')]), 'stop')

assert_equal(parser.peek(), none)

def test_match():

assert_equal(parser.match([('direction', 'north')], 'direction'), ('direction', 'north'))

assert_equal(parser.match([('direction', 'north')], 'verb'), none)

assert_equal(parser.match([('verb', 'go'), ('direction', 'north')], 'verb'), ('verb', 'go'))

assert_equal(parser.match([('stop', 'of'), ('number', 1234), ('noun', 'princess')], 'stop'), ('stop', 'of'))

assert_equal(parser.match([('stop', 'of'), ('number', 1234), ('noun', 'princess')], 'verb'), none)

def test_parse_verb():

assert_equal(parser.parse_verb([('stop', 'of'), ('verb', 'go'), ('direction', 'north')]), ('verb', 'go'))

assert_equal(parser.parse_verb([('stop', 'the'), ('verb', 'go'), ('direction', 'north')]), ('verb', 'go'))

assert_raises(parser.parsererror, parser.parse_verb, [('stop', 'in'), ('number', 1234), ('noun', 'princess')])

def test_parse_object():

assert_equal(parser.parse_object([('noun', 'bear'), ('verb', 'go'), ('direction', 'north')]), ('noun', 'bear'))

assert_equal(parser.parse_object([('direction', 'south'), ('verb', 'go'), ('noun', 'bear')]), ('direction', 'south'))

assert_raises(parser.parsererror, parser.parse_object, [('verb', 'kill'), ('stop', 'of'), ('verb', 'go')])

def test_parse_subject():

sub1 = parser.parse_subject([('verb', 'go'), ('direction', 'north')], ('noun', 'princess'))

assert_equal(sub1.subject, 'princess')

assert_equal(sub1.verb, 'go')

assert_equal(sub1.object, 'north')

sub2 = parser.parse_subject([('verb', 'kill'), ('stop', 'of'), ('noun', 'bear')], ('noun', 'player'))

assert_equal(sub2.subject, 'player')

assert_equal(sub2.verb, 'kill')

assert_equal(sub2.object, 'bear')

def test_parse_sentence():

sen1 = parser.parse_sentence([('verb', 'kill'), ('stop', 'of'), ('noun', 'bear')])

assert_equal(sen1.subject, 'player')

assert_equal(sen1.verb, 'kill')

assert_equal(sen1.object, 'bear')

sen2 = parser.parse_sentence([('noun', 'princess'), ('stop', 'of'), ('verb', 'go'), ('direction', 'west')])

assert_equal(sen2.subject, 'princess')

assert_equal(sen2.verb, 'go')

assert_equal(sen2.object, 'west')

assert_raises(parser.parsererror, parser.parse_sentence, [('stop', 'the'), ('stop', 'of'), ('direction', 'west'), ('verb', 'go')])

測試函式可能出現的情況。

笨辦法學Python

1.知識點 13節講的主要是module 模組 的概念,常用的語法是from xx import 依託於python強大的模組庫,使得呼叫十分輕鬆,功能十分強悍。argv叫做引數變數,可以理解為乙個包裹,在解包 unpack 的時候,將引數值賦給不同的變數名,其中第乙個變數是 隱藏 的,用來指代檔案...

笨辦法學Python(三十)

前一習題中你寫了一些 if 語句 if statements 並且試圖猜出它們是什麼,以及實現的是什麼功能。在你繼續學習之前,我給你解釋一下上一節的加分習題的答案。上一節的加分習題你做過了吧,有沒有?你認為 if 對於它下一行的 做了什麼?if 語句為 建立了乙個所謂的 分支 就跟 rpg 遊戲中的...

笨辦法學Python(六)

雖然你已經在程式中寫過字串了,你還沒學過它們的用處。在這章習題中我們將使用複雜的字串來建立一系列的變數,從中你將學到它們的用途。首先我們解釋一下字串是什麼東西。字串通常是指你想要展示給別人的 或者是你想要從程式裡 匯出 的一小段字元。python 可以通過文字裡的雙引號 或者單引號 識別出字串來。這...