python包的数据缓存
我有一个 python 模块,它生成大型数据文件,我想将其缓存在磁盘上以供将来使用。对于普通用户来说,缓存可能会达到数百MB,但可以节省大量计算时间。
这些文件不随模块一起分发,而是在第一次使用给定参数集运行代码时生成。
到目前为止,我自己只是使用单个文件模块,并将它们放在相对于模块的硬编码路径中(data/)。但我现在需要使用 distutils 在 Python 包中分发这个模块,我想知道是否有一个标准方法可以做到这一点。
我正在考虑类似 scipy.weave 的编译缓存之类的东西 - 但想知道是否有更现代的支持方式来实现它。在 *nix 平台上,我希望它出现在 ~/.something
中,但我不确定 Windows 上的等效项是什么。此外,这应该是可配置的,以便用户可以将其指向其他地方(如果更方便的话),或者在用户之间共享缓存目录。这样的配置文件应该如何工作?它应该去哪里?
或者我应该将其作为安装选项,通过 setup.py 旁边的配置文件或通过手动编辑 setup.py 进行设置,然后在安装之前对模块中的目录进行硬编码?
任何指示都非常收到...
I have a python module which generates large data files which I want to cache on disk for future use. The cache is likely to end up some hundreds of MB for a normal user, but save a lot of computation time.
The files aren't distributed with the module, but are generated the first time the code is run with a given set of parameters.
So far I've just been using a single file module myself and putting them in a hardcoded path relative to the module (data/). But I now need to distribute this module in a Python package with distutils and I was wondering if there is a standard way to do that.
I was thinking of something like the compiled cache of scipy.weave - but wondering if there is a more modern supported way of doing it. On *nix platforms I would expect it to go in ~/.something
but I'm not sure what the windows equivalent would be. Also this should configurable so that users can point it somewhere else if it's more convenient, or to share the cache dir between users. How should such a config file work? Where should it go?
Or should I just have it as an install option, either through a config file next to setup.py or set by manually editing setup.py, then hard code the directory in the module before installation?
Any pointers greatfully received...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用标准库模块 ConfigParser 来解析 ini 文件(或 .rc 文件)取决于您的文化)。要查找该文件, os.path.expanduser 是一个有用的函数,它可以在所有平台上为“~/.mytoolrc”等路径执行正确的操作。要让用户覆盖事物的位置,您可以通过 os 使用环境变量。环境。
You can use the standard library module ConfigParser to parse an ini file (or .rc file depending on your culture). To find the file, os.path.expanduser is a useful function that does the right thing on all platforms for paths like "~/.mytoolrc". To let the user override the location of things, you can use environment variables via os.environ.
自由操作系统世界中有一个新兴标准:http://standards。 freedesktop.org/basedir-spec/basedir-spec-latest.html
此模块可以为您在 Windows 和 Max OS X 上提供帮助,但它似乎在 XDG Base Dir Spec 方面被破坏了:http://pypi.python.org/pypi/appdirs
There is an emerging standard in the free OS world: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
This module can help you for Windows and Max OS X, but it seems to be broken with respect the the XDG Base Dir Spec: http://pypi.python.org/pypi/appdirs