473,513 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

best way to read a configuration file


Hi All,

i am wondering about the best way to read in a configuration file that
goes like:

###########

[users]
source_dir = '/home/karthik/Projects/python'
data_dir = '/home/karthik/Projects/data'
result_dir = '/home/karthik/Projects/Results'
param_file = $result_dir/param_file
res_file = $result_dir/result_file
comment = 'this is a comment'

K = 8
simulate_K = 0

N = 4000
mod_scheme = 'QPSK'

Na = K+2

######################

As of now i use config parser and i get this in a dictionary but

a) but i have users.na and not users.Na (all the fields are in lower
case)

b) all the rhs arguements are string, but i have handled this by
trying " try eval(rhs) ... except ..." block

c) Na = 'K+2' though does not work, would like to have Na = 10, but i
get users.na = 'K+2'

d) result_file, param_file should actually be should be with pathname
extensions filled in.

Have looked in c.l.py none one ofthe suggestions was to use
splitlines, this cant handle blank lines, other was to have config.py
...

Hope to get some solution for this ..

with warm regards

karthik
--

-----------------------------------------------------------------------
Karthikesh Raju, email: ka*****@james.hut.fi
Researcher, http://www.cis.hut.fi/karthik
Helsinki University of Technology, Tel: +358-9-451 5389
Laboratory of Comp. & Info. Sc., Fax: +358-9-451 3277
Department of Computer Sc.,
P.O Box 5400, FIN 02015 HUT,
Espoo, FINLAND
-----------------------------------------------------------------------
Jul 18 '05 #1
5 2378
"Karthikesh Raju" <ka*****@james.hut.fi> wrote in message
news:2d************@itl-pc87.hut.fi...

Hi All,

i am wondering about the best way to read in a configuration file that
goes like:

###########

[users]
source_dir = '/home/karthik/Projects/python'
data_dir = '/home/karthik/Projects/data'
result_dir = '/home/karthik/Projects/Results'
param_file = $result_dir/param_file
res_file = $result_dir/result_file
comment = 'this is a comment'

K = 8
simulate_K = 0

N = 4000
mod_scheme = 'QPSK'

Na = K+2

######################

As of now i use config parser and i get this in a dictionary but

a) but i have users.na and not users.Na (all the fields are in lower
case)

b) all the rhs arguements are string, but i have handled this by
trying " try eval(rhs) ... except ..." block

c) Na = 'K+2' though does not work, would like to have Na = 10, but i
get users.na = 'K+2'

d) result_file, param_file should actually be should be with pathname
extensions filled in.

Have looked in c.l.py none one ofthe suggestions was to use
splitlines, this cant handle blank lines, other was to have config.py
..

Hope to get some solution for this ..

with warm regards

karthik
--

-----------------------------------------------------------------------
Karthikesh Raju, email: ka*****@james.hut.fi
Researcher, http://www.cis.hut.fi/karthik
Helsinki University of Technology, Tel: +358-9-451 5389
Laboratory of Comp. & Info. Sc., Fax: +358-9-451 3277
Department of Computer Sc.,
P.O Box 5400, FIN 02015 HUT,
Espoo, FINLAND
-----------------------------------------------------------------------

Jul 18 '05 #2
"Karthikesh Raju" <ka*****@james.hut.fi> wrote in message
news:2d************@itl-pc87.hut.fi...

Hi All,

i am wondering about the best way to read in a configuration file that
goes like:

###########

[users]
source_dir = '/home/karthik/Projects/python'
data_dir = '/home/karthik/Projects/data'
result_dir = '/home/karthik/Projects/Results'
param_file = $result_dir/param_file
res_file = $result_dir/result_file
comment = 'this is a comment'

K = 8
simulate_K = 0

N = 4000
mod_scheme = 'QPSK'

Na = K+2

######################

As of now i use config parser and i get this in a dictionary but

a) but i have users.na and not users.Na (all the fields are in lower
case)

b) all the rhs arguements are string, but i have handled this by
trying " try eval(rhs) ... except ..." block

c) Na = 'K+2' though does not work, would like to have Na = 10, but i
get users.na = 'K+2'

d) result_file, param_file should actually be should be with pathname
extensions filled in.

Have looked in c.l.py none one ofthe suggestions was to use
splitlines, this cant handle blank lines, other was to have config.py
..

Hope to get some solution for this ..

with warm regards

karthik
--

-----------------------------------------------------------------------
Karthikesh Raju, email: ka*****@james.hut.fi
Researcher, http://www.cis.hut.fi/karthik
Helsinki University of Technology, Tel: +358-9-451 5389
Laboratory of Comp. & Info. Sc., Fax: +358-9-451 3277
Department of Computer Sc.,
P.O Box 5400, FIN 02015 HUT,
Espoo, FINLAND
-----------------------------------------------------------------------


I modified the config parser that is included as an example that ships with
pyparsing, by adding:

iniLines = "\n".join( file("karthik.ini").readlines() )
config = inifile_BNF().parseString(iniLines)
pprint.pprint( config.asList() )
for k in config.users.keys():
print k,":",config.users[k]

This gives the following output:

[['users',
['source_dir ', " '/home/karthik/Projects/python'"],
['data_dir ', " '/home/karthik/Projects/data'"],
['result_dir ', " '/home/karthik/Projects/Results'"],
['param_file ', ' $result_dir/param_file'],
['res_file ', ' $result_dir/result_file'],
['comment ', " 'this is a comment'"],
['K ', ' 8'],
['simulate_K ', ' 0'],
['N ', ' 4000'],
['mod_scheme ', " 'QPSK'"],
['Na ', ' K+2']]]
comment : 'this is a comment'
data_dir : '/home/karthik/Projects/data'
mod_scheme : 'QPSK'
Na : K+2
K : 8
simulate_K : 0
N : 4000
res_file : $result_dir/result_file
result_dir : '/home/karthik/Projects/Results'
source_dir : '/home/karthik/Projects/python'
param_file : $result_dir/param_file

This actually shows the 3 access modes to the results from a pyparsing
parseString() operation:
- as a raw list of tokens (optionally grouped into sublists, giving a parse
tree)
- as an object with attributes (note reference to 'config.users', also
notice that keys are not converted to lower case)
- as a dictionary (access to config.users[k] for each key k)

The 2nd level keys could also be referenced using the form
config.users.data_dir, config.users.comment, etc.

pyparsing also comes with an expression parser and evaluator in its
examples. With some creative merging of the two, I think you could in
fairly short order have a config parser that would handle your Na = K+2 and
res_file = "$result_dir..." config values.

The pyparsing home page is at http://pyparsing.sourceforge.net.

HTH,
-- Paul
Jul 18 '05 #3
Sorry about that last null post, my fingers tripped over the send button
before I started typing my response.

-- Paul
Jul 18 '05 #4
Thankx paul, will have to download pyparsing, will do in a short will
and try your trick, hope it works well :-)

With warm regards

karthik
--

-----------------------------------------------------------------------
Karthikesh Raju, email: ka*****@james.hut.fi
Researcher, http://www.cis.hut.fi/karthik
Helsinki University of Technology, Tel: +358-9-451 5389
Laboratory of Comp. & Info. Sc., Fax: +358-9-451 3277
Department of Computer Sc.,
P.O Box 5400, FIN 02015 HUT,
Espoo, FINLAND
-----------------------------------------------------------------------
Jul 18 '05 #5
I kept this sample from an ealier thread or website. I don't have the
link nor can I give credit to the author. If you have some lattitude
on the exact format of the config file, this may do the trick for you
though:

Python App Macro Lang
class App:
def __init__(self, name):
self._name = name
def name(self):
return self._name

i=123

macrosource = """
print app.name()
i=1
if i==2:
server="internal.blah.com"
else:
server="external.blah.com"

"""

code = compile(macrosource, '<string>', "exec")
anApp = App('Global App Object')
context = {}

# populate context with fun things
context["app"] = anApp
exec code in context

print "The server is:" + context["server"]
if i != 123:
raise "local variable 'i' changed"

output = """
c:\cygwin\bin\sh -c "python PyAsMacroLang.py"
Global App Object
The server is:external.blah.com
"""
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2104
by: Herve MAILLARD | last post by:
Hi, I have to write a software doing the following : - Load a file (containing data) Data will be display in a Treeview and are typed as following Equipement Bloc Tag It could have for each...
7
6565
by: | last post by:
In the beginning we had Ini files. Later we had registery files. Now have xml files and our read-only myapp.config file. My question now, is what is the best way to store and load user and...
7
11035
by: Jimbo | last post by:
What's the best format to save a configuration file? I'm currently using an INI extension and I write it like a normal ascii file. Is this the best way? I've heard of using XML to create a config...
7
5971
by: MrNobody | last post by:
I was a Java developer so I'm used to using property files as a means to keep configuration settings for my apps. I'm wondering what options are there with ..NET? Some settings I want to include...
2
1989
by: G. Dean Blake | last post by:
We currently distribute a web application to serveral servers for a customer. We have been putting the connection string in our web.config file under <appSettings> <add...
11
2394
by: Tom | last post by:
I am planning on adding a Preferences form to my application and using the Property Grid to display the preferences to the user. What do you think would be the best way to save these preferences...
10
2814
by: Brett | last post by:
If I have many hard coded values such as file paths, file names, timeouts, etc, where is the best place to define them? Meaning, in the case something needs changing for example, rather than...
5
1384
by: Ronald S. Cook | last post by:
We have a .NET Win app that runs at 12 different cattle feeding lots. Each lot runs an isolated instance of our app using its own SQL Server instance. For overall settings specific to a feedlot,...
5
1862
by: Allan Ebdrup | last post by:
I have a webservice in ASP.Net 2.0, I need to store and load a xml configuration file in relation to one of the web methods in my webservice. What's the best place to store and the best way to load...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.