Re: Import from database

From: Steve Holden (steve_at_holdenweb.com)
Date: 01/25/05


To: python-list@python.org
Date: Tue, 25 Jan 2005 16:08:33 -0500

Kartic wrote:

> Steve,
>
> Hmmm... Yes, I see what you are saying. Could you post your create
> table statement so that I can create a db and play around with
> dbimport?
>
> Thanks,
> --Kartic
>
Here it is, plus also the loader program I used to suck in the standard
library (in case it's that that's faulty):

CREATE TABLE `module` (
   `modName` varchar(25) NOT NULL default '',
   `modPackage` tinyint(1) NOT NULL default '0',
   `modCompileTime` timestamp(14) NOT NULL,
   `modCode` longtext,
   PRIMARY KEY (`modName`)
) TYPE=MyISAM;

#### WARNING: email client may wrap some lines ...
#
# Establish standard library in database
#
import db
import os
import glob
import sys
import marshal

conn = db.conn()
curs = conn.cursor()

if sys.argv[1] == "-d":
     curs.execute("delete from module")
     print "All existing modules cleared"
     del sys.argv[1]

def importpy(path, modname, package):
     print "Importing", path, modname
     c = compile(file(path).read(), "db:%s" % modname, "exec")
     curs.execute("""delete from module where modName = %s""", (modname,))
     curs.execute("""insert into module (modName, modCode, modPackage,
modCompileTime)
                             values (%s, %s, %s, now())""", (modname,
marshal.dumps(c), package))
     print "Added", modname
     conn.commit()

def importall(path, modlist):
     os.chdir(path)
     for f in glob.glob("*"):
         if os.path.isdir(f):
             fn = os.path.join(path, f, "__init__.py")
             if os.path.exists(fn):
                 ml = modlist + [f]
                 importpy(fn, ".".join(ml), 1)
                 importall(os.path.join(path, f), ml)
         elif f.endswith('.py') and '.' not in f[:-3] and f !=
"__init__.py":
             importpy(os.path.join(path, f), ".".join(modlist+[f[:-3]]), 0)

if __name__ == "__main__":
     for path in sys.argv[1:]:
         importall(path, [])

regards
  Steve



Relevant Pages

  • Re: getting started
    ... Copyright Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998 ... &optional (documentation nil documentation-p)) ... (,def (definitions ',(symbol-package name)))) ... (labels ((save-one-package (out package) ...
    (comp.lang.lisp)
  • Re: REPL tutorial?
    ... Also, you could define your own def* macros, etc, to make it more ... (define-package-attribute (setf order) ... &optional (documentation nil documentation-p)) ... (labels ((save-one-package (out package) ...
    (comp.lang.lisp)
  • Re: help me pick a name for my project!
    ... because we can provide an interface to ... package, for those of our colleagues who don't ... create or replace package keyword ... def add: ...
    (comp.databases.oracle.misc)
  • help me pick a name for my project!
    ... the database that looks like a standard python ... package, for those of our colleagues who don't ... create or replace package keyword ... def add: ...
    (comp.databases.oracle.misc)