29

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?

Lucas
  • 3,376
  • 6
  • 31
  • 46

3 Answers3

46

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 correct:

import ConfigParser
import StringIO

s_config = """
[example]
is_real: False
"""
buf = StringIO.StringIO(s_config)
config = ConfigParser.ConfigParser()
config.readfp(buf)
print config.getboolean('example', 'is_real')
Noel Evans
  • 8,113
  • 8
  • 48
  • 58
  • well where to use the buffer? ConfigParser.Read() excepts a file name. – Lucas Feb 13 '14 at 22:05
  • 1
    [`cStringIO` objects](http://docs.python.org/2/library/stringio.html#module-cStringIO) are not [`buffer`s](http://docs.python.org/2/library/functions.html#buffer). _Strings_ are buffers, but buffers cannot be used where `file`-like objects are required; `cStringIO` _wraps_ a `buffer` in order to make it behave like a `file`. Besides, your example does not demonstrate how a `cStringIO` behaves like a file; `getvalue` is a method specific to `cStringIO` instances, but files don't have it. – lanzz Feb 13 '14 at 22:11
  • @Lucas I've posted a complete example that takes a StringIO buffer and gets a sample value – Noel Evans Feb 13 '14 at 22:15
  • 1
    Note that `config.readfp` is deprecated in favor of `config.read_file` [as of Python 3.2](https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.read_file) – mindthief Sep 18 '19 at 01:13
32

The question was tagged as python-2.7 but just for the sake of completeness: Since 3.2 you can use the ConfigParser function read_string() so you don't need the StringIO method anymore.

import configparser

s_config = """
[example]
is_real: False
"""
config = configparser.ConfigParser()
config.read_string(s_config)
print(config.getboolean('example', 'is_real'))
Sebastian
  • 5,471
  • 5
  • 35
  • 53
  • 1 more reason I hate working with Python 2.7 still (thanks RHEL 7) – Nick T Jun 21 '17 at 21:18
  • To be fair to RHEL7, it's perfectly possibly to install Python 3.x. https://developers.redhat.com/blog/2018/08/13/install-python3-rhel/ – RCross Jul 30 '19 at 14:52
0

Python has read_string and read_dict since version 3.2. It does not support reading from lists.

The example shows reading from a dictionary. Keys are section names, values are dictionaries with keys and values that should be present in the section.

#!/usr/bin/env python3

import configparser

cfg_data = {
    'mysql': {'host': 'localhost', 'user': 'user7',
              'passwd': 's$cret', 'db': 'ydb'}
}

config = configparser.ConfigParser()
config.read_dict(cfg_data)

host = config['mysql']['host']
user = config['mysql']['user']
passwd = config['mysql']['passwd']
db = config['mysql']['db']

print(f'Host: {host}')
print(f'User: {user}')
print(f'Password: {passwd}')
print(f'Database: {db}')
Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77