Commits

Jason R. Coombs committed f7624e8

Updated code to more current conventions

Comments (0)

Files changed (1)

jaraco/windows/timers.py

 __author__ = 'Jason R. Coombs <jaraco@jaraco.com>'
 
 class WaitableTimer:
+	"""
+	t = WaitableTimer()
+	t.set(None, 10) # every 10 seconds
+	t.wait_for_signal() # 10 seconds elapses
+	t.stop()
+	t.wait_for_signal(20) # 20 seconds elapses (timeout occurred)
+	"""
 	def __init__(self):
-		self.SignalEvent = win32event.CreateEvent(None, 0, 0, None)
-		self.StopEvent = win32event.CreateEvent(None, 0, 0, None)
+		self.signal_event = win32event.CreateEvent(None, 0, 0, None)
+		self.stop_event = win32event.CreateEvent(None, 0, 0, None)
 
-	def Set(self, dueTime, period):
-		thread.start_new_thread(self._SignalLoop_, (dueTime, period))
+	def set(self, due_time, period):
+		thread.start_new_thread(self._signal_loop, (due_time, period))
 
-	def Stop(self):
-		win32event.SetEvent(self.StopEvent)
+	def stop(self):
+		win32event.SetEvent(self.stop_event)
 
-	def WaitForSignal(self, timeoutSeconds = None):
-		if not timeoutSeconds:
-			timeoutMilliseconds = win32event.INFINITE
-		else:
-			timeoutMilliseconds = int(timeoutSeconds * 1000)
-		win32event.WaitForSingleObject(self.SignalEvent, timeoutMilliseconds)
+	def wait_for_signal(self, timeout = None):
+		"""
+		wait for the signal; return after the signal has occurred or the
+		timeout in seconds elapses.
+		"""
+		timeout_ms = int(timeout*1000) if timeout else win32event.INFINITE
+		win32event.WaitForSingleObject(self.signal_event, timeout_ms)
 
-	def _SignalLoop_(self, dueTime, period):
-		if not dueTime and not period:
-			raise ValueError("dueTime or period must be non-zero")
+	def _signal_loop(self, due_time, period):
+		if not due_time and not period:
+			raise ValueError("due_time or period must be non-zero")
 		try:
-			if not dueTime:
-				dueTime = time.time() + period
-			if dueTime:
-				self._Wait_(dueTime - time.time())
+			if not due_time:
+				due_time = time.time() + period
+			if due_time:
+				self._wait(due_time - time.time())
 			while period:
-				dueTime += period
-				self._Wait_(dueTime - time.time())
+				due_time += period
+				self._wait(due_time - time.time())
 		except Exception:
 			pass
 
 		#we're done here, just quit
 
-	def _Wait_(self, seconds):
+	def _wait(self, seconds):
 		milliseconds = int(seconds*1000)
 		if milliseconds > 0:
-			res = win32event.WaitForSingleObject(self.StopEvent, milliseconds)
+			res = win32event.WaitForSingleObject(self.stop_event, milliseconds)
 			if res == win32event.WAIT_OBJECT_0: raise Exception
 			if res == win32event.WAIT_TIMEOUT: pass
-		win32event.SetEvent(self.SignalEvent)
+		win32event.SetEvent(self.signal_event)
 
-	def getEvenDueTime(self, period):
+	@staticmethod
+	def get_even_due_time(period):
 		now = time.time()
 		return now - (now % period)