https://github.com/loic-simon/asyncode
Emulating Python's interactive interpreter in asynchronous contexts
https://github.com/loic-simon/asyncode
Last synced: 20 days ago
JSON representation
Emulating Python's interactive interpreter in asynchronous contexts
- Host: GitHub
- URL: https://github.com/loic-simon/asyncode
- Owner: loic-simon
- License: mit
- Created: 2020-12-05T23:46:56.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2020-12-12T18:00:29.000Z (over 4 years ago)
- Last Synced: 2024-12-26T03:17:09.952Z (5 months ago)
- Language: Python
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# asyncode
[](https://pypi.org/project/asyncode)
[](https://pypi.org/project/asyncode)
[](https://pypi.org/project/asyncode)
[](https://asyncode.readthedocs.io)
[](https://travis-ci.org/github/loic-simon/asyncode)Python package for emulating Python's interactive interpreter in asynchronous contexts.
## Installation
Use the package manager [pip](https://pypi.org/project/pip) to install asyncode:
```bash
pip install asyncode
```### Dependencies
* Python **≥ 3.5** *(no CI for Python < 3.8)*
## Usage
This package's external API consists in two classes, **`AsyncInteractiveInterpreter`** and **`AsyncInteractiveConsole`**, which subclass respectively [`code.InteractiveInterpreter`](https://docs.python.org/3/library/code.html#interactive-interpreter-objects) and [`code.InteractiveConsole`](https://docs.python.org/3/library/code.html#interactive-console-objects).
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:
```py
import asyncodeclass 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 dataasync 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](https://asyncode.readthedocs.io) for more informations.
## Contributing
Pull requests are welcome. Do not hesitate to get in touch with me (see below) for any question or suggestion about this project!
## License
This work is shared under [the MIT license](LICENSE).
© 2020 Loïc Simon ([[email protected]](mailto:[email protected]))