I
I
Ignat Biroev2021-11-29 11:03:13
PyQt
Ignat Biroev, 2021-11-29 11:03:13

How can the configuration file be compiled so that the application body loads the settings?

I'm new to development so I'm having a hard time creating configurations.
How can I set the configuration code so that the application fills in the appropriate fields and allows you to set settings right inside the application to save them and load them?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2021-11-29
@fox_12

Use Databases to Store Configurations
Either configparser

V
Vindicar, 2021-11-29
@Vindicar

1. You define the configuration file format. Lots of options:

  • a simple ini-file (configparser module) - useful if you need small data, and no more than two levels of hierarchy (i.e. a group-key with a value).
  • xml-file (xml module) - cumbersome, but it supports many nesting levels and there are correctness checkers
  • json file (json module) - much simpler in terms of syntax, the output is a combination of python dictionaries and lists. I would recommend it.
  • It is possible even to fasten a DBMS (like sqlite3), at desire. But this is not always justified.

2. Choose the storage location for the config.
  • In the program folder? There will be one configuration for all users on the PC. Not the fact that you will have write permissions to this folder. But it is easy to copy the program to another computer.
  • In the user's APPDATA folder? Far away, the user is unlikely to climb, but different users on the same computer will have their own settings. It is more difficult to transfer the program to another computer - you will still need to find and copy the settings folder. (It is more difficult not in terms of copy protection, but in terms of ease of distribution.)
  • In the user's My Documents folder? Not the fact that this is a good idea, but the pros are about the same as above.
  • A combination of the previous ones is possible. For example, I do this: if the folder with the executable script contains a file named "portable", I look for the config right there. Otherwise, I'm looking for it in APPDATA.

3. You determine how you will get access to the config inside the program. For example, a separate module that will import all parts of the program. The module contains a singleton class that contains the config values ​​in a form convenient for the rest of the program. This class can also contain default configuration values. The program, at startup, searches for and loads the config into an instance of this class, during operation it refers to this class to get or set parameters, and at the end it saves the class back to the config.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question