詳解Python中的PyInputPlus模塊_第1頁
詳解Python中的PyInputPlus模塊_第2頁
詳解Python中的PyInputPlus模塊_第3頁
詳解Python中的PyInputPlus模塊_第4頁
詳解Python中的PyInputPlus模塊_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

第詳解Python中的PyInputPlus模塊目錄安裝PyInputPlus關(guān)鍵字參數(shù)min、max、greaterThan和lessThan關(guān)鍵字參數(shù)blank關(guān)鍵字參數(shù)limit、timeout和default關(guān)鍵字參數(shù)allowRegexes和blockRegexes將自定義驗證函數(shù)傳遞給inputCustom()輸入驗證代碼檢查用戶輸入值,在Python中我們用的最多的是input()函數(shù),但是有些情況下反復(fù)使用input()函數(shù)可能會遺漏某些場景,并且該函數(shù)允許無效輸入通過檢查。因此我們需要使用Python的第三方模塊PyInputPlus進行輸入驗證。

PyInputPlus包含與input()類似的、用戶多種數(shù)據(jù)(數(shù)字日期、E-mail地址等)的函數(shù)。如果用戶輸入了無效的內(nèi)容,例如格式錯誤的日期或超出預(yù)期范圍的數(shù)字,PyIputPlus會再次提示他們輸入。PyInputPlus還包含其他有用的功能,例如提示用戶的次數(shù)限制和時間限制(如果要求用戶在時限內(nèi)作出響應(yīng))。

安裝PyInputPlus

PyInputPlus不是Python標準庫的一部分,因此需要使用pip單獨安裝。命令如下:

pipinstall--userpyinputplus

PyInputPlus具有以下幾種用于不同類型輸入的函數(shù):

inputStr()類似于內(nèi)置的input()函數(shù),但具有一般的PyInputPlus功能inputNum()確保用戶輸入數(shù)字并返回int或float值,這取決于數(shù)字是否包含小數(shù)點inputChoice()確保用戶輸入習題提供的選項之一inputMenu()與inputChoice類似,但提供一個帶有數(shù)字或字母選項的菜單inputDatetime()確保用戶輸入日期和時間inputYesNo()確保用戶輸入yes或no響應(yīng)inputBool()類似inputYesNo(),但接收True或False響應(yīng),并返回一個布爾值inputEmail()確保用戶輸入有效的E-mail地址inputFilepath()確保用戶輸入有效的文件路徑和文件名,并可以選擇檢查是否存在具有該名稱的文件inputPassword()類似于內(nèi)置的input(),但是在用戶輸入時顯示*字符,因此不會在屏幕上顯示密碼或者其他敏感信息只要輸入了無效內(nèi)容,以上函數(shù)就會自動提示用戶:

importpyinputplusaspyip

response=pyip.inputNum()

'five'isnotanumber

response

10

每次調(diào)用PyInputPlus模塊的函數(shù)時,import語句中的aspyip代碼讓我們不必輸入pyinputplus,而是可以使用較短的pyip名稱。正如可以將字符串傳遞給input()以提供提示一樣,也可以將字符串傳遞給PyInputPlus模塊的函數(shù)的prompt關(guān)鍵字參數(shù)來顯示提示:

importpyinputplusaspyip

response=pyip.inputInt(prompt='Enteranumber')

Enteranumber:cat

'cat'isnotanInteger.

Enteranumber:10

response

10

關(guān)鍵字參數(shù)min、max、greaterThan和lessThan

接收int和float數(shù)的inputNum()、inputInt()和inputFloat()函數(shù)還具有min、max、greaterThan和lessThan關(guān)鍵字參數(shù),用于指定有效值范圍,例如如下例子:

importpyinputplusaspyip

response=pyip.inputNum('Enternum:',min=4)

Enternum:3

Inputmustbeatminimum:4

Enternum:4

response

response=pyip.inputNum('Enternum:',greaterThan=4)

Enternum:4

Inputmustbegreaterthan4.

Enternum:5

response

response=pyip.inputNum('Enternum:',min=4,lessThan=6)

Enternum:6

Inputmustbelessthan6.

Enternum:3

Inputmustbeatminimum4.

Enternum:4

response

4

關(guān)鍵字參數(shù)blank

在默認情況下,除非將blank關(guān)鍵字參數(shù)設(shè)置為True,否則不允許輸入空格字符:

importpyinputplusaspyip

response=pyip.inputNum('Enternum:')

Enternum:(blankinputenteredhere)

Blankvaluesarenotallowed.

Enternum:40

response

response=pyip.inputNum(blank=True)

(blankinputenteredhere)

response

''

如果想使輸入可選,使用blank=True,這樣用戶不需要輸入任何內(nèi)容。

關(guān)鍵字參數(shù)limit、timeout和default

在默認情況下,PyInputPlus模塊的函數(shù)在程序運行時會一直要求用戶提供有效輸入,如果希望某個函數(shù)在經(jīng)過一定次數(shù)的嘗試或一定的時間后停止要求用戶輸入,就可以使用limit和timeout關(guān)鍵字參數(shù)。用limit關(guān)鍵字參數(shù)傳遞一個整數(shù),以確定PyInputPlus的函數(shù)在放棄之前嘗試接受有效輸入多少次。用timeout關(guān)鍵字參數(shù)傳遞一個整數(shù),以確定用戶在多少秒之內(nèi)必須提供有效輸入,然后PyInputPlus模塊的函數(shù)會放棄。

如果用戶未能提供有效輸入,那么這些關(guān)鍵字參數(shù)將分別導(dǎo)致函數(shù)引發(fā)RetryLimitException或TimeoutException異常。

當你使用這些關(guān)鍵字參數(shù)并傳入default關(guān)鍵字參數(shù)時,該函數(shù)將返回默認值,而不是引發(fā)異常。例如:

response=pyip.inputNum(limit=2,default='N/A')

hello

'hello'isnotanumber

world

'world'isnotanumber

response

'N/A'

inputNum()函數(shù)使用了default關(guān)鍵字參數(shù)后不會引發(fā)RetryLimitException,只會返回字符串N/A.

關(guān)鍵字參數(shù)allowRegexes和blockRegexes

我們也可以使用正則表達式指定輸入是否被接受。關(guān)鍵字參數(shù)allowRegexes和blockRegexes利用正則表達式字符串列表來確定PyInputPlus模塊的函數(shù)將接受或拒絕哪些內(nèi)容作為有效輸入。例如,使用inputNum()函數(shù)將接收羅馬數(shù)字以及常規(guī)數(shù)字作為有效輸入:

importpyinputplusaspyip

response=pyip.inputNum(allowRegexes=[r'(I|V|X|L|C|D|M)+',r'zero'])

response

'XLII'

我們還可以用blockRegexes關(guān)鍵字參數(shù)指定PyInputPlus模塊的函數(shù)不接收的正則表達式字符串列表:

importpyinputplusaspyip

response=pyip.inputNum(blockRegexes=[r'[02468]$'])

Thisresponseisinvalid.

response

43

如果同時指定allowRegexes和blockRegexes參數(shù),那么允許列表將優(yōu)先于阻止列表。例如:

importpyinputplusaspyip

response=pyip.inputStr(allowRegexes=[r'caterpillar','category'],blockRegexes=[r'cat'])

Thisresponseisinvalid

catastrophe

Thisresponseisinvalid

category

response

'category'

將自定義驗證函數(shù)傳遞給inputCustom()

可以編寫函數(shù)以執(zhí)行自定義的驗證邏輯,并將函數(shù)傳遞給inputCustom()。例如,我們可以創(chuàng)建自己的addsUpToTen()函數(shù),然后將其傳遞給inputCustom()。注意,函數(shù)調(diào)用看起來像inputCustom(addsUpToTen),而不是inputCustom(addsUpToTen()),因為我們是將addsUpToTen()函數(shù)本身傳遞給inputCustom(),而不是調(diào)用addsUpToTen()函數(shù)并傳遞其返回值:

importpyinputplusaspyip

defaddsUpToTen(numbers):

numbersList=list(numbers)

fori,digitinenumerate(numersList):

numbersList[i]=int(digit)

ifsum(numbersList)!=10:

raiseException('Thedigitsmustaddupto10,not%s.'%(sum(numbersList)))

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論