Script Edge Cases

For a few hours, I debated whether to replace await expressions with asyncio.run() calls when converting Jupyter notebooks into Python scripts.

I experimented a bit with the following regular expression, which can detect occurrences of the await keyword that are not enclosed in single or double quotes (thanks to https://stackoverflow.com/a/23667311/192092). You can combine this pattern with AWAIT_PATTERN.sub(f, text) where the match is positive if match.group(1) != None for f(match).

AWAIT_PATTERN = re.compile(r'\'[^\']+\'|"[^"]+"|(\bawait\b)')

Ultimately, I decided against it, as a solution involving regular expressions will not cover cases such as async with and async for.

I also debated whether to wrap the notebook code inside an async function if await is detected.

After reading an article that says every Jupyter Notebook cell runs in an async loop, I decided to use the wrapper solution. So CrossCompute should be able to support most if not all cases of using await and other asynchronous techniques in Jupyter notebooks, as long as the code runs without error in Jupyter.