Skip to main content

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 = types.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'

When creating a TinyKernel, you can pass a dict of globals to initialize the environment:

k = TinyKernel(glb={'foo':'bar'})
k('foo*2')
'barbar'

Pass name to customize the string that appears in tracebacks ("kernel" by default):

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 "<ipython-input-37-8b370e64c5cb>", line 5, in <module>
    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 "<myapp--1-57331cf14e08>", line 3, in <module>
    print(f())
  File "<myapp--1-57331cf14e08>", 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tinykernel-0.0.2.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

tinykernel-0.0.2-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file tinykernel-0.0.2.tar.gz.

File metadata

  • Download URL: tinykernel-0.0.2.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.7.1 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.3

File hashes

Hashes for tinykernel-0.0.2.tar.gz
Algorithm Hash digest
SHA256 fa8a7a4006912e1285b5fe5790fe80f1351d86afb38c4ce55655ec6e9fa2b304
MD5 56403bc74bfe89c803ed1c1119d875cd
BLAKE2b-256 aa4e63e2c225a8bf7bd3db36bca6d54b6f0ee036c92f44e0a03a10fcd33906ef

See more details on using hashes here.

File details

Details for the file tinykernel-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: tinykernel-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.7.1 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.3

File hashes

Hashes for tinykernel-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ab941d7b67a3fdbb331aeff9f0c140a8df94eed28948efe9ffc5bbda75ab95d2
MD5 dd612c5aa98d250fe5fd35085dbf8675
BLAKE2b-256 235c07a452f3ba3819b2f75d747fd90ce1225458432a30a2d77c788086754c7f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page