Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Generating data from INI configuration files
from configparser import ConfigParser
def generate_data(filename, name):
"""This function read parameters from an ini file and generates data depending on the 'dtype' option."""
config = ConfigParser()
mode = config.get(name, "mode", fallback="discrete")
dtype = config.get(name, "dtype", fallback="string")
if mode == "discrete":
if dtype == "int":
values = map(int, config.get(name, "values").split(","))
elif dtype == "float":
values = map(float, config.get(name, "values").split(","))
values = map(str, config.get(name, "values").split(","))
elif mode == "step":
if dtype == "int":
start = config.getint(name, "start")
stop = config.getint(name, "stop")
step = config.getint(name, "step")
elif dtype == "float":
start = config.getfloat(name, "start")
stop = config.getfloat(name, "stop")
step = config.getfloat(name, "step")
raise ValueError("dtype key of either 'int' or 'float' must exist.")
stop = round(stop + step, 14) # increment stop for inclusive range and round off floating point error
values = frange(start, stop, step)
return values
def frange(start, stop=None, step=None):
"""This provides a floating point range function is both positive and negative directions"""
if stop is None:
stop = start
start = 0
if step is None:
step = 1
i = start
while True:
if step > 0 and i >= stop:
elif step < 0 and i <= stop:
yield i
i = round(i + step, 14) # round off floating point rounding error
mode = discrete
dtype = float
values = 1.04,1.1,1.16
mode = step
dtype = int
start = 10
stop = 100
step = 10
mode = step
dtype = float
start = 0
stop = -0.1
step = -0.02
values = A,B,C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.