Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fastai/tinykernel
A minimal Python kernel so you can run Python in your Python
https://github.com/fastai/tinykernel
embedded fastai ipython kernel nbdev nbprocess python
Last synced: about 1 month ago
JSON representation
A minimal Python kernel so you can run Python in your Python
- Host: GitHub
- URL: https://github.com/fastai/tinykernel
- Owner: fastai
- License: apache-2.0
- Created: 2021-08-11T09:19:19.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-04-17T09:56:45.000Z (over 2 years ago)
- Last Synced: 2024-09-28T18:40:56.220Z (about 2 months ago)
- Topics: embedded, fastai, ipython, kernel, nbdev, nbprocess, python
- Language: Python
- Homepage: https://fastai.github.io/tinykernel/
- Size: 24.4 KB
- Stars: 39
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# tinykernel
> A minimal Python kernel, so you can run Python in your Python.All the clever stuff in this library is provided by Python's builtin `ast` module and compilation/exec/eval system, along with [IPython](https://ipython.org/)'s `CachingCompiler` which does some [deep magic](https://cprohm.de/article/better-test-output-with-ast-rewriting-and-a-patched-standard-library.html/). `tinykernel` just brings them together with a little glue.
## Install
With pip:
pip install tinykernel
With conda:
conda install -c fastai tinykernel
## How to use
This library provides a single class, `TinyKernel`, which is a tiny persistent kernel for Python code:
```python
k = TinyKernel()
```Call it, passing Python code, to have the code executed in a separate Python environment:
```python
k("a=1")
```Expressions return the value of the expression:
```python
k('a')
```1
All variables are persisted across calls:
```python
k("a+=1")
k('a')
```2
Multi-line inputs are supported. If the last line is an expression, it is returned:
```python
k("""import types
b = types.SimpleNamespace(foo=a)
b""")
```namespace(foo=2)
The original source code is stored, so `inspect.getsource` works and, tracebacks have full details.
```python
k("""def f(): pass # a comment
import inspect
inspect.getsource(f)""")
```'def f(): pass # a comment\n'
When creating a `TinyKernel`, you can pass a dict of globals to initialize the environment:
```python
k = TinyKernel(glb={'foo':'bar'})
k('foo*2')
```'barbar'
Pass `name` to customize the string that appears in tracebacks ("kernel" by default):
```python
k = TinyKernel(name='myapp')
code = '''def f():
return 1/0
print(f())'''
try: k(code)
except Exception as e: print(traceback.format_exc())
```Traceback (most recent call last):
File "", line 5, in
try: k(code)
File "/home/jhoward/git/tinykernel/tinykernel/__init__.py", line 20, in __call__
if expr: return self._run(Expression(expr.value), nm, 'eval')
File "/home/jhoward/git/tinykernel/tinykernel/__init__.py", line 12, in _run
def _run(self, p, nm, mode='exec'): return eval(compiler(p, nm, mode), self.glb)
File "", line 3, in
print(f())
File "", line 2, in f
return 1/0
ZeroDivisionError: division by zero
## Acknowledgements
Thanks to Christopher Prohm, Matthias Bussonnier, and Aaron Meurer for their helpful insights in [this twitter thread](https://twitter.com/jeremyphoward/status/1424990665746763781).