0

I am trying to writing a program to read a configuration file but while testing it am having this error:

self.connection_attempts = self.config_file.get('CONNECTION_ATTEMPTS', 'TIME')
AttributeError: 'list' object has no attribute 'get'

I ma pretty sure it is something I don't get, but it is few hours I am trying to understand where the problem is. My __init__ method looks like this:

import simpleconfigparser

class ReportGenerator:
    def __init__(self):
        self.config_parser = simpleconfigparser.configparser()
        self.config_file = config_parser.read('config.ini')
        self.connection_attempts = config_file.get('CONNECTION_ATTEMPTS', 'TIME')
        self.connection_timeout = config_file.get('CONNECTION_TIMEOUT', 'TIMEOUT')
        self.report_destination_path = config_file.get('REPORT', 'REPORT_PATH')

This code uses the SimpleConfigParser package.

7
  • 1
    config_file is a list object, hence your error Commented May 24, 2018 at 12:19
  • Please refer to this post : stackoverflow.com/questions/5125619/… Commented May 24, 2018 at 12:19
  • I noticed, but in another program I wrote i used axactly the same code, only outside the init method, and it works Commented May 24, 2018 at 12:20
  • config_parser.read(...) creates a list. Lists in python cannot be inquired by .get. Maybe you can convert it into a dictionary Commented May 24, 2018 at 12:20
  • If config_file is a dictionary object this works with get(). The config_file might be a list object Commented May 24, 2018 at 12:21

1 Answer 1

2

You want config_parser.get() not config_file.get(). config_parser.read() simply returns the list of config files successfully read after populating the config object. (Usually it is called config or cfg, not config_parser).

This list (config_file) serves no purpose in your code and you might as well not capture it at all.

from simpleconfigparser import simpleconfigparser

TIME = 5
TIMEOUT = 10
REPORT_PATH = '/tmp/'

class ReportGenerator:
    def __init__(self):
        self.config = simpleconfigparser()
        config.read('config.ini')

        self.connection_attempts = config.get('CONNECTION_ATTEMPTS', TIME)
        self.connection_timeout = config.get('CONNECTION_TIMEOUT', TIMEOUT)
        self.report_destination_path = config.get('REPORT', REPORT_PATH)

My guess would also be, that you use the default value in .get() the wrong way, but i cannot be certain with the information you have given.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.