best way to read a configuration file

Kevin Dahlhausen kdahlhaus at yahoo.com
Wed Mar 3 12:52:29 EST 2004


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
"""



More information about the Python-list mailing list