error on importing variable value

int32bit at yahoo.com int32bit at yahoo.com
Sun Dec 30 11:44:15 EST 2007


On Dec 29, 6:05 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Sat, 29 Dec 2007 15:31:30 -0800 (PST), int32... at yahoo.com declaimed
> the following in comp.lang.python:
>
>
>
> > I can't figure out why this doesn't work. Any ideas appreciated.
>
> > conn = MySQLdb.connect (db = "vocab")
>
>         This is a keyword parameter association, the parameter named "db" is
> given the string value "vocab".
>
> > import defs
> > conn = MySQLdb.connect (defs.connect)
> > where defs.py is
>
> > connect = 'db = "vocab"'
>
>         This is a string.  You'd get the same error using:
>
> conn = MySQLdb.connect('db="vocab"')
>
> as you are giving the entire string to whatever the first defined
> parameter in .connect() is...
>
> Change defs.py to:
>
> -=-=-=-=-
> connect = { "db" : "vocab" }
>
> and change the connection to read:
>
> -=-=-=-=-
> conn = MySQLdb.connect(**defs.connect)
>
> to force keyword unpacking of the dictionary
>
> --
>         Wulfraed        Dennis Lee Bieber               KD6MOG
>         wlfr... at ix.netcom.com              wulfr... at bestiaria.com
>                 HTTP://wlfraed.home.netcom.com/
>         (Bestiaria Support Staff:               web-a... at bestiaria.com)
>                 HTTP://www.bestiaria.com/


Thanks. This works great. As a side note, it can also be extended so
that if defs.py is

connect = { "host" : "localhost", "user" : "joey", "db" : "vocab" }

the MySQLdb.connect(**defs.connect) still works.








More information about the Python-list mailing list