Skip to content

Instantly share code, notes, and snippets.

@ckuhl
Created June 15, 2018 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckuhl/76c3e0d59db86cc1c8666a49f0f21814 to your computer and use it in GitHub Desktop.
Save ckuhl/76c3e0d59db86cc1c8666a49f0f21814 to your computer and use it in GitHub Desktop.
Loading python code into, and then running it from, a sqlite database
import sqlite3
import sys
import imp
from peewee import SqliteDatabase, Model, TextField
db = SqliteDatabase('code.sqlite3')
class Module(Model):
code = TextField()
name = TextField()
class Meta:
database = db
db.connect()
db.create_tables([Module])
with open('demo.py') as f:
demo_module = Module(code=f.read(), name='demo')
demo_module.save()
def import_code(name, code):
module = imp.new_module(name)
exec(code) in module.__dict__
return module
m = Module.get()
import_code(m.name, m.code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment