lightweight human-readable config?
Larry Bates
lbates at swamisoft.com
Fri Oct 31 15:45:13 EST 2003
You can have defaults in configparser .INI file if you like.
import ConfigParser
ini_filename='program.ini'
defaults={'key1':'value1','key2':'value2')
#
# This will initialize the ConfigParser with dictionary
# of key,value defaults.
INI=ConfigParser.ConfigParser(defaults)
#
# Make sure .INI file exists
#
if not os.path.exists:
print "Unable to locate .INI filename=%s" % ini_filename
sys.exit(2)
#
# Now read the .INI file's contents
#
INI.read(ini_filename)
section='INIT'
option='debug'
try: debug=INI.getboolean(section, option)
except: debug=0
option='trace'
try: trace=INI.getboolean(section, option)
except: trace=0
section='RUNTIME'
option='first_invoice'
try: first_invoice=INI.get(section, option)
except:
print ".INI file missing first_invoice key"
sys.exit(2)
option='last_invoice'
try: last_invoice=INI.get(section, option)
except:
print ".INI file missing last_invoice key"
sys.exit(2)
.
. remainder of program
.
.INI files have following format
[SECTION1]
KEY1=
KEY2=
KEY3=
[SECTION2]
KEY1=
KEY2=
KEY3=
You can have as many sections as you like and keynames
are unique under a section. Normally I do something like:
[INIT]
debug=1
trace=1
logfile='program.log'
[RUNTIME]
first_invoice=12345
last_invoice=99999
This way I can turn on/off debug and trace modes and
have the program pick up RUNTIME parameters for processing.
Hope information helps.
-Larry
"Maxim Khesin" <max at cNvOiSsiPoAnMtech.com> wrote in message
news:U1fob.23140$Gq.6423269 at twister.nyc.rr.com...
> > Will the standard library ConfigParser work? Point your web
> > browser here:
> >
> > http://www.python.org/doc/current/lib/module-ConfigParser.html
> >
> > Regards,
> > Heather
>
> Thanks for the pointer. It is not clear how this supports defaults
> (without understanding windows .ini (I cannot find official docs for
> that) of RFC 822 (I found too much documentation for this :) )
> m
>
More information about the Python-list
mailing list