Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python config parser cache to reduce I/O on an embedded system

In my project we use a config file that's parsed with the configparser module. Is there any way to cache the entire config file, and then use configparser methods to read it from memory? I'm hoping to not have to just store the config variables in a dictionary and look them up that way since using the configparser is really tightly coupled with a lot of our app and I'm hoping I can just change where the config is located to point to memory instead of a file name. Is this possible?

like image 532
Falmarri Avatar asked Oct 11 '25 21:10

Falmarri


1 Answers

Yep. If you use the StringIO module, you can create a file-like object that is in memory.

Then you can use the method (ConfigParser inherits from RawConfigParser):

RawConfigParser.readfp(fp[, filename]) Read and parse configuration data from the file or file-like object in fp (only the readline() method is used). If filename is omitted and fp has a name attribute, that is used for filename; the default is .

like image 68
Bradley Kreider Avatar answered Oct 14 '25 13:10

Bradley Kreider