A minimal Python kernel so you can run Python in your Python
Project description
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's CachingCompiler
which does some deep magic. 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:
k = TinyKernel()
Call it, passing Python code, to have the code executed in a separate Python environment:
k("a=1")
Expressions return the value of the expression:
k('a')
1
All variables are persisted across calls:
k("a+=1")
k('a')
2
Multi-line inputs are supported. If the last line is an expression, it is returned:
k("""import types
b = SimpleNamespace(foo=a)
b""")
namespace(foo=2)
The original source code is stored, so inspect.getsource
works and, tracebacks have full details.
k("""def f(): pass # a comment
import inspect
inspect.getsource(f)""")
'def f(): pass # a comment\n'
Acknowledgements
Thanks to Christopher Prohm, Matthias Bussonnier, and Aaron Meurer for their helpful insights in this twitter thread.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for tinykernel-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a48dfafbc9d849f6b4805523c793d4a071838ed8462ed38035893a3589a2542 |
|
MD5 | 8d4aef8292656eec40c2fd2fe6fe085b |
|
BLAKE2b-256 | ac24000ae30eff723416492a55a7fb559d68af1310716a959031f92a5025a89d |