您的位置:首页 > 编程语言 > Python开发

python核心编程-诊断函数

2015-12-05 21:13 513 查看
使用在调试和性能测量方面上,创建一个设置测试环境的诊断函数,然后对有疑问的地方,调用函数


#!/usr/bin/env python
# -*- coding: UTF-8 -*-

def testit(func, *nkwargs, **kwargs):
try:
print '%s'% nkwargs
print '%s'% kwargs
retval = func(*nkwargs, **kwargs)
result = (True, retval)
except Exception, diag:
result = (False, str(diag))
print 'result:%s' % result[0]
return result

def test():
funcs = (int, long, float)
vals = (1234, 12.34, '123', '12.34')

for eachFunc in funcs:
print '_'*20
for eachVal in vals:
retval = testit(eachFunc, eachVal)
if retval[0]:
print '%s(%s) = '% (eachFunc.__name__,'eachVal'),retval[1]
else:
print '%s(%s) = FAILED:'% (eachFunc.__name__,'eachVal'),retval[1]
if __name__=='__main__':
test()


输出:

D:\Python27\test>func11.py
____________________
1234
{}
result:True
int(eachVal) =  1234
12.34
{}
result:True
int(eachVal) =  12
123
{}
result:True
int(eachVal) =  123
12.34
{}
result:False
int(eachVal) = FAILED: invalid literal for int() with base 10: '12.34'
____________________
1234
{}
result:True
long(eachVal) =  1234
12.34
{}
result:True
long(eachVal) =  12
123
{}
result:True
long(eachVal) =  123
12.34
{}
result:False
long(eachVal) = FAILED: invalid literal for long() with base 10: '12.34'
____________________
1234
{}
result:True
float(eachVal) =  1234.0
12.34
{}
result:True
float(eachVal) =  12.34
123
{}
result:True
float(eachVal) =  123.0
12.34
{}
result:True
float(eachVal) =  12.34

D:\Python27\test>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: