1. 程式人生 > >Python例項淺談之一識別符號檢查

Python例項淺談之一識別符號檢查

一、問題

Python識別符號必須以字母或下劃線開頭,後面跟字母、下劃線或者數字,且識別符號不能為關鍵字,如何有效的檢查?

二、解決

1、方法一

(1)python指令碼
新建idcheck.py,輸入程式碼,然後執行pythonidcheck.py。

#!/usr/bin/env python
'''
$Id$

idcheck.py -- checks identifiers for validity

This application is limited in that it currently only supports
checking identifiers with length > 1 (does not process identifiers
of length greater than 1.  This application also does not recognize
do keywords.

Exercise:

    6-2) update this script to process identifiers of length 1
        as well as recognizing keywords as invalid identifiers
        (for use by the programmer; they are valid Python
        identifiers otherwise).
'''

import string        # string utility module

# create alphabet and number sets
alphas = string.letters + '_'
nums = string.digits

# salutation message and input prompt
print 'Welcome to the Identifier Checker v1.0'
print 'Testees must be at least 2 chars long.'
inp = raw_input('Identifier to test >>> ')

# only take action for identifiers with length >= 1
if len(inp) >= 1:

    # first character must be alphabetic
    if inp[0] not in alphas:
        print 'invalid: first symbol must be alphabetic'

    # remaning characters can be alphanumeric
    else:
        for otherChar in inp[1:]:
            if otherChar not in alphas + nums:
                print 'invalid: remaining symbols must be alphanumeric'
                break
        else:
            print "okay as an identifier"
else:
    print 'invalid: length must be >= 1'
(2)執行結果

        其中for-else結構中else語句是一個可選項,它只在 for 迴圈完整的結束而沒有遇到break 時執行。
        效能:for otherChar in inp[1:]:迴圈中的 if 語句執行了合併兩個字串的操作,被合併的這兩個字串從始至終就沒變過,而每次都會重新進行一次計算,需要建立新物件,如果先把這兩個字串存為一個新字串,就可以直接引用這個字串而不用進行重複計算了。所以可以改為:

        alphnums = alphas + nums
        for otherChar in inp[1:]:
            if otherChar not in alphnums:
                print 'invalid: remaining symbols must be alphanumeric'
                break
        else:
            print "okay as an identifie

2、方法二

       針對方法一中不能檢查識別符號是否為Python的關鍵字的問題(關鍵字是不允許用做識別符號的),故引入keyword模組中的kwlist關鍵字列表。
(1)python指令碼
新建idcheck2.py,輸入程式碼,然後執行python idcheck2.py。
#!/usr/bin/env python

from keyword import kwlist
import string

ALPHAS = string.ascii_letters + '_'
NUMS = string.digits

def main():
    print 'Welcome to the Identifier Checker v2.0'
    myInput = raw_input('Identifier to test >>>').strip()

    if len(myInput) == 0:
	print "ERROR: no identifier candidate entered"
	return

    if myInput in kwlist:
	print "ERROR: %r is a keyword" % myInput
	return

    alnums = ALPHAS + NUMS
    for i, c in enumerate(myInput):
	if i == 0 and c not in ALPHAS:
	    print 'ERROR: first symbol must be alphabetic'
	    break
	if c not in alnums:
	    print 'ERROR: remaining symbols must be alphanumeric'
	    break
    else:
	print "okay as an identifier"

if __name__ == '__main__':
    main()

其中Python的關鍵字列表為:


(2)執行結果

三、總結

(1)Python程式碼效能的分析和提高還需要不停的總結,在迴圈中儘量不要重複計算值不變的字串。
(2)若有更好的設計或思路,請留言,在此先感謝!