configparser

Getting a list from a config file with ConfigParser

♀尐吖头ヾ 提交于 2019-12-05 18:52:08
问题 I have something like this in my config file (a config option that contains a list of strings): [filters] filtersToCheck = ['foo', '192.168.1.2', 'barbaz'] Is there a more elegant (built-in) way to get a list from filtersToCheck instead of removing the brackets, single-quotes, spaces and then using split() to do that? Maybe a different module? (Using python3.) 回答1: You cannot use the python object like a list in the value for the config file. But you can ofcourse have them as comma separated

Mocking Method Calls In Python

点点圈 提交于 2019-12-05 08:17:04
I have been searching stack exchange and around the web for how to do this, but I cannot understand how to mock behaviors for methods. I am trying to mock openpyxl behaviors and behaviors for my custom class. Here is my attempt: import unittest from unittest.mock import MagicMock import openpyxl from MyPythonFile import MyClass class TestMyClass(unittest.TestCase): def test_myclass(self): myclass = MyClass() wb = openpyxl.workbook() ws = openpyxl.worksheet() wbPath = 'wbPath' openpyxl.load_workbook(wbPath, data_only = True) = MagicMock(return_value=wb) When I try the final line I get the error

ConfigObj/ConfigParser vs. using YAML for Python settings file

妖精的绣舞 提交于 2019-12-04 15:37:41
问题 Which is better for creating a settings file for Python programs, the built-in module (ConfigParser) or the independent project (ConfigObj), or using the YAML data serialization format? I have heard that ConfigObj is easier to use than ConfigParser, even though it is not a built-in library. I have also read that PyYAML is easy to use, though YAML takes a bit of time to use. Ease of implementation aside, which is the best option for creating a settings/configuration file? 回答1: Using ConfigObj

How to read config(.ini) file in python which will work on 2.7 and 3.x python

空扰寡人 提交于 2019-12-04 15:21:32
Should I use ConfigParser which is compatible with python 2.7 and 3.x or do you suggest any other module in python which is compatible with both versions of python for reading config file? You can make use of configparser backport, so it will work on both Python version. pip install configparser Contrary to the other answers here, you don't need to install any extra packages to write INI-parsing code that is compatible with Python 2 and 3. Python 3.0's configparser is just a renamed version of Python 2's ConfigParser . There have been some extra features added in Python 3.2 and 3.5 (see the

Export with alphabetical sort in Python ConfigParser

微笑、不失礼 提交于 2019-12-04 05:22:29
Is there any solution to force the RawConfigParser.write() method to export the config file with an alphabetical sort? Even if the original/loaded config file is sorted, the module mixes the section and the options into the sections arbitrarily, and is really annoying to edit manually a huge unsorted config file. PD: I'm using python 2.6 Three solutions: Pass in a dict type (second argument to the constructor) which returns the keys in your preferred sort order. Extend the class and overload write() (just copy this method from the original source and modify it). Copy the file ConfigParser.py

Getting a list from a config file with ConfigParser

試著忘記壹切 提交于 2019-12-04 03:48:09
I have something like this in my config file (a config option that contains a list of strings): [filters] filtersToCheck = ['foo', '192.168.1.2', 'barbaz'] Is there a more elegant (built-in) way to get a list from filtersToCheck instead of removing the brackets, single-quotes, spaces and then using split() to do that? Maybe a different module? (Using python3.) You cannot use the python object like a list in the value for the config file. But you can ofcourse have them as comma separated values and once you get it do a split [filters] filtersToCheck = foo,192.168.1.2,barbaz and do

Python ConfigParser: Checking for option existence

╄→尐↘猪︶ㄣ 提交于 2019-12-03 22:08:10
I'm using Python's ConfigParser to create a configuration file. I want to check if a section has a particular option defined and, if it does, get the value. If the option isn't defined, I just want to continue without any special behavior. There seem to be two ways of doing this. if config.has_option('Options', 'myoption'): OPTION = config.get('Options', 'myoption') Or: try: OPTION = config.get('Options', 'myoption') except ConfigParser.NoOptionError: pass Is one method preferred over the other? The if involves less lines, but I've occasionally read that try / except is considered more

Closing file opened by ConfigParser

故事扮演 提交于 2019-12-03 19:28:41
问题 I have the following: config = ConfigParser() config.read('connections.cfg') sections = config.sections() How can I close the file opened with config.read ? In my case, as new sections/data are added to the config.cfg file, I update my wxtree widget. However, it only updates once, and I suspect it's because config.read leaves the file open. And while we are at it, what is the main difference between ConfigParser and RawConfigParser ? 回答1: Use readfp instead of read: with open('connections.cfg

Iterate over sections in a config file

守給你的承諾、 提交于 2019-12-03 16:16:24
问题 I recently got introduced to the library configparser. I would like to be able to check if each section has at least one Boolean value set to 1. For example: [Horizontal_Random_Readout_Size] Small_Readout = 0 Medium_Readout = 0 Large_Readout = 0 The above would cause an error. [Vertical_Random_Readout_Size] Small_Readout = 0 Medium_Readout = 0 Large_Readout = 1 The above would pass. Below is some pseudo code of what I had in mind: exit_test = False for sections in config_file: section_check =

Python 2.4.3: ConfigParser.NoSectionError: No section: 'formatters'

旧时模样 提交于 2019-12-03 16:07:12
问题 Trying to use a logging configuration file to implement TimedRotatinigFileHandler . Just won't take the config file for some reason. Any suggestions appreciated. x.py: import logging import logging.config import logging.handlers logging.config.fileConfig("x.ini") MyLog = logging.getLogger('x') MyLog.debug('Starting') x.ini: [loggers] keys=root [logger_root] level=NOTSET handlers=trfhand [handlers] keys=trfhand [handler_trfhand] class=handlers.TimedRotatingFileHandler when=M interval=1