timer.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # This Python file uses the following encoding: utf-8
  2. import time
  3. from datetime import datetime, timedelta
  4. from functools import wraps
  5. def timer(function):
  6. """
  7. 装饰器 打印函数时间时间
  8. """
  9. @wraps(function)
  10. def function_timer(*args, **kwargs):
  11. t0 = time.time()
  12. result = function(*args, **kwargs)
  13. t1 = time.time()
  14. print('%s: %s s' % (function.__name__, str(round(t1 - t0, 10))))
  15. return result
  16. return function_timer
  17. def future_time(string):
  18. """
  19. Args:
  20. string (str): Such as 14:59.
  21. Returns:
  22. datetime.datetime: Time with given hour, minute in the future.
  23. """
  24. hour, minute = [int(x) for x in string.split(':')]
  25. future = datetime.now().replace(hour=hour, minute=minute, second=0, microsecond=0)
  26. future = future + timedelta(days=1) if future < datetime.now() else future
  27. return future
  28. def past_time(string):
  29. """
  30. Args:
  31. string (str): Such as 14:59.
  32. Returns:
  33. datetime.datetime: Time with given hour, minute in the past.
  34. """
  35. hour, minute = [int(x) for x in string.split(':')]
  36. past = datetime.now().replace(hour=hour, minute=minute, second=0, microsecond=0)
  37. past = past - timedelta(days=1) if past > datetime.now() else past
  38. return past
  39. def future_time_range(string):
  40. """
  41. Args:
  42. string (str): Such as 23:30-06:30.
  43. Returns:
  44. tuple(datetime.datetime): (time start, time end).
  45. """
  46. start, end = [future_time(s) for s in string.split('-')]
  47. if start > end:
  48. start = start - timedelta(days=1)
  49. return start, end
  50. def time_range_active(time_range):
  51. """
  52. Args:
  53. time_range(tuple(datetime.datetime)): (time start, time end).
  54. Returns:
  55. bool:
  56. """
  57. return time_range[0] < datetime.now() < time_range[1]
  58. class Timer:
  59. def __init__(self, limit, count=0):
  60. """
  61. Args:
  62. limit (int, float): Timer limit
  63. count (int): Timer reach confirm count. Default to 0.
  64. When using a structure like this, must set a count.
  65. Otherwise it goes wrong, if screenshot time cost greater than limit.
  66. if self.appear(MAIN_CHECK):
  67. if confirm_timer.reached():
  68. pass
  69. else:
  70. confirm_timer.reset()
  71. Also, It's a good idea to set `count`, to make alas run more stable on slow computers.
  72. Expected speed is 0.35 second / screenshot.
  73. """
  74. self.limit = limit
  75. self.count = count
  76. self._current = 0
  77. self._reach_count = count
  78. def start(self):
  79. if not self.started():
  80. self._current = time.time()
  81. self._reach_count = 0
  82. return self
  83. def started(self):
  84. return bool(self._current)
  85. def current(self):
  86. """
  87. Returns:
  88. float
  89. """
  90. if self.started():
  91. return time.time() - self._current
  92. else:
  93. return 0.
  94. def reached(self):
  95. """
  96. Returns:
  97. bool
  98. """
  99. self._reach_count += 1
  100. return time.time() - self._current > self.limit and self._reach_count > self.count
  101. def reset(self):
  102. self._current = time.time()
  103. self._reach_count = 0
  104. return self
  105. def clear(self):
  106. self._current = 0
  107. self._reach_count = self.count
  108. return self
  109. def reached_and_reset(self):
  110. """
  111. Returns:
  112. bool:
  113. """
  114. if self.reached():
  115. self.reset()
  116. return True
  117. else:
  118. return False
  119. def wait(self):
  120. """
  121. Wait until timer reached.
  122. """
  123. diff = self._current + self.limit - time.time()
  124. if diff > 0:
  125. time.sleep(diff)
  126. def show(self):
  127. from module.logger import logger
  128. logger.info(str(self))
  129. def __str__(self):
  130. return f'Timer(limit={round(self.current(), 3)}/{self.limit}, count={self._reach_count}/{self.count})'
  131. __repr__ = __str__