Added generator decorator

This commit is contained in:
aewens 2019-05-18 19:50:01 -05:00
parent 333545c4e5
commit b9c222c496
3 changed files with 9 additions and 7 deletions

View File

@ -1,12 +1,10 @@
from abots.helpers import coroutine
class CoroEvent:
def __init__(self):
self._set = False
self._targets = list()
self._events = self._loop()
self._events.__next__()
@coroutine
def _loop(self):
try:
while True:

View File

@ -4,4 +4,5 @@ from abots.helpers.numbers import clamp, randfloat, isnumeric
from abots.helpers.general import eprint, deduce, noop, cast, get_digit, obtain
from abots.helpers.general import utc_now, utc_now_timestamp
from abots.helpers.logging import Logger
from abots.helpers.black_magic import infinitedict, debugger, coroutine
from abots.helpers.black_magic import infinitedict, debugger
from abots.helpers.black_magic import coroutine, generator

View File

@ -1,3 +1,4 @@
from abots.events import CoroEvent
from collections import defaultdict
from functools import wraps
@ -21,16 +22,18 @@ def coroutine(func):
@wraps(func)
def wrapper_coroutine(*args, **kwargs):
coro = func(*args, **kwargs)
coro.send(None)
coro.__next__()
return coro
return wrapper_coroutine
@coroutine
def generator(func):
@wraps(func)
@coroutine
def wrapper_generator(*args, **kwargs):
event = CoroEvent()
try:
yield from func(*args, **kwargs)
while True:
yield from func(event, *args, **kwargs)
except GeneratorExit:
pass
return wrapper_generator