python-检查合法标识符(45)

import  sys
import keyword
import string

first_chs = string.ascii_letters + '_'
all_chs = first_chs + string.digits

def check_id(idt):
    if keyword.iskeyword(idt):  #判断是不是关键字
        return "%s is keyword" % idt

    if idt[0] not in first_chs:
        return "1st invalid"

    for ind,ch in enumerate(idt[1:]):
        if ch not in all_chs:
            return "char in postion #%s invalid" % (ind + 2)

        return "%s is valid" % idt

if __name__ == '__main__':
    print(check_id(sys.argv[1]))    # python 脚本名称 想要测试的标识符名字



#测试
# [root@room9pc01 lianxi]# python3 45检查合法标识符.py True
# True is keyword
# [root@room9pc01 lianxi]# python3 45检查合法标识符.py a.*
# char in postion #2 invalid
# [root@room9pc01 lianxi]# python3 45检查合法标识符.py aaaa.*
# aaaa.* is valid

你可能感兴趣的:(python-检查合法标识符(45))