configparser

How to use variables already defined in ConfigParser

送分小仙女□ 提交于 2019-11-30 01:28:37
问题 I'm using ConfigParser in Python config.ini is [general] name: my_name base_dir: /home/myhome/exp exe_dir: ${base_dir}/bin Here I want exp_dir becomes /home/myhome/exp/bin not ${base_dir}/bin . It means ${base_dir} would be substituted to /home/myhome/exp automatically . 回答1: You can use ConfigParser interpolation On top of the core functionality, SafeConfigParser supports interpolation. This means values can contain format strings which refer to other values in the same section, or values in

Where to put a configuration file in Python?

a 夏天 提交于 2019-11-29 18:56:55
In development mode, I have the following directory tree : | my_project/ | setup.py | my_project/ | __init__.py | main.py | conf/ | myproject.conf I use ConfigParser to parse the myproject.conf file. In my code, it's easy to load the file with a good path : my_project/conf/myproject.conf The problem is : When I install my project using the setup.py, the configuration file are situated (thanks to setup.py) in the /etc/my_project/myproject.conf and my application in the /usr/lib/python<version>/site-packages/my_project/ . How can I refer my my_project/conf/myproject.conf file in my project in

Writing comments to files with ConfigParser

♀尐吖头ヾ 提交于 2019-11-29 10:41:34
问题 How can one write comments to a given file within sections? If I have: import ConfigParser with open('./config.ini', 'w') as f: conf = ConfigParser.ConfigParser() conf.set('DEFAULT', 'test', 1) conf.write(f) I will get the file: [DEFAULT] test = 1 But how can I get a file with comments inside [DEFAULT] section, like: [DEFAULT] ; test comment test = 1 I know I can write codes to files by doing: import ConfigParser with open('./config.ini', 'w') as f: conf = ConfigParser.ConfigParser() conf.set

Where to put a configuration file in Python?

怎甘沉沦 提交于 2019-11-28 14:09:37
问题 In development mode, I have the following directory tree : | my_project/ | setup.py | my_project/ | __init__.py | main.py | conf/ | myproject.conf I use ConfigParser to parse the myproject.conf file. In my code, it's easy to load the file with a good path : my_project/conf/myproject.conf The problem is : When I install my project using the setup.py, the configuration file are situated (thanks to setup.py) in the /etc/my_project/myproject.conf and my application in the /usr/lib/python<version>

Creating a class with all the elements specified in a file using ConfigParser

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 08:50:57
I have created a .ini-like file with all the values which I need later in my program, see below: [debugging] checkForAbort = 10 ... [official] checkForAbort = 30 ... I would like to read all these items into a single class and make it accessible from other parts of my python project. What I have so far is the code below: from ConfigParser import SafeConfigParser import glob class ConfigurationParameters def __init__(self): self.checkForAbortDuringIdleTime = None parser = SafeConfigParser() # making a list here in case we have multiple files in the future! candidatesConfiguration = ['my.cfg']

How to ConfigParse a file keeping multiple values for identical keys?

我的梦境 提交于 2019-11-28 07:00:24
I need to be able to use the ConfigParser to read multiple values for the same key. Example config file: [test] foo = value1 foo = value2 xxx = yyy With the 'standard' use of ConfigParser there will be one key foo with the value value2 . But I need the parser to read in both values. Following an entry on duplicate key I have created the following example code: from collections import OrderedDict from ConfigParser import RawConfigParser class OrderedMultisetDict(OrderedDict): def __setitem__(self, key, value): try: item = self.__getitem__(key) except KeyError: super(OrderedMultisetDict, self)._

Preserve case in ConfigParser?

有些话、适合烂在心里 提交于 2019-11-28 05:36:31
I have tried to use Python's ConfigParser module to save settings. For my app it's important that I preserve the case of each name in my sections. The docs mention that passing str() to ConfigParser.optionxform() would accomplish this, but it doesn't work for me. The names are all lowercase. Am I missing something? <~/.myrc contents> [rules] Monkey = foo Ferret = baz Python pseudocode of what I get: import ConfigParser,os def get_config(): config = ConfigParser.ConfigParser() config.optionxform(str()) try: config.read(os.path.expanduser('~/.myrc')) return config except Exception, e: log.error

How to read config from string or list?

风格不统一 提交于 2019-11-27 22:05:34
问题 Is it possible to read the configuration for ConfigParser from a string or list? Without any kind of temporary file on a filesystem OR Is there any similar solution for this? 回答1: You could use a buffer which behaves like a file: Python 3 solution import configparser import io s_config = """ [example] is_real: False """ buf = io.StringIO(s_config) config = configparser.ConfigParser() config.read_file(buf) print(config.getboolean('example', 'is_real')) In Python 2.7 , this implementation was

Creating a class with all the elements specified in a file using ConfigParser

可紊 提交于 2019-11-27 02:28:16
问题 I have created a .ini-like file with all the values which I need later in my program, see below: [debugging] checkForAbort = 10 ... [official] checkForAbort = 30 ... I would like to read all these items into a single class and make it accessible from other parts of my python project. What I have so far is the code below: from ConfigParser import SafeConfigParser import glob class ConfigurationParameters def __init__(self): self.checkForAbortDuringIdleTime = None parser = SafeConfigParser() #

Preserve case in ConfigParser?

吃可爱长大的小学妹 提交于 2019-11-27 00:59:11
问题 I have tried to use Python's ConfigParser module to save settings. For my app it's important that I preserve the case of each name in my sections. The docs mention that passing str() to ConfigParser.optionxform() would accomplish this, but it doesn't work for me. The names are all lowercase. Am I missing something? <~/.myrc contents> [rules] Monkey = foo Ferret = baz Python pseudocode of what I get: import ConfigParser,os def get_config(): config = ConfigParser.ConfigParser() config