Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration file with list of key-value pairs in python

I have a python script that analyzes a set of error messages and checks for each message if it matches a certain pattern (regular expression) in order to group these messages. For example "file x does not exist" and "file y does not exist" would match "file .* does not exist" and be accounted as two occurrences of "file not found" category.

As the number of patterns and categories is growing, I'd like to put these couples "regular expression/display string" in a configuration file, basically a dictionary serialization of some sort.

I would like this file to be editable by hand, so I'm discarding any form of binary serialization, and also I'd rather not resort to xml serialization to avoid problems with characters to escape (& <> and so on...).

Do you have any idea of what could be a good way of accomplishing this?

Update: thanks to Daren Thomas and Federico Ramponi, but I cannot have an external python file with possibly arbitrary code.

like image 776
Paolo Tedesco Avatar asked Sep 09 '25 15:09

Paolo Tedesco


1 Answers

I sometimes just write a python module (i.e. file) called config.py or something with following contents:

config = {
    'name': 'hello',
    'see?': 'world'
}

this can then be 'read' like so:

from config import config
config['name']
config['see?']

easy.

like image 83
Daren Thomas Avatar answered Sep 12 '25 03:09

Daren Thomas