Skip to content

Instantly share code, notes, and snippets.

@ckuhl ckuhl/import_db.py
Created Jun 15, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.