Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# update status and schedule new emits
if q:
if not self._is_throttling:
self.status_event.emit(True)
loop.call_at(times[0] + self._interval, self._try_emit)
elif self._is_throttling:
self.status_event.emit(False)
self._is_throttling = bool(q)
if not q and self._source is None:
self.set_done()
self.status_event.set_done()
class Sample(Op):
__slots__ = ('_timer',)
def __init__(self, timer, source=None):
Op.__init__(self, source)
self._timer = timer
timer.connect(
self._on_timer,
self.on_source_error,
self.on_source_done)
def on_source(self, *args):
self._value = args
def _on_timer(self, *args):
if self._value is not NO_VALUE:
self.emit(*self._value)
class ArrayMax(Op):
__slots__ = ()
def on_source(self, arg):
self.emit(arg.max())
class ArraySum(Op):
__slots__ = ()
def on_source(self, arg):
self.emit(arg.sum())
class ArrayProd(Op):
__slots__ = ()
def on_source(self, arg):
self.emit(arg.prod())
class ArrayMean(Op):
__slots__ = ()
def on_source(self, arg):
self.emit(arg.mean())
class ArrayStd(Op):
__slots__ = ()
class Sum(Reduce):
__slots__ = ()
def __init__(self, start=0, source=None):
Reduce.__init__(self, operator.add, start, source)
class Product(Reduce):
__slots__ = ()
def __init__(self, start=1, source=None):
Reduce.__init__(self, operator.mul, start, source)
class Mean(Op):
__slots__ = ('_count', '_sum')
def __init__(self, source=None):
Op.__init__(self, source)
self._count = 0
self._sum = 0
def on_source(self, arg):
self._count += 1
self._sum += arg
self.emit(self._sum / self._count)
class Any(Reduce):
__slots__ = ()
def __init__(self, source=None):
Op.__init__(self, source)
self._values = []
def on_source(self, *args):
self._values.append(
args[0] if len(args) == 1 else args if args else NO_VALUE)
def on_source_done(self, source):
self.emit(self._values)
self._disconnect_from(self._source)
self._source = None
self.set_done()
class Deque(Op):
__slots__ = ('_count', '_q')
def __init__(self, count, source=None):
Op.__init__(self, source)
self._count = count
self._q = deque()
def on_source(self, *args):
self._q.append(
args[0] if len(args) == 1 else args if args else NO_VALUE)
if self._count and len(self._q) > self._count:
self._q.popleft()
self.emit(self._q)
class ArrayProd(Op):
__slots__ = ()
def on_source(self, arg):
self.emit(arg.prod())
class ArrayMean(Op):
__slots__ = ()
def on_source(self, arg):
self.emit(arg.mean())
class ArrayStd(Op):
__slots__ = ()
def on_source(self, arg):
self.emit(arg.std(ddof=1) if len(arg) > 1 else np.nan)
class ArrayAny(Op):
__slots__ = ()
def on_source(self, arg):
self.emit(arg.any())
class ArrayAll(Op):
__slots__ = ()
__slots__ = ('_prev', '_has_prev')
def __init__(self, source=None):
Op.__init__(self, source)
self._has_prev = False
def on_source(self, *args):
value = args[0] if len(args) == 1 else args if args else NO_VALUE
if self._has_prev:
self.emit(self._prev, value)
else:
self._has_prev = True
self._prev = value
class List(Op):
__slots__ = ('_values')
def __init__(self, source=None):
Op.__init__(self, source)
self._values = []
def on_source(self, *args):
self._values.append(
args[0] if len(args) == 1 else args if args else NO_VALUE)
def on_source_done(self, source):
self.emit(self._values)
self._disconnect_from(self._source)
self._source = None
self.set_done()
def init(obj, event_names: Iterable):
"""
Convenience function for initializing multiple events as members
of the given object.
Args:
event_names: Names to use for the created events.
"""
for name in event_names:
setattr(obj, name, Event(name))
def create(obj):
"""
Create an event from a async iterator, awaitable, or event
constructor without arguments.
Args:
obj: The source object. If it's already an event then it
is passed as-is.
"""
if isinstance(obj, Event):
return obj
if hasattr(obj, '__call__'):
obj = obj()
if isinstance(obj, Event):
return obj
elif hasattr(obj, '__aiter__'):
return Event.aiterate(obj)
elif hasattr(obj, '__await__'):
return Event.wait(obj)
else:
raise ValueError(f'Invalid type: {obj}')
def __init__(self, name: str = '', _with_error_done_events: bool = True):
self.error_event = None
"""
Sub event that emits errors from this event as
``emit(source, exception)``.
"""
self.done_event = None
"""
Sub event that emits when this event is done as
``emit(source)``.
"""
if _with_error_done_events:
self.error_event = Event('error', False)
self.done_event = Event('done', False)
self._slots = [] # list of [obj, weakref, func] sublists
self._name = name or self.__class__.__qualname__
self._value = NO_VALUE
self._done = False
self._source = None
def __init__(self, name: str = '', _with_error_done_events: bool = True):
self.error_event = None
"""
Sub event that emits errors from this event as
``emit(source, exception)``.
"""
self.done_event = None
"""
Sub event that emits when this event is done as
``emit(source)``.
"""
if _with_error_done_events:
self.error_event = Event('error', False)
self.done_event = Event('done', False)
self._slots = [] # list of [obj, weakref, func] sublists
self._name = name or self.__class__.__qualname__
self._value = NO_VALUE
self._done = False
self._source = None