Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Config files with different types

Reply
Thread Tools

Config files with different types

 
 
Zach Hobesh
Guest
Posts: n/a
 
      07-02-2009
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
 
Reply With Quote
 
 
 
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      07-03-2009
In message <mailman.2506.1246557392.8015.python->, 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.

 
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
Different behaviour in different struct declaration types __PaTeR C Programming 7 01-01-2009 12:13 AM
encrypting config file that aren't web config files... Ollie Riches ASP .Net 1 12-04-2008 04:59 PM
dll config and web.config and Label Expressions (binding label text to dll config settings) CSharpner ASP .Net 0 04-09-2007 09:00 PM
how to play different types of audio-video files in the browser? anil.rita@gmail.com Javascript 13 01-01-2007 05:10 AM
Default Config Files or Basic Config Steve Marshall Cisco 2 11-05-2003 10:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57