Pythonic reading from config files
Posted
by Adam Matan
on Stack Overflow
See other posts from Stack Overflow
or by Adam Matan
Published on 2010-04-11T09:32:35Z
Indexed on
2010/04/11
9:33 UTC
Read the original article
Hit count: 384
Hi,
I have a python class which reads a config file using ConfigParser:
Config file:
[geography]
Xmin=6.6
Xmax=18.6
Ymin=36.6
YMax=47.1
Python code:
class Slicer:
def __init__(self, config_file_name):
config = ConfigParser.ConfigParser()
config.read(config_file_name)
# Rad the lines from the file
self.x_min = config.getfloat('geography', 'xmin')
self.x_max = config.getfloat('geography', 'xmax')
self.y_min = config.getfloat('geography', 'ymin')
self.y_max = config.getfloat('geography', 'ymax')
I feel that the last four lines are repetitive, and should somehow be compressed to one Pythonic line that would create a self.item
variable for each item in the section.
Any ideas?
Adam
© Stack Overflow or respective owner