best approach for an application config file
best approach for an application config file
Hello,
I need a config file for the application running on my pyboard.
I need to read/modify a specific attribute on the file and possible create new file if it is missing.
My initial approach would be using xml and I'm wondering if anybody could suggest a lib to use. I found the following xmltok but it seems to be just a reader, not a writer.
https://pypi.python.org/pypi/micropython-xmltok
Any suggestion would be more than welcome.
I need a config file for the application running on my pyboard.
I need to read/modify a specific attribute on the file and possible create new file if it is missing.
My initial approach would be using xml and I'm wondering if anybody could suggest a lib to use. I found the following xmltok but it seems to be just a reader, not a writer.
https://pypi.python.org/pypi/micropython-xmltok
Any suggestion would be more than welcome.
Re: best approach for an application config file
I've always thought that xml was too wordy, so I personally wouldn't use it unless I had to for compatability.
ujson is builtin to MicroPython
http://docs.micropython.org/en/latest/l ... ujson.html
That works well for simple types (dicts, lists, string, numbers).
I've also used python source code as a configuration format (although typically for read-only config)
ujson is builtin to MicroPython
http://docs.micropython.org/en/latest/l ... ujson.html
That works well for simple types (dicts, lists, string, numbers).
I've also used python source code as a configuration format (although typically for read-only config)
Re: best approach for an application config file
I'm with Dave on the wordy XML thing. I'd certainly give JSON a try but I'd favor YAML for super clean syntax.
/Daryl
/Daryl
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: best approach for an application config file
My preferred option is pickle (in the micropython library). Unlike (u)json all native Python data types are supported. The principal drawback is security: if the pickled file can be modified by ill-intentioned people they can force the execution of arbitrary Python code. If that's a problem I'd use json.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: best approach for an application config file
thanks to everybody. I thinks I will go with ujson.
Following some examples that I found on line for future reference:
Write data to a file:
Read data from a file:
Some more info about nested dictionaries:
Following some examples that I found on line for future reference:
Write data to a file:
Code: Select all
import json
config = {'key1': 'value1', 'key2': 'value2'}
f = open('config.json', 'w')
f.write(ujson.dumps(config))
f.close()
Code: Select all
import json
f = open('config.json', 'r')
c = ujson.loads(f.readall())
#edit the data
config['key3'] = 'value3'
#write it back to the file
f = open('config.json', 'w')
f.write(ujson.dumps(config))
f.close()
Code: Select all
>>> d = {}
>>> d['dict1']['innerkey1'] = 'value1'
>>> d['dict2']['innerkey2'] = 'value2'
{'dict1': {'innerkey1': 'value1'},'dict2': {'innerkey2': 'value2'}}
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: best approach for an application config file
@Sokrates Your nested dictionary example won't work as posted. Try:
Code: Select all
>>> d = {}
>>> d['dict1'] ={}
>>> d['dict2'] ={}
>>> d['dict1']['innerkey1'] = 'value1'
>>> d['dict2']['innerkey2'] = 'value2'
>>> d
{'dict2': {'innerkey2': 'value2'}, 'dict1': {'innerkey1': 'value1'}}
>>>
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: best approach for an application config file
defaultdict to the rescue!pythoncoder wrote:@Sokrates Your nested dictionary example won't work as posted.
https://www.accelebrate.com/blog/using- ... ct-python/
Re: best approach for an application config file
Yes, you are right. Thanks for pointing it out.pythoncoder wrote:@Sokrates Your nested dictionary example won't work as posted. Try:Code: Select all
>>> d = {} >>> d['dict1'] ={} >>> d['dict2'] ={} >>> d['dict1']['innerkey1'] = 'value1' >>> d['dict2']['innerkey2'] = 'value2' >>> d {'dict2': {'innerkey2': 'value2'}, 'dict1': {'innerkey1': 'value1'}} >>>
Re: best approach for an application config file
[quote="pythoncoder"]My preferred option is pickle (in the micropython library). Unlike (u)json all native Python data types are supported. The principal drawback is security: if the pickled file can be modified by ill-intentioned people they can force the execution of arbitrary Python code. If that's a problem I'd use json.[/quote]
Hi, sorry for reviving this old thread but I looked everywhere and couldn't find an answer.
You mention that you use pickle and it's part of the micropython standard library. However, I could only find a dummy implementation of pickle (at least for the ESP8266 version which is what I'm using). See https://github.com/micropython/micropyt ... ter/pickle
Any ideas on if and how I could use pickle? I'm currently using json but it would make my life so much easier to have pickle...
Many thanks,
Hi, sorry for reviving this old thread but I looked everywhere and couldn't find an answer.
You mention that you use pickle and it's part of the micropython standard library. However, I could only find a dummy implementation of pickle (at least for the ESP8266 version which is what I'm using). See https://github.com/micropython/micropyt ... ter/pickle
Any ideas on if and how I could use pickle? I'm currently using json but it would make my life so much easier to have pickle...
Many thanks,
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: best approach for an application config file
I'm not sure what you mean by a "dummy implementation". The version in the link provided works. Note that the pickle files aren't interchangeable with those created by CPython. There is also this issue https://github.com/micropython/micropython/issues/2280. Otherwise I'm not aware of any problem with the version in the library.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.