Embedding questions
Fredrik Lundh
fredrik at pythonware.com
Thu Dec 9 04:45:40 EST 1999
Olaf Appelt <tholap at compuserve.com> wrote:
> Furthermore I want to avoid having module files lying around in directories.
> The code should be compiled, go into database and later be read from db to
> be executed. All that without going files. Just strings moved between Python
> API and DB.
>
> Is that possible?
Greg Stein's imputil.py [1] allows you to modify the
import statement so it can pick up code from any
data source.
an example (assuming "db" is a database object which
works pretty much like a code string dictionary).
...
class DatabaseImporter(Importer):
def __init__(self, db):
self.__db = db
def get_code(self, parent, modname, fqname):
# is it a module?
try:
code = marshal.loads(self.__db[fqname])
return 0, code
except KeyError:
pass
# is it a package?
try:
fqname = fqname + ".__init__"
code = marshal.loads(self.__db[fqname])
return 1, code
except KeyError:
pass
return None # not found in this database
# prepends the database to the import chain
DatabaseImporter(mydb).install()
...
to compile python source code into code strings, use:
string = marshal.dumps(compile(source, filename, "exec"))
...
hope this helps!
</F>
1) http://www.lyra.org/greg/python/imputil.py
More information about the Python-list
mailing list