Commit 4f6a9e09 authored by Kirill Smelkov's avatar Kirill Smelkov

time: Add type annotations to function arguments where possible

parent 106c1b95
...@@ -44,7 +44,7 @@ def pysleep(double dt): ...@@ -44,7 +44,7 @@ def pysleep(double dt):
# #
# Note: there is no way to stop created ticker. # Note: there is no way to stop created ticker.
# Note: for dt <= 0, contrary to Ticker, tick returns nilchan instead of panicking. # Note: for dt <= 0, contrary to Ticker, tick returns nilchan instead of panicking.
def tick(dt): # -> chan time def tick(double dt): # -> chan time
if dt <= 0: if dt <= 0:
return pynilchan return pynilchan
return Ticker(dt).c return Ticker(dt).c
...@@ -52,14 +52,14 @@ def tick(dt): # -> chan time ...@@ -52,14 +52,14 @@ def tick(dt): # -> chan time
# after returns channel connected to dt timer. # after returns channel connected to dt timer.
# #
# Note: with after there is no way to stop/garbage-collect created timer until it fires. # Note: with after there is no way to stop/garbage-collect created timer until it fires.
def after(dt): # -> chan time def after(double dt): # -> chan time
return Timer(dt).c return Timer(dt).c
# after_func arranges to call f after dt time. # after_func arranges to call f after dt time.
# #
# The function will be called in its own goroutine. # The function will be called in its own goroutine.
# Returned timer can be used to cancel the call. # Returned timer can be used to cancel the call.
def after_func(dt, f): # -> Timer def after_func(double dt, f): # -> Timer
return Timer(dt, f=f) return Timer(dt, f=f)
...@@ -75,7 +75,7 @@ cdef class Ticker: ...@@ -75,7 +75,7 @@ cdef class Ticker:
cdef sync.Mutex _mu cdef sync.Mutex _mu
cdef bint _stop cdef bint _stop
def __init__(self, dt): def __init__(Ticker self, double dt):
if dt <= 0: if dt <= 0:
pypanic("ticker: dt <= 0") pypanic("ticker: dt <= 0")
self.c = pychan(1) # 1-buffer -- same as in Go self.c = pychan(1) # 1-buffer -- same as in Go
...@@ -86,7 +86,7 @@ cdef class Ticker: ...@@ -86,7 +86,7 @@ cdef class Ticker:
# stop cancels the ticker. # stop cancels the ticker.
# #
# It is guaranteed that ticker channel is empty after stop completes. # It is guaranteed that ticker channel is empty after stop completes.
def stop(self): def stop(Ticker self):
self._mu.lock() self._mu.lock()
self._stop = True self._stop = True
...@@ -95,7 +95,7 @@ cdef class Ticker: ...@@ -95,7 +95,7 @@ cdef class Ticker:
self.c.recv() self.c.recv()
self._mu.unlock() self._mu.unlock()
def _tick(self): def _tick(Ticker self):
while 1: while 1:
# XXX adjust for accumulated error δ? # XXX adjust for accumulated error δ?
pysleep(self._dt) pysleep(self._dt)
...@@ -129,7 +129,7 @@ cdef class Timer: ...@@ -129,7 +129,7 @@ cdef class Timer:
cdef double _dt # +inf - stopped, otherwise - armed cdef double _dt # +inf - stopped, otherwise - armed
cdef int _ver # current timer was armed by n'th reset cdef int _ver # current timer was armed by n'th reset
def __init__(self, dt, f=None): def __init__(Timer self, double dt, f=None):
self._f = f self._f = f
self.c = pychan(1) if f is None else pynilchan self.c = pychan(1) if f is None else pynilchan
self._dt = INFINITY self._dt = INFINITY
...@@ -149,7 +149,7 @@ cdef class Timer: ...@@ -149,7 +149,7 @@ cdef class Timer:
# Note: similarly to Go, if Timer is used with function - it is not # Note: similarly to Go, if Timer is used with function - it is not
# guaranteed that after stop the function is not running - in such case # guaranteed that after stop the function is not running - in such case
# the caller must explicitly synchronize with that function to complete. # the caller must explicitly synchronize with that function to complete.
def stop(self): # -> canceled def stop(Timer self): # -> canceled
self._mu.lock() self._mu.lock()
if self._dt == INFINITY: if self._dt == INFINITY:
...@@ -169,7 +169,7 @@ cdef class Timer: ...@@ -169,7 +169,7 @@ cdef class Timer:
# reset rearms the timer. # reset rearms the timer.
# #
# the timer must be either already stopped or expired. # the timer must be either already stopped or expired.
def reset(self, dt): def reset(Timer self, double dt):
self._mu.lock() self._mu.lock()
if self._dt != INFINITY: if self._dt != INFINITY:
self._mu.unlock() self._mu.unlock()
...@@ -180,7 +180,7 @@ cdef class Timer: ...@@ -180,7 +180,7 @@ cdef class Timer:
self._mu.unlock() self._mu.unlock()
def _fire(self, dt, ver): def _fire(Timer self, double dt, int ver):
pysleep(dt) pysleep(dt)
self._mu.lock() self._mu.lock()
if self._ver != ver: if self._ver != ver:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment