Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > From dictionary keys to variables

Reply
Thread Tools

From dictionary keys to variables

 
 
Michael Foord
Guest
Posts: n/a
 
      10-11-2004
I use a module called COnfigObj to read my config files into python.
It exposes them as a dictionary.

I often find myself writing code like :

valuelist = ['name1', 'name2', 'name3'....]
config = ConfigObj(filename, configspec=valuelist)
name1 = config['name1']
name2 = config['name2']
name3 = config['name3']
..
..

Now is there any reason not to 'cheat' and use __main__.__dict__ to
create the variables ?

valuelist = ['name1', 'name2', 'name3'....]
config = ConfigObj(filename, configspec=valuelist)
import __main__
for entry in valuelist:
__main__.__dict__[entry] = config[entry]

(The configspec is optional - but helps with ConfigObj).

Regards,


Fuzzy

http://www.voidspace.org.uk/at;antib...thonutils.html
 
Reply With Quote
 
 
 
 
Timothy Babytch
Guest
Posts: n/a
 
      10-11-2004
Michael Foord wrote:
Why cheat? There is legal way to do that:

for entry in valuelist:
globals()[entry] = config[entry]

> Now is there any reason not to 'cheat' and use __main__.__dict__ to
> create the variables ?
>
> valuelist = ['name1', 'name2', 'name3'....]
> config = ConfigObj(filename, configspec=valuelist)
> import __main__
> for entry in valuelist:
> __main__.__dict__[entry] = config[entry]





--
Timothy Babytch
--
You know you've achieved perfection in design,
not when you have nothing more to add,
but when you have nothing more to take away.
 
Reply With Quote
 
 
 
 
Jeff Shannon
Guest
Posts: n/a
 
      10-12-2004
Michael Foord wrote:

>I use a module called COnfigObj to read my config files into python.
>It exposes them as a dictionary.
>
>I often find myself writing code like :
>
>valuelist = ['name1', 'name2', 'name3'....]
>config = ConfigObj(filename, configspec=valuelist)
>name1 = config['name1']
>name2 = config['name2']
>name3 = config['name3']
>
>


Any reason why it's necessary to define variables instead of just using
the dict? You're probably not really saving anything by doing this...
there *might* be a slight savings in typing if you use the names
numerous times, but directly accessing the dictionary makes it clearer
where your values are coming from. And dict lookups are fast, so unless
you're using these valuables repeatedly inside a tight loop, you won't
get significant speed savings. (If you *are* using some inside a tight
loop, then it's sensible to hoist the lookup out of the loop, but don't
do this until you know that that loop is a significant source of
slowdown -- as they say, premature optimization is the root of all evil.)

Jeff Shannon
Technician/Programmer
Credit International

 
Reply With Quote
 
Michael Foord
Guest
Posts: n/a
 
      10-12-2004
Timothy Babytch <(E-Mail Removed)> wrote in message news:<ckecld$1qun$(E-Mail Removed)>...
> Michael Foord wrote:
> Why cheat? There is legal way to do that:
>
> for entry in valuelist:
> globals()[entry] = config[entry]
>
> > Now is there any reason not to 'cheat' and use __main__.__dict__ to
> > create the variables ?
> >
> > valuelist = ['name1', 'name2', 'name3'....]
> > config = ConfigObj(filename, configspec=valuelist)
> > import __main__
> > for entry in valuelist:
> > __main__.__dict__[entry] = config[entry]

>
>


Ahh... I didn't realise you could do assignment into globals().........

Thanks

Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html

>
>
> --
> Timothy Babytch

 
Reply With Quote
 
Michael Foord
Guest
Posts: n/a
 
      10-12-2004
Jeff Shannon <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Michael Foord wrote:
>
> >I use a module called COnfigObj to read my config files into python.
> >It exposes them as a dictionary.
> >
> >I often find myself writing code like :
> >
> >valuelist = ['name1', 'name2', 'name3'....]
> >config = ConfigObj(filename, configspec=valuelist)
> >name1 = config['name1']
> >name2 = config['name2']
> >name3 = config['name3']
> >
> >

>
> Any reason why it's necessary to define variables instead of just using
> the dict? You're probably not really saving anything by doing this...
> there *might* be a slight savings in typing if you use the names
> numerous times, but directly accessing the dictionary makes it clearer
> where your values are coming from. And dict lookups are fast, so unless
> you're using these valuables repeatedly inside a tight loop, you won't
> get significant speed savings. (If you *are* using some inside a tight
> loop, then it's sensible to hoist the lookup out of the loop, but don't
> do this until you know that that loop is a significant source of
> slowdown -- as they say, premature optimization is the root of all evil.)
>


It's not really optimisation. There are two factors, I write lot's of
little scripts that pull there paths/config options out of a config
file in this way. With something like the above or 'globals()[entry]'
I can just cut and paste a chunk of code. That means I can add/change
the variable names (which correspond to keywords in the config file)
by just changing the name once in the 'valuelist'. I can then access
the variable directly without having to keep *typing* the
config['..... bit.. The only optimisation is on my wrist !!

Anyway, thanks. You may still be right of course...

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html

> Jeff Shannon
> Technician/Programmer
> Credit International

 
Reply With Quote
 
Jeff Shannon
Guest
Posts: n/a
 
      10-12-2004
Michael Foord wrote:

>Jeff Shannon <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>
>
>>Any reason why it's necessary to define variables instead of just using
>>the dict? You're probably not really saving anything by doing this...
>>there *might* be a slight savings in typing if you use the names
>>numerous times, but directly accessing the dictionary makes it clearer
>>where your values are coming from. And dict lookups are fast, so unless
>>you're using these valuables repeatedly inside a tight loop, you won't
>>get significant speed savings. (If you *are* using some inside a tight
>>loop, then it's sensible to hoist the lookup out of the loop, but don't
>>do this until you know that that loop is a significant source of
>>slowdown -- as they say, premature optimization is the root of all evil.)
>>
>>
>>

>
>It's not really optimisation. There are two factors, I write lot's of
>little scripts that pull there paths/config options out of a config
>file in this way. With something like the above or 'globals()[entry]'
>I can just cut and paste a chunk of code. That means I can add/change
>the variable names (which correspond to keywords in the config file)
>by just changing the name once in the 'valuelist'. I can then access
>the variable directly without having to keep *typing* the
>config['..... bit.. The only optimisation is on my wrist !!
>
>


Oof. Don't cut and paste code if you can avoid it. Instead, make
utility modules and import them. Much *much* safer. Every time you cut
and paste, you run the risk of forgetting to rename something, or
copying a name that isn't defined in your new script, or any number of
other minor little glitches. (I do much of my workday programming in an
environment that doesn't support modularization well at all, and we have
no real options for code reuse other than to either cut and paste, or
retype from scratch. I know first-hand just how easy it is to make
mistakes when cutting and pasting, because I do it all the freakin'
time.... bleah! I *so* wish I could use Python for all of that...)

And really, if you're trading readability for typing ease, you're
getting a bad deal. You're very likely to read that code many more
times than you're going to type it, so a slight addition to the
difficulty of reading and understanding it will soon make up for a bit
of extra typing. More importantly, if there's any chance that anyone
*else* will ever look at your code, it's much nicer for *them* to not
have to figure out how the heck this variable got here... And even if
your code is something that you expect to be a solitary project that'll
never be distributed to anyone else, it's still a good idea to get in
the habit of writing readable code. If nothing else, you never know
when you might want to post segments of it to c.l.py for
assistance/advice.

(All of this being strictly my personal opinion, of course, which is
probably worth about as much as you paid for it....)

Jeff Shannon
Technician/Programmer
Credit International

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
dictionary within dictionary Fox ASP General 5 03-13-2005 05:03 AM



Advertisments