2
\$\begingroup\$

I'm trying to write a function that tries to read a config and if fails, creates a new one and writes to the file "settings.ini". This feels really chunky and not particularly concise. Is this the supposed way of reading a configparser file?

from configparser import ConfigParser
def getSettings(self):
    try:
        with open('settings.ini') as f:
            return config.read_file(f)

    except:
        config['DEFAULT'] = {'Host': '127.0.0.1',
                             'Port': '5822'}
        return config['DEFAULT']
\$\endgroup\$
8
  • 1
    \$\begingroup\$ I don't see anything wrong with the question \$\endgroup\$
    – Reinderien
    Jun 26, 2021 at 19:49
  • 4
    \$\begingroup\$ Other than - you need to include your definition of config \$\endgroup\$
    – Reinderien
    Jun 26, 2021 at 19:50
  • 2
    \$\begingroup\$ Welcome to Code Review! Please edit your question so that the title describes the purpose of the code, rather than its mechanism. We really need to understand the motivational context to give good reviews. Thanks! \$\endgroup\$ Jun 27, 2021 at 8:47
  • 3
    \$\begingroup\$ If the code is working then the question is on topic as long as the definition of config is added and the title is modified as @TobySpeight indicated. \$\endgroup\$
    – pacmaninbw
    Jun 27, 2021 at 12:01
  • 1
    \$\begingroup\$ Now you have removed even more code, while there was already not enough for a review. The definition of config is where you define the configuration. What does the configuration look like? Just copy the configuration file into the question, after stripping it from sensitive data like passwords and API keys, and please provide a usage example. How are you actually using this code in your application? If there's no application yet, it's too early in the process for a review. \$\endgroup\$
    – Mast
    Jul 1, 2021 at 11:33

0

Browse other questions tagged or ask your own question.