From dictionary keys to variables

Discussion in 'Python' started by Michael Foord, Oct 11, 2004.

  1. 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;antibots/pythonutils.html
     
    Michael Foord, Oct 11, 2004
    #1
    1. Advertising

  2. 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.
     
    Timothy Babytch, Oct 11, 2004
    #2
    1. Advertising

  3. Michael Foord

    Jeff Shannon Guest

    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
     
    Jeff Shannon, Oct 12, 2004
    #3
  4. Timothy Babytch <> wrote in message news:<ckecld$1qun$>...
    > 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/atlantibots/pythonutils.html

    >
    >
    > --
    > Timothy Babytch
     
    Michael Foord, Oct 12, 2004
    #4
  5. Jeff Shannon <> wrote in message news:<>...
    > 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/atlantibots/pythonutils.html

    > Jeff Shannon
    > Technician/Programmer
    > Credit International
     
    Michael Foord, Oct 12, 2004
    #5
  6. Michael Foord

    Jeff Shannon Guest

    Michael Foord wrote:

    >Jeff Shannon <> wrote in message news:<>...
    >
    >
    >>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
     
    Jeff Shannon, Oct 12, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sandeep Kanwal

    serial keys/validation keys

    sandeep Kanwal, Oct 29, 2004, in forum: C++
    Replies:
    1
    Views:
    585
    Mike Wahler
    Oct 29, 2004
  2. Harry George
    Replies:
    9
    Views:
    705
    sonal
    Jun 13, 2006
  3. Replies:
    10
    Views:
    728
    Daniel T.
    Feb 3, 2006
  4. alan
    Replies:
    3
    Views:
    365
    Victor Bazarov
    Nov 28, 2007
  5. A. Farber
    Replies:
    10
    Views:
    238
    A. Farber
    Jun 12, 2004
Loading...

Share This Page