python 配置解析器缓存以减少嵌入式系统上的 I/O

发布于 2024-09-30 21:28:50 字数 179 浏览 1 评论 0原文

在我的项目中,我们使用使用 configparser 模块解析的配置文件。有没有办法缓存整个配置文件,然后使用 configparser 方法从内存中读取它?我希望不必将配置变量存储在字典中并以这种方式查找它们,因为使用 configparser 与我们的许多应用程序确实紧密结合,我希望我可以更改配置所在的位置指向内存而不是文件名。这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左岸枫 2024-10-07 21:28:50

是的。如果您使用 StringIO 模块,您可以在内存中创建一个类似文件的对象。

然后就可以使用该方法(ConfigParser继承自RawConfigParser):

RawConfigParser.readfp(fp[, 文件名])
从fp中的文件或类文件对象中读取并解析配置数据(仅使用readline()方法)。如果省略 filename 并且 fp 有一个 name 属性,则用于文件名;默认为 .

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文