Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Using ConfigParser vs json file [closed]

I am currently using the ConfigParser module to read and parse the configuration for a python program. I understand that using ConfigParser streamlines the parsing and reading of configuration from file, however I am just curious as to what would be the tradeoffs if I simply use a json format for reading/writing config files. Wouldn't that be equally easy to parse etc, same as ConfigParser ?

like image 783
bawejakunal Avatar asked Mar 28 '16 02:03

bawejakunal


People also ask

Should I use JSON for a config file?

With so many better options for configuration languages, there's no good reason to use JSON. If you are creating a new application, framework, or library that requires configuration choose something other than JSON.

What is Configparser used for in Python?

The configparser module from Python's standard library defines functionality for reading and writing configuration files as used by Microsoft Windows OS. Such files usually have . INI extension. The INI file consists of sections, each led by a [section] header.

Is Configparser built in Python?

configparser comes from Python 3 and as such it works well with Unicode. The library is generally cleaned up in terms of internal data storage and reading/writing files.


1 Answers

JSON would be easy enough for your program to parse, but it would also burden the user with the responsibility of getting braces and quotes just right, and it would add unnecessary clutter to your configuration files. If that extra complexity is okay with you, or if you really need the kind of deep nesting that is slightly easier to parse in JSON than in flat config files, then by all means, use JSON. Some people even take this a step further and put their configuration in Python files.

Personally, I feel that configuration files that users might need to read or edit should be as simple as possible, so I use (a subset of) the configparser syntax. If I need hierarchy, I simply represent it with dots:

parent.child1 = foo
parent.child2 = bar

When I want to avoid requiring [sections] in my config files, I can either trick configparser into not needing them, or use a TOML parser instead.

like image 72
ʇsәɹoɈ Avatar answered Oct 09 '22 00:10

ʇsәɹoɈ