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>
相关文章推荐
- python基础教程共60课-第10课变量2
- python基础教程共60课-第9课random
- Python中的类概念理解
- python基础教程共60课-第8课while
- python 中的电子邮箱的操作
- (python)下载喜马拉雅电台的音频
- 5.1.7 timezone对象
- Python将时间为AM的转换为24小时制
- python,random.random()和random.Random()的区别联系
- 实现Python str.split功能的小函数
- python读取图像
- Python爬虫进阶一之爬虫框架Scrapy安装配置
- 定义 Model兼容python2.x和python3.x
- Mac 环境下下配置opencv +python
- python 字符串与16进制互转
- 通过python的obbc模块连接mysql数据库
- Climbing Stairs-python
- Python火车票代码
- Python 异常处理
- Python 文件I/O