Created
June 15, 2018 14:21
-
-
Save ckuhl/76c3e0d59db86cc1c8666a49f0f21814 to your computer and use it in GitHub Desktop.
Loading python code into, and then running it from, a sqlite database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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