Most Pythonic way to store (small) configuration

Rustom Mody rustompmody at gmail.com
Wed Aug 5 10:25:58 EDT 2015


On Wednesday, August 5, 2015 at 7:38:46 PM UTC+5:30, Steven D'Aprano wrote:
> On Wed, 5 Aug 2015 11:46 pm, Rustom Mody wrote:
> 
> > On Sunday, August 2, 2015 at 3:44:51 PM UTC+5:30, Cecil Westerhof wrote:
> >> There are a lot of ways to store configuration information:
> >> - conf file
> >> - xml file
> >> - database
> >> - json file
> >> - and possible a lot of other ways
> > 
> > One that I dont think has been mentioned:
> > ast.literal_eval
> 
> 
> Probably because it doesn't work :-)

In the way you describe... yes
Works alright if you give it *literals*

>>> from ast import literal_eval
>>> literal_eval('{"x":"hello", "y":2, "z":3.142}')
{'z': 3.142, 'x': 'hello', 'y': 2}

which is identical the the json.loads behavior
>>> loads('{"x":"hello", "y":2, "z":3.142}')
{'z': 3.142, 'x': 'hello', 'y': 2}

of course the natural method of use would of course get the string from
the open-ing of a config file -- something along the lines

configuration = literal_eval(open("~/.fooconfig"))



More information about the Python-list mailing list