鱼C论坛

 找回密码
 立即注册
查看: 1449|回复: 1

[技术交流] 第 44 讲 魔法方法 简单定制

[复制链接]
发表于 2018-4-21 11:00:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. 测试题:
  2. 0.
  3. import time as t

  4. class MyTimer:
  5.     def __init__(self):
  6.         self.prompt = '未开始计时!'
  7.         self.begin = 0
  8.         self.end = 0
  9.         self.unit = ['年','月','天','小时','分钟','秒']
  10.         self.lasted = []
  11.    
  12.     def __str__(self):
  13.         return self.prompt
  14.    
  15.     __repr__ = __str__

  16.     def __add__(self,other):
  17.         prompt = '合计时间为:'
  18.         result = []
  19.         for index in range(6):
  20.             result.append(self.lasted[index] + other.lasted[index])
  21.             if result[index] != 0:
  22.                 prompt += (str(result[index]) + self.unit[index])
  23.         return prompt
  24.    
  25.     #开始计时函数
  26.     def start(self):
  27.         self.begin = t.localtime()
  28.         self.prompt = '请调用stop()!'
  29.         print('开始计时...')

  30.     #结束计时函数
  31.     def stop(self):
  32.         if self.begin == 0:
  33.             print('请先调用start()!')
  34.         else:
  35.             self.end = t.localtime()
  36.             self.__calc()
  37.             print('停止计时...')

  38.     def __calc(self):
  39.         self.prompt = '累计时间为:'
  40.         #之前的做法
  41. ##        for index in range(6):
  42. ##            self.lasted.append(self.end[index] - self.begin[index])
  43. ##            if self.lasted[index]:
  44. ##                self.prompt += str(self.lasted[index])
  45. ##                self.prompt += self.unit[index]

  46.         #把时间相减为负考虑进来

  47.         for index in range(6):
  48.             self.lasted.append(self.end[index] - self.begin[index])
  49.             
  50.             #对lasted进行操作,把数据换算成正的时间
  51.         if self.lasted[5] <0:
  52.             self.lasted[5] += 60
  53.             self.lasted[4] -=1
  54.         if self.lasted[4] <0:
  55.             self.lasted[4] += 60
  56.             self.lasted[3] -=1
  57.         if self.lasted[3] <0:
  58.             self.lasted[3] += 24
  59.             self.lasted[2] -=1
  60.         if self.lasted[2] <0:
  61.             self.lasted[2] += 31
  62.             self.lasted[1] -=1
  63.         if self.lasted[1] <0:
  64.             self.lasted[1] += 12
  65.             self.lasted[0] -=1

  66.             #把时间加到Prompt
  67.         for index in range(6):
  68.             if self.lasted[index] != 0:
  69.                 self.prompt += (str(self.lasted[index]) + self.unit[index])        
  70.            
  71. 2.
  72. import time as t

  73. class MyTimer:
  74.     def __init__(self,func,number = 10):
  75.         self.prompt = '未开始计时!'
  76.         self.begin = 0
  77.         self.end = 0
  78.         self.lasted = 0
  79.         self.default_timer = t.perf_counter
  80.         self.func = func
  81.         self.number = number
  82.    
  83.     def __str__(self):
  84.         return self.prompt
  85.    
  86.     __repr__ = __str__

  87.     def timing(self):
  88.         self.begin = self.default_timer()
  89.         for each in range(self.number):
  90.             self.func()
  91.         self.end = self.default_timer()
  92.         self.lasted = self.end - self.begin
  93.         self.prompt = ('累计时间为:%.2f' % self.lasted + '秒')

  94.     def set_timer(self,timer):
  95.         if timer == 'perf_counter':
  96.             self.default_timer = t.perf_counter
  97.         elif timer == 'process_time':
  98.             self.default_timer = t.process_time
  99.         else:
  100.             print('请输入perf_counter或者process_time')

  101. def a():
  102.     print('i')

  103. # 记住self好似用了全局变量.
  104. # 注意先后顺序.
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-5-26 23:04:23 | 显示全部楼层
import time as t

class My_time:
    def __init__(self):
        self.prompt = '未开始计时...'
        

    def __str__(self):
        return self.prompt
   
    __repr__ = __str__
   
    def start(self):
        print('计时开始...')
        self.begin = t.localtime()

    def stop(self):
        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])
            self.prompt += str(self.lasted[index])

self.lasted和self.begin self.end没有初始的话 ,算时间不准,为什么会这样呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 22:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表