Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
try:
if ref:
obj = ref()
if obj is None:
if func:
func(*args)
else:
if func:
func(obj, *args)
else:
obj(*args)
except Exception as error:
if len(self.error_event):
self.error_event.emit(self, error)
else:
Event.logger.exception(
f'Value {args} caused exception for event {self}')
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, maximum, interval, cost_func=None, source=None):
Op.__init__(self, source)
self.status_event = Event('throttle_status')
"""
Sub event that emits ``True`` when throttling starts and ``False``
when throttling ends.
"""
self._maximum = maximum
self._interval = interval
self._cost_func = cost_func
self._q = deque() # deque of (args, cost) tuples
self._time_q = deque() # deque of previous emit times
self._cost_q = deque() # deque of costs of previous emits
self._is_throttling = False