rodee
rodee

Reputation: 3161

How to Update config file programatically in Python

I am modifying a existing Python code, I see config.py with content like below

person = {
       'peter': {
            'age' : '60',
             'sex' : 'm',
             'phone' : '999 999 999'
        },
        'sara': {
            'age' : '68',
             'sex' : 'f',
             'phone' : '111 111 111'
        }
}

The Python program has:

import config
# config item is accessed as below
print config.person['peter']['age']

I have 2 questions, firstly, What is this format in config.py called? can I call it as Python built-in data structure? because this is not JSON.

Secondly, how can I update the config.py entry (or add new person), for example, how to change config.person['sara']['phone'] to 888 888 888? or add a new person zac ?

I am using python3.

Upvotes: 0

Views: 4651

Answers (3)

abarnert
abarnert

Reputation: 365787

The format is just plain executable Python source code.

This format is generally not used when you need to programmatically change the contents, but a human being with some basic Python knowledge can edit it in a simple text editor.

For example, to change Sara's phone number in your text editor, you just go to this line:

         'phone' : '111 111 111'

… and change it to this:

         'phone' : '888 888 888'

Similarly, to add another person, you can copy and paste one of the existing people's entries and edit it.


But if you wanted to have your script edit this file, it would need to parse the Python source (e.g., using the ast module), map the assignments to a set of key-value pairs, modify the value, turn the result back into a set of assignments, then turn that back into source code. Or it could do something a little simpler and hackier with the same effect. But the point is, there's no clear and easy way to edit this file programmatically. I don't want to provide code that does this parsing and generating, because it's really not something you should be doing if you don't know how, and haven't thought through the safety implications, etc.

If you want a config file you can edit programmatically, replace it with something like JSON. That's just as easy for a human to edit, but it has the added benefit of also being easy for your program to edit, and being inherently safe from mistaken or malicious bad data.

If you or your users already have deployments using the config.py format, you can write migration code that falls back to the "legacy" config file if the JSON file can't be found, something like this:

try:
    with open('config.json') as f:
        config = json.load(f)
except FileNotFoundError:
    try:
        import config
    except Exception as e:
        raise SomeMoreAopropriateError(…)
    with open('config.json', 'w') as f:
        json.dump(config, f)

(If the old code used, e.g., execfile instead of import, do the same thing here, of course.)

Upvotes: 2

ivan_pozdeev
ivan_pozdeev

Reputation: 36026

It's just a regular dict.

Most probably, this file is imported like a module, after which you can access config.person as a Python object.

Now, this config format is not supposed to be programmatically overwritten, and for a good reason: a .py is code rather than data and can have arbitrary logic in it. If you wish to do it anyway, you'll need to jump through hoops. If you only need structured data, without any logic, you'll be better off with JSON (see the json module).


That said, here's the plan how it will look like:

  1. Choose what the resulting file contents will look like. Options include:
    • Overwrite the entire file with repr() of the dict
    • Overwrite the entire file with pprint.pprint() of the dict
    • Edit the file line by line by hand
  2. At an appropriate moment, write the chosen representation into the file. Its path can be found as config.__file__.

Upvotes: 0

Roushan
Roushan

Reputation: 4420

You can use update method for adding new entry in dictionary :

In [46]: person.update({'another':{"age":'45',"sex":"m","phone":"811 345 678"}})

In [47]: person
Out[47]: 
{'another': {'age': '45', 'phone': '811 345 678', 'sex': 'm'},
 'peter': {'age': '60', 'phone': '999 999 999', 'sex': 'm'},
 'sara': {'age': '68', 'phone': '111 111 111', 'sex': 'f'}}

for updating the existing the value you can use the name as key and another key for age or male or sex

In [48]: person['another']['age']='50' 

In [49]: person
Out[49]: 
{'another': {'age': '50', 'phone': '811 345 678', 'sex': 'm'},
 'peter': {'age': '60', 'phone': '999 999 999', 'sex': 'm'},
 'sara': {'age': '68', 'phone': '111 111 111', 'sex': 'f'}}

Upvotes: 0

Related Questions