Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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))
Ejemplo n.º 3
0
 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))
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
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'])
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
 def gentype(a, b, *cs, **fs):
     """my docstring"""
     return_((yield sum([a, b, sum(cs), sum(fs.values()), a])))
Ejemplo n.º 10
0
def myquery():
    return_((yield snug.GET('my/url')))
Ejemplo n.º 11
0
 def __iter__(self):
     redirect = yield '/posts/latest'
     redirect = yield redirect.split(':')[1]
     response = yield redirect.split(':')[1]
     return_(response.decode('ascii'))
Ejemplo n.º 12
0
def emptygen():
    if False:
        yield
    return_(99)
Ejemplo n.º 13
0
def oneway_delegator(gen):
    yielder = yield_from(gen)
    for item in yielder:
        with yielder:
            yield item
    return_(yielder.result)