Skip to content

Instantly share code, notes, and snippets.

@bmoregeo
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoregeo/0982aa49d1e14135a914 to your computer and use it in GitHub Desktop.
Save bmoregeo/0982aa49d1e14135a914 to your computer and use it in GitHub Desktop.
Python Config Files with ConfigObj
# Import the ConfigObj library
from configobj import ConfigObj
# Parse the configuration file using ConfigObj
config = ConfigObj(filename)
# The ConfigObj library parses the configuration file as a
# python dictionary. Super Cool!
# This means you can do things like this -
print config['name']
# and it will return - example
# or do this
print config['server']['value'] * config['server']['threads']
# To get a subsection you just follow the dictionary path.
# data -
print config['features']
# features
print config['features']['polygons']
# Tons of fun
######
# You can specify boolean values in your configuration
# file with: True, 1, true or False, false, 0
print config['bool']
# The parsing engine guesses what you input
# This is good for a lot of things like the polygon features URL
# It will assume the URL is a String element
print config['features']['polygons']
# The parser will assume the list is an array of items
print config['list']
# The parser will assume intetger is an Integer element
print config['integer]
# However it gets tricky, when the assumptions are wrong!
# It assumes declare_list is a string, but we want to read it as an array!
print config['declare_list"]
# You can specify the data type to read. In this case we want a list!
print config.as_list('declare_list')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment