Embedding questions
Olaf Appelt
tholap at compuserve.com
Thu Dec 9 07:06:41 EST 1999
Hi Fredrik,
thank you very much. I'll look into this.
Olaf
Fredrik Lundh <fredrik at pythonware.com> wrote in message
news:00a401bf422a$28ae6280$f29b12c2 at secret.pythonware.com...
> 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