Created
May 5, 2019 01:11
-
-
Save mumbleskates/7360a51739cc9562d887356d4270d820 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
"""Lazy hacks to enable easier usage of async functions, context managers, and iterators in the REPL.""" | |
from asyncio import set_event_loop | |
try: | |
import uvloop as loop_maker | |
except ImportError: | |
import asyncio as loop_maker | |
loop = loop_maker.new_event_loop() | |
set_event_loop(loop) | |
def aw8(task): | |
return loop.run_until_complete(task) | |
class awith(object): | |
def __init__(self, async_context): | |
self.wrapped = async_context | |
def __enter__(self, *args, **kwargs): | |
return aw8(self.wrapped.__aenter__(*args, **kwargs)) | |
def __exit__(self, *args, **kwargs): | |
return aw8(self.wrapped.__aexit__(*args, **kwargs)) | |
def aiter(async_iterable): | |
ait = aw8(async_iterable.__aiter__()) | |
try: | |
while True: | |
yield aw8(ait.__anext__()) | |
except StopAsyncIteration: | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment