Re: Loading Resources From a Python Module/Package
Barry Warsaw <[email protected]> Sat, 31 Jan 2015 13:29:33 -0500
| Newsgroups | gmane.comp.python.import |
|---|---|
| Organization | Damn Crazy Followers of the Horn |
| Message-ID | <20150131132933.28d485f5@marathon> |
On Jan 31, 2015, at 01:07 PM, Donald Stufft wrote: >The reasons for not wanting to use a context manager are sort of intertwined >with each other. > >The competitor to this function is something like: > > import os.path > import time > > LOGO_PATH = os.path.join(os.path.dirname(__file__), "logo.gif") > > def print_logo_path(): > print(LOGO_PATH) > > > while True: > print_logo_path() > time.sleep(1) I'm just wondering if that's extracted from a real example or whether it's just a possible use case you'd want to support. It's not a use case I've ever needed. I reviewed a bunch of resource_filename() uses and in almost all cases it's 1. Crafting a path-y thing for some other API that only takes paths. 2. Constructing a path for essentially shutil.copy()'ing the file somewhere else (e.g. a test http server's file vending directory). There are one or two where it might be inconvenient to use a context manager, but the majority of cases would be fine. >It makes it more akward to use anytime you need to use the file in multiple >locations or multiple times and since each context manager instance (in the >worst case) is going to need to get bytes, create a temp file, and write bytes >for each use of the context manager. Perhaps it makes sense to either provide two APIs and/or implement a higher level API on top of a lower-level one? >The other thing is that for the "common" case, where the resource is available >on the file system already because we're just using a FileLoader, there is no >need for an atexit handler or a temporary file at all. The context manager >would only really exist for the uncommon case where we need to write the data >to a temporary file. Using the atexit handler allows us to provide the best >API for the common case, without too much problem for the uncommon case. A context manager could also conditionalize the delete just like your proposal conditionalizes adding to the atexit handler. >Yes it does mean that in certain cases the temporary files may be left behind, >particularly with kill -9 or segfaults or what have you. However that case >already exists, the only thing the context manager does is narrow the window >of case where a kill -9 or a segfault can leave temporary files behind. Sure, but it reduces the window for leakage, which will probably be enough. Cheers, -Barry _______________________________________________ Import-SIG mailing list [email protected] https://mail.python.org/mailman/listinfo/import-sig
signature.asc
(application/pgp-signature, 819 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCAAGBQJUzR8NAAoJEBJutWOnSwa/pP4QAIEEAvn5+bbytUIUOr7daUgX +4hgNkymYLAdEvlLCiuo/zGozj4E3ATBKkgHZ8PKnuhyJX1ZY5ACR8FIX6qj+wzo JUwDoEaj/p6lwE6GT2kDKTNEh87NsyR59sFclbLSJ2G0L3WLA1Nek+dVHQQWWWex 5ukuX397fgdVOvWE54Mmc/BoRY7UnNzGhD4ZB8OGmCooTd5aiD4N2kgMB5ojFgI2 GafEmVfwQs4HUWV7GjDDMnlyey0DP3X+DWqn4UC3jWIQDxM3A0f6wRsGdr6q8ntF 0R66U9bki0i8bMPscHjd5Jp/XSJ1rsXatSwjDrqwOrVxw9a2iQeROQYB2kGLWmzm xXca9z3MA4Ys8RHW84KNtvhdEkZegB9+g5oyleUDQdd+xNsFUJkuDWoZgaLhhGe5 Ajwhsoxn4dMoYVWmNsc9xg1YuivqsViUEXQM82vXxK2zOEulflnbftnXsaucuZYV /6HcbpAHW/EByQXhQp+qog228K7ASsGi3pU2XS6edR+7yR6fwTgnpld2zwHXtohG NYEb+0Ea3iPsPHMCGpO40dlq6omYWakOU/XjNSm94Zu8aD1wOcF4D1CQnJN1qKJJ OAY0HFPvkNZCzOYCw1ks6LpWVY0sfO4QAbELkbKLrX8c3tMON1puOYVhLkn/0OKK t47q4FvCzX7XgvhmBG50 =+Q3R -----END PGP SIGNATURE-----