Re: Loading Resources From a Python Module/Package
Barry Warsaw <[email protected]> Sat, 31 Jan 2015 12:44:51 -0500
| Newsgroups | gmane.comp.python.import |
|---|---|
| Organization | The Organization of Unorganized Woozalists |
| Message-ID | <20150131124451.582b5dc3@marathon> |
On Jan 30, 2015, at 07:52 PM, Donald Stufft wrote: >> On Jan 30, 2015, at 7:18 PM, Paul Moore <[email protected]> wrote: >> Related question - how would the temp files be cleaned up? At exit? > >My patch registers an atexit handler that cleans up the temporary files yea. Why not implement it as a context manager? I'm not a big fan of overloading the atexit handler because there are situations where it might not get called (e.g. the program crashes or is kill -9'd), but a context manager allows the resource to be cleaned up asap. Reviewing my own uses of pkg_resources.resource_filename() I think it would work just fine because I rarely need the path much longer than the immediate operation. If I did need to cache it more permanently, I could easily do: with resource_filename('my.package.path', 'foo.dat') as path: shutil.copy(path, some_more_permanent_location) Easy peasy. Cheers, -Barry