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