Skip to content

Instantly share code, notes, and snippets.

@twobraids
Created February 19, 2016 23:39
Show Gist options
  • Save twobraids/66efbbebd43358427b41 to your computer and use it in GitHub Desktop.
Save twobraids/66efbbebd43358427b41 to your computer and use it in GitHub Desktop.
an example of getting configuration information into a file
$ python t0.py --help
usage:
t0.py [OPTIONS]...
OPTIONS:
--admin.conf
the pathname of the config file (path/filename)
--admin.dump_conf
a pathname to which to write the current config
(default: )
--admin.expose_secrets
should options marked secret get written out or hidden?
(default: False)
--admin.print_conf
write current config to stdout (json, ini, conf)
--admin.strict
mismatched options generate exceptions rather than just warnings
(default: False)
--help
print this
--planets.mercury.mass
mass
(default: 0)
--planets.mercury.x
x
(default: 100)
--planets.mercury.x_velocity
x_velocity
(default: 0)
--planets.mercury.y
y
(default: 100)
--planets.mercury.y_velocity
y_velocity
(default: 0)
--planets.venus.mass
mass
(default: 0)
--planets.venus.x
x
(default: 200)
--planets.venus.x_velocity
x_velocity
(default: 0)
--planets.venus.y
y
(default: 200)
--planets.venus.y_velocity
y_velocity
(default: 0)
$ python t0.py --planets.venus.mass=83 --planets.mercury.mass=17
17
17
17
New Planet: mercury
mass: 17
x: 100
x_velocity: 0
y: 100
y_velocity: 0
New Planet: venus
mass: 83
x: 200
x_velocity: 0
y: 200
y_velocity: 0
$ python t0.py --planets.venus.mass=45 --planets.mercury.mass=3 --admin.dump_conf=solarsystem.ini
$ cat solarsystem.ini
[planets]
[[mercury]]
# mass
mass=3
# x
#x=100
# x_velocity
#x_velocity=0
# y
#y=100
# y_velocity
#y_velocity=0
[[venus]]
# mass
mass=45
# x
#x=200
# x_velocity
#x_velocity=0
# y
#y=200
# y_velocity
#y_velocity=0
$ python t0.py --admin.conf=solarsystem.ini
3
3
3
New Planet: mercury
mass: 3
x: 100
x_velocity: 0
y: 100
y_velocity: 0
New Planet: venus
mass: 45
x: 200
x_velocity: 0
y: 200
y_velocity: 0
$ python t0.py --admin.conf=solarsystem.ini --planets.mercury.x_velocity=10000 --admin.dump_conf=next_solarsystem.ini
$ python t0.py --admin.conf=next_solarsystem.ini
3
3
3
New Planet: mercury
mass: 3
x: 100
x_velocity: 10000
y: 100
y_velocity: 0
New Planet: venus
mass: 45
x: 200
x_velocity: 0
y: 200
y_velocity: 0
$
#!/usr/bin/env python
# grab the configuration function from the configman module
from configman import configuration
# create structure of nested dicts that define a hierarchy of of
# configuration values that you want available in your program
initial_config = {
"planets": {
'mercury': {
'mass': 0,
'x': 100,
'y': 100,
'x_velocity': 0,
'y_velocity': 0,
},
'venus': {
'mass': 0,
'x': 200,
'y': 200,
'x_velocity': 0,
'y_velocity': 0,
},
}
}
# tell configman to start your program reading any configuration from the
# commandline, configuration file or environment variables.
config = configuration(initial_config)
# config is now a nested structure of constants that you can access in either
# dotted form like this:
print config.planets.mercury.mass
# or a subscripting form like this
print config["planets"]["mercury"]["mass"]
# or even mixed and matched
print config.planets['mercury'].mass
# you could even use it to create a loop:
class Planet(object):
def __init__(self, name, **kwargs):
print "New Planet:", name
for an_attribute, a_value in sorted(kwargs.iteritems()):
print " %s: %s" % (an_attribute, a_value)
setattr(self, an_attribute, a_value)
my_solar_system = set()
for planet_name, some_planet_attributes in config.planets.iteritems():
# take the dict that decscribes the initial configuration of a planet
# and turn them into arguments for the planet constructor
new_planet = Planet(planet_name, **some_planet_attributes)
my_solar_system.add(new_planet)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment