【python】简单定制一个定时器类(time转换精准秒)
来源:互联网 发布:联网控制软件 编辑:程序博客网 时间:2024/06/10 19:15
#代码示例(原):import time as tclass MyTimer(): def __init__(self): self.unit = ['年', '月', '日', '小时', '分', '秒'] #单位 self.prompt = '未开始计时!' self.lasted = [] self.begin = 0 self.end = 0 def __str__(self): return self.prompt __repr__ = __str__ #赋值起到相同的def定义作用 def __add__(self, other): prompt = '总共运行了' 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 #开始计时 def start(self): self.begin = t.localtime() self.prompt = '提示:请先调用 stop() 停止计时!' print('计时开始...') print(self.begin) #time.struct_time()元组 #停止计时 def stop(self): if not self.begin: print('提示:请先调用 start() 进行计时!') else: self.end = t.localtime() print(self.end) #time.struct_time()元组 self._calc() print('计时结束...') #内部方法:计算运行时间 def _calc(self): self.lasted = [] self.prompt = '总共运行了' for index in range(6): #控制循环次数6次 self.lasted.append(self.end[index] - self.begin[index]) if self.lasted[index]: self.prompt += (str(self.lasted[index]) + self.unit[index]) #为下一轮计时初始化类的属性变量 self.begin = 0 self.end = 0 print(self.prompt)运行:
>>> t1 = MyTimer()
>>> t1
未开始计时!
>>> t1.start()
计时开始...
>>> t1.stop()
总共运行了4秒
计时结束...
>>> t1
总共运行了4秒
>>> t1
总共运行了4秒
>>> t1 = MyTimer()
>>> t1.stop()
提示:请先调用 start() 进行计时!
>>> t1.start()
计时开始...
>>> t2 = MyTimer()
>>> t2.start()
计时开始...
>>> t1.stop()
总共运行了1分-41秒 #[bug] 当时间有进位时,目前代码写法中直接相减得不到正确结果。
计时结束...
>>> t2.stop()
总共运行了13秒
计时结束...
【优化后】
#代码示例(优化后):import time as t#时间系数的常量定义d_sec = 60 * 60 * 24h_sec = 60 * 60m_sec = 60s_sec = 1class MyTimer(): def __init__(self): #self.unit = ['年', '月', '日', '小时', '分', '秒'] #单位 self.unit = ['秒', '分', '小时', '日'] #单位 self.prompt = '未开始计时!' self.lasted = [] self.begin = 0 self.end = 0 def __str__(self): return self.prompt __repr__ = __str__ #赋值起到相同的def定义作用 def __add__(self, other): prompt = '总共运行了' 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 #开始计时 def start(self): #精度范围:日时分秒 (月份不均故暂不做精度) time_ss = t.localtime() self.begin = time_ss.tm_mday * d_sec + time_ss.tm_hour * h_sec + time_ss.tm_min * m_sec + time_ss.tm_sec * s_sec self.prompt = '提示:请先调用 stop() 停止计时!' print('计时开始...') print(self.begin) #秒数 #停止计时 def stop(self): if not self.begin: print('提示:请先调用 start() 进行计时!') else: #精度范围:日时分秒 (月份不均故暂不做精度) time_tt = t.localtime() self.end = time_tt.tm_mday * d_sec + time_tt.tm_hour * h_sec + time_tt.tm_min * m_sec + time_tt.tm_sec * s_sec print(self.end) #秒数 self._calc() print('计时结束...') #内部方法:计算运行时间 def _calc(self): self.lasted = [] self.prompt = '总共运行了' ran = 0 #控制遍历单位的进位,如从分钟进位到小时 sec_diff = self.end - self.begin min_diff = sec_diff // 60 sec_diff %= 60 if sec_diff > 0: self.lasted.append(sec_diff) print('sec_diff: %d' % sec_diff) ran = 1 hou_diff = min_diff // 60 min_diff %= 60 if min_diff > 0: self.lasted.append(min_diff) print('min_diff: %d' % min_diff) ran = 2 day_diff = hou_diff // 24 hou_diff %= 24 if hou_diff > 0: self.lasted.append(hou_diff) print('hou_diff: %d' % hou_diff) ran = 3 for index in range(ran): if self.lasted[index]: self.prompt += (str(self.lasted[index]) + self.unit[index])## for index in range(6): #控制循环次数6次## self.lasted.append(self.end[index] - self.begin[index])## if self.lasted[index]:## self.prompt += (str(self.lasted[index]) + self.unit[index]) #为下一轮计时初始化类的属性变量 self.begin = 0 self.end = 0 print(self.prompt)运行:
>>> t1= MyTimer()
>>> t2= MyTimer()
>>> t1.start()
计时开始...
255002
>>> t2.start()
计时开始...
255004
>>> t1.stop()
255109
sec_diff: 47
min_diff: 1
总共运行了47秒1分 #顺序暂时没调整,这个不在话下,次序问题
计时结束...
>>> t2.stop()
255111
sec_diff: 47
min_diff: 1
总共运行了47秒1分
计时结束...
阅读全文
0 0
- 【python】简单定制一个定时器类(time转换精准秒)
- Python 简单的定制一个计时器
- python 可定制化 定时器
- 一个简单的时间类的定制
- Python datetime / time 转换
- python time.sleep(t) t为秒
- Python time time()方法返回的是秒
- 一个简单的定时器
- 一个简单的定时器
- python time,datetime,string转换
- python time,datetime,string转换
- python datetime,time转换操作
- python time,datetime,string转换
- 一个简单的整点定时器
- 一个最简单的定时器
- 简单转换,给定字符串转成秒,秒转成字符串
- Python定时器的一个实现
- Python开启一个线程定时器
- 餐饮软件之易石软件简介
- R语言:批量导入文件夹中的文件
- VC API 分割字符串
- 利用java socket和sampled实现点对点即时语音通信
- SSRF原理以及漏洞挖掘方法
- 【python】简单定制一个定时器类(time转换精准秒)
- printf()输出格式
- 在Eclipse中安装Pydev来创建Python工程
- 排序算法汇总及简单实现
- Ubuntu 14.04安装Python 3.x
- driud配置
- Python 爬虫实战:分析豆瓣中最新电影的影评
- 2017暑假总结
- Bootstrap组件之下拉菜单