def delegator(gen): yielder = yield_from(gen) for item in yielder: try: with yielder: yielder.send((yield item)) except GeneratorExit: return_('foo') return_(yielder.result)
def _exec(executable): # type: (Executable) -> t.Generator if isinstance(executable, str): return_((yield executable)) elif isinstance(executable, Query): return_( load(executable.cls, executable.selections, (yield str(executable)))) else: raise NotImplementedError('not executable: ' + repr(executable))
def __iter__(self): response = yield ('max: {}'.format(self.max_items), 'cursor: {}'.format(self.cursor)) objs = response['objects'] next_cursor = response['next_cursor'] if next_cursor is None: next_query = None else: next_query = mylist(max_items=self.max_items, cursor=next_cursor) return_(snug.Page(objs, next_query=next_query))
def try_until_even(req): """an example relay""" response = yield req while response % 2: try: response = yield 'NOT EVEN!' except GeneratorExit: return except ValueError: yield 'caught ValueError' return_(response)
def try_until_positive(req): """an example relay""" response = yield req while response < 0: try: response = yield 'NOT POSITIVE!' except GeneratorExit: return except ValueError: yield 'caught ValueError' return_(response)
def __iter__(self): val = self.start while val < 100: try: sent = yield val except GeneratorExit: return except ValueError: yield 'caught ValueError' if sent > val: val = sent return_(val * 3)
def middleware(url, query_str): # type: (str, str) -> snug.Query[Dict[str, JSON]] response = yield snug.Request( 'POST', url, content=json.dumps({'query': query_str}).encode('ascii'), headers={'Content-Type': 'application/json'} ) if response.status_code >= 400: raise HTTPError(response) content = json.loads(response.content.decode('utf-8')) if 'errors' in content: raise ErrorResponse(**content) return_(content['data'])
def mymax(val): """an example generator function""" while val < 100: try: sent = yield val except GeneratorExit: return except ValueError: sent = yield 'caught ValueError' except TypeError: return_('mymax: type error') if sent > val: val = sent return_(val * 3)
def gentype(a, b, *cs, **fs): """my docstring""" return_((yield sum([a, b, sum(cs), sum(fs.values()), a])))
def myquery(): return_((yield snug.GET('my/url')))
def __iter__(self): redirect = yield '/posts/latest' redirect = yield redirect.split(':')[1] response = yield redirect.split(':')[1] return_(response.decode('ascii'))
def emptygen(): if False: yield return_(99)
def oneway_delegator(gen): yielder = yield_from(gen) for item in yielder: with yielder: yield item return_(yielder.result)