Config files with different types

Discussion in 'Python' started by Zach Hobesh, Jul 2, 2009.

  1. Zach Hobesh

    Zach Hobesh Guest

    Hi all,

    I've written a function that reads a specifically formatted text file
    and spits out a dictionary.  Here's an example:

    config.txt:

    Destination = C:/Destination
    Overwrite = True


    Here's my function that takes 1 argument (text file)

    the_file = open(textfile,'r')
    linelist = the_file.read().split('\n')
    the_file.close()
    configs = {}
    for line in linelist:
    try:
    key,value = line.split('=')
    key.strip()
    value.strip()
    key.lower()
    value.lower()
    configs[key] = value

    except ValueError:
    break

    so I call this on my config file, and then I can refer back to any
    config in my script like this:

    shutil.move(your_file,configs['destination'])

    which I like because it's very clear and readable.

    So this works great for simple text config files. Here's how I want
    to improve it:

    I want to be able to look at the value and determine what type it
    SHOULD be. Right now, configs['overwrite'] = 'true' (a string) when
    it might be more useful as a boolean. Is there a quick way to do
    this? I'd also like to able to read '1' as an in, '1.0' as a float,
    etc...

    I remember once I saw a script that took a string and tried int(),
    float() wrapped in a try except, but I was wondering if there was a
    more direct way.

    Thanks in advance,

    Zach
     
    Zach Hobesh, Jul 2, 2009
    #1
    1. Advertising

  2. In message <>, Zach
    Hobesh wrote:

    > I want to be able to look at the value and determine what type it
    > SHOULD be. Right now, configs['overwrite'] = 'true' (a string) when
    > it might be more useful as a boolean.


    Typically the type should be what you expect, not what is given. For
    example, it doesn't make sense for your configs["overwrite"] setting to be
    "red", does it?

    A reasonable solution might be to have a table of expected types for each
    config item keyword, e.g.

    configs_types = \
    {
    ...
    "overwrite" : lambda x : x[0] not in set("nNfF"),
    ...
    }

    Then you can just do something like

    configs[keyword] = configs_types[keyword](configs[keyword])

    with appropriate trapping of ValueError exceptions.
     
    Lawrence D'Oliveiro, Jul 3, 2009
    #2
    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. CSharpner
    Replies:
    0
    Views:
    1,060
    CSharpner
    Apr 9, 2007
  2. Ollie Riches
    Replies:
    1
    Views:
    1,651
    Gregory A. Beamer
    Dec 4, 2008
  3. __PaTeR
    Replies:
    7
    Views:
    486
    Barry Schwarz
    Jan 1, 2009
  4. Nick Johnson

    Windows Application App.config Files vs. web.config

    Nick Johnson, Mar 12, 2007, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    674
    Nick Johnson
    Mar 12, 2007
  5. Jabba Laci
    Replies:
    1
    Views:
    130
    Roy Smith
    Oct 6, 2013
Loading...

Share This Page