pyhocon icon indicating copy to clipboard operation
pyhocon copied to clipboard

dump a python dict to a hocon file

Open lmzhangucsd opened this issue 6 years ago • 3 comments

I see there is option for dumping json, yaml and properties format, but is there a way to dump to hocon itself?

lmzhangucsd avatar Feb 13 '19 21:02 lmzhangucsd

import pyhocon
config = pyhocon.ConfigTree()
d = {'foo':1, 'bar':2, 'baz':3}
config['example'] = d
with open("example.conf","w") as outfile:
    outfile.write(pyhocon.HOCONConverter.to_hocon(config))

dsynkov avatar Aug 12 '19 00:08 dsynkov

@dsynkov , In your example the result is: example {'foo': 1, 'bar': 2, 'baz': 3} But isn't this output is invalid because hocon is based off json. The ouput should have have single quotes wrapped around keys.

pitchblack408 avatar Dec 19 '19 22:12 pitchblack408

@liamz39 , I was looking for a simple library to write the conf files.

https://stackoverflow.com/questions/59417529/how-can-i-generate-hocon-conf-file-dynamically-using-pyhocon-in-python-3/59430263#59430263

From what I have read, it seems like pyhocon only has two ways to write hocon conf files. One is JSON and the other is an off looking style using that HOCONConverter.to_hocon method. I say off because when it writes, it looks different than any of the styles documented by lightbend. I guess I will stick to the json. If you have figured out more, please respond.

pitchblack408 avatar Dec 20 '19 20:12 pitchblack408