Python package for emulating Python's interactive interpreter in asynchronous contexts.
Use the package manager pip to install asyncode:
pip install asyncode
- Python ≥ 3.5 (no CI for Python < 3.8)
This package's external API consists in two classes, AsyncInteractiveInterpreter
and AsyncInteractiveConsole
, which subclass respectively code.InteractiveInterpreter
and code.InteractiveConsole
.
These classes are meant to be used in already running asynchronous contexts. Minimal useful code will need to subclass provided classes to implement specific functions:
import asyncode
class MyAsyncConsole(asyncode.AsyncInteractiveConsole):
"""AsyncInteractiveConsole adapted to running environment"""
async def write(self, data):
"""Use specific function"""
await some_output_coroutine(data)
async def raw_input(self, prompt=""):
"""Use specific functions"""
if prompt:
await some_output_coroutine(prompt)
data = await some_input_coroutine()
return data
async def run_interpreter():
"""Run an interactive Python interpreter"""
console = MyAsyncConsole()
try:
await console.interact()
except SystemExit:
# Do not exit the whole program when sending "exit()" or "quit()"
await some_output_coroutine("Bye!")
Read the docs for more informations.
Pull requests are welcome. Do not hesitate to get in touch with me (see below) for any question or suggestion about this project!
This work is shared under the MIT license.
© 2020 Loïc Simon ([email protected])