module from BLOB

Fredrik Lundh fredrik at pythonware.com
Mon Feb 4 10:56:17 EST 2002


marcin andrzejewski wrote:
> is it possible to import module directly from database BLOB/CLOB field ?

something like this could work:

import imp, marshal
from my_database_api import fetch_blob_from_database

def import_from_blob(name):
    # fetch code object
    code = marshal.loads(fetch_blob_from_database(name))
    # create a new module object
    module = imp.new_module(name)
    # execute the code in the module's namespace
    exec code in vars(module)
    return module

mymod = import_from_blob("mymod")

to create the blobs, do something like:

    module = "mymod"
    text = open(module + ".py").read()
    code = compile(text, module + ".py", "exec")
    data = marshal.dumps(code)
    store_blob_in_database(module, data)

for details, see the library reference.

hope this helps!

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list