有三种方法可以用来判断某个变量是否被定义过,见下面几个例子,注意变量名称要用引号括起来
-
if 'ss' in locals().keys():
-
print 'yes'
-
else:
-
print 'no'
-
try:
-
print ss
-
except NameError:
-
print 'this var not defined'
-
if 'var_name' in dir():
-
print 'yes'
-
else:
-
print 'no'
下面是一组python用于验证数据是否合法的函数,包括检测类型是否为整数、浮点数、字符串、字典、元组、数组、bool类型等,还可以检测是否为货币、是否为空、是否是合法的日期格式,是否是邮件地址(Email),是否是中文字符,是否符合用户账号的规则,是否是合法的ip地址等等。
-
#! /usr/bin/env python
-
#coding=utf-8
-
import types
-
import re
-
-
-
"""
-
验证所有表单提交的数据
-
"""
-
-
#判断是否为整数 15
-
def IsNumber(varObj):
-
-
return type(varObj) is types.IntType
-
-
#判断是否为字符串 string
-
def IsString(varObj):
-
-
return type(varObj) is types.StringType
-
-
#判断是否为浮点数 1.324
-
def IsFloat(varObj):
-
return type(varObj) is types.FloatType
-
-
#判断是否为字典 {'a1':'1','a2':'2'}
-
def IsDict(varObj):
-
-
return type(varObj) is types.DictType
-
-
#判断是否为tuple [1,2,3]
-
def IsTuple(varObj):
-
-
return type(varObj) is types.TupleType
-
-
#判断是否为List [1,3,4]
-
def IsList(varObj):
-
-
return type(varObj) is types.ListType
-
-
#判断是否为布尔值 True
-
def IsBoolean(varObj):
-
-
return type(varObj) is types.BooleanType
-
-
#判断是否为货币型 1.32
-
def IsCurrency(varObj):
-
-
#数字是否为整数或浮点数
-
if IsFloat(varObj) and IsNumber(varObj):
-
#数字不能为负数
-
if varObj >0:
-
return isNumber(currencyObj)
-
return False
-
return True
-
-
#判断某个变量是否为空 x
-
def IsEmpty(varObj):
-
-
if len(varObj) == 0:
-
return True
-
return False
-
-
#判断变量是否为None None
-
def IsNone(varObj):
-
-
return type(varObj) is types.NoneType# == "None" or varObj == "none":
-
-
#判断是否为日期格式,并且是否符合日历规则 2010-01-31
-
def IsDate(varObj):
-
-
if len(varObj) == 10:
-
rule = '(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)$/'
-
match = re.match( rule , varObj )
-
if match:
-
return True
-
return False
-
return False
-
-
#判断是否为邮件地址
-
def IsEmail(varObj):
-
-
rule = '[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$'
-
match = re.match( rule , varObj )
-
-
if match:
-
return True
-
return False
-
-
#判断是否为中文字符串
-
def IsChineseCharString(varObj):
-
-
for x in varObj:
-
if (x >= u"\u4e00" and x<=u"\u9fa5") or (x >= u'\u0041' and x<=u'\u005a') or (x >= u'\u0061' and x<=u'\u007a'):
-
continue
-
else:
-
return False
-
return True
-
-
-
#判断是否为中文字符
-
def IsChineseChar(varObj):
-
-
if varObj[0] > chr(127):
-
return True
-
return False
-
-
#判断帐号是否合法 字母开头,允许4-16字节,允许字母数字下划线
-
def IsLegalAccounts(varObj):
-
-
rule = '[a-zA-Z][a-zA-Z0-9_]{3,15}$'
-
match = re.match( rule , varObj )
-
-
if match:
-
return True
-
return False
-
-
#匹配IP地址
-
def IsIpAddr(varObj):
-
-
rule = '\d+\.\d+\.\d+\.\d+'
-
match = re.match( rule , varObj )
-
-
if match:
-
return True
-
return False
-
-
-
-
if __name__=='__main__':
-
print 'IsDate:',IsDate('2010-01-31')
-
print 'IsNone:',IsNone(None)
-
print 'IsEmpty:',IsEmpty('1')
-
print 'IsCurrency:',IsCurrency(1.32)
-
print 'IsList:',IsList([1,3,4])
-
print 'IsTuple:',IsTuple([1,3,4])
-
print 'IsDict:',IsDict({'a1':'1','a2':'2'})
-
print 'IsFloat:',IsFloat(1.2)
-
print 'IsString:',IsString('string')
-
print 'IsNumber:',IsNumber(15)
-
print 'IsEmail:',IsEmail('sgicer@163.com')
-
print 'IsChineseChar:',IsChineseChar(u'啊')
-
print 'IsChineseCharString:',IsChineseCharString(u'啊倒萨')
-
print 'IsLegalAccounts:',IsLegalAccounts('alan_z')
-
print 'IsIpAddr:',IsIpAddr('127.1110.0.1')
-
pass
阅读(3600) | 评论(0) | 转发(0) |