|
|
10鱼币
本帖最后由 不二如是 于 2016-9-1 16:46 编辑
mac 版本3.6也不行
- #计时器
- import time as t #引入时间模块
- class MyTimer():
- ###考虑到现在情况下,prompt定义顺序靠后,会由输入错误引起问题
- ###类的方法名和属性名一样 会报错 故而把start->begin .stop->end
- def __init__(self):
- ####便于观看,加入年,月,日
- self.unit = ['年','月','日','小时','分','秒']
- self.prompt = '未开始计时'
- self.lasted = []
- self.begin = 0
- self.end = 0
-
- ##重写魔法方法__str__
- def __str__(self):
- return self.prompt
-
- ##重写__repr__
- __repr__ = __str__
-
- #开始计时
- def start(self):
- self.begin = t.localtime()
- #####防止没调用stop()直接__str__
- self.prompt = '请调用stop()停止计时'
- print ('开始计时')
-
- #停止计时
- def stop(self):
- #####防止没start就stop
- if not self.begin:
- print ('提示:请先start()再stop()!')
- else:
- self.end = t.localtime()
- self._calc()
- print ('计时结束')
-
-
- #内部方法,计算运行时间
- def _calc(self):
- self.lasted = []
- self.prompt = '总共运行了'
- for index in range(6):
- self.lasted.append(self.end[index] - self.begin[index])
- if(self.lasted[index]):
- ####防止出现一串 ex:000003 ->3
- self.prompt += str(self.lasted[index])
- print(self.prompt)
- #####为下一轮初始化
- self.begin = 0
- self.end = 0
- ###### __add__ 两个变量时间相加
- def __add__(self):
- prompt = '总共运行了'
- result = []
- for index in range(6):
- result.append(self.lasted[index] + other.lasted[index])
- if result[index]:
- prompt += (str(result[index]) + self.init[index])
- return prompt
-
复制代码 |
|