python——初级定时器
2016-07-29 11:45
232 查看
import time as t class MyTimer(): def __init__(self): self.unit=['year','month','day','hour','minute','sec'] self.prompt='未开始'; self.lasted=[] self.begin=0 self.end=0 def __str__(self): return self.prompt __repr__=__str__ def __add__(self,other): prompt="totle time:" result=[] for index in range(6): result.append(self.lasted[index]+other.lasted[index]) if result[index]: prompt+=(str(result[index])+self.unit[index]) return prompt #start def start(self): self.begin=t.localtime() self.prompt='please stop'; print('start') #end def stop(self): if not self.begin: print('please start') else: self.end=t.localtime() self._calc() print('end') #inner function calulate runtime def _calc(self): self.lasted=[] self.prompt='runtime:' for index in range(6): self.lasted.append(self.end[index]-self.begin[index]) if self.lasted[index]: self.prompt+=(str(self.lasted[index])+self.unit[index]) print(self.prompt) self.begin=0 self.end=0
>>> t1=MyTimer() >>> t1.start() start >>> t2.stop() Traceback (most recent call last): File "<pyshell#63>", line 1, in <module> t2.stop() NameError: name 't2' is not defined >>> t1.stop() runtime:10sec end >>> t2=MyTimer() >>> t2.start() start >>> t2.stop() runtime:4sec end >>> t1+t2 'totle time:14sec' >>>
有时候会出现负数,以后再改吧……
相关文章推荐
- python数字图像处理(12):基本图形的绘制
- python数字图像处理(11):图像自动阈值分割
- python数字图像处理(10):图像简单滤波
- python数字图像处理(9):直方图与均衡化
- python数字图像处理(8):对比度与亮度调整
- python数字图像处理(7):图像的形变与缩放
- python数字图像处理(6):图像的批量处理
- python数字图像处理(5):图像的绘制
- python数字图像处理(4):图像数据类型及颜色空间转换
- python学习 匿名函数
- python数字图像处理(3):图像像素的访问与裁剪
- python数字图像处理(2):图像的读取、显示与保存
- python数字图像处理(1):环境安装与配置
- 用python简单处理图片(3):添加水印
- Latex编辑功能的简陋实现
- Python使用Ckan API查找并展示特定的resource内容
- python3.4连接mysql数据库
- 用python简单处理图片(5):图像直方图
- 用python简单处理图片(4):图像中的像素访问
- 用python简单处理图片(2):图像通道\几何变换\裁剪