Re: Loading Resources From a Python Module/Package
Donald Stufft <[email protected]> Sat, 31 Jan 2015 13:18:05 -0500
| Newsgroups | gmane.comp.python.import |
|---|---|
| Message-ID | <[email protected]> |
> On Jan 31, 2015, at 1:11 PM, Barry Warsaw <[email protected]> wrote: > > On Jan 31, 2015, at 11:43 AM, Donald Stufft wrote: > >> I think we do want to allow directories, it’s not unusual to have something >> like: >> >> warehouse >> ├── __init__.py >> ├── templates >> │ ├── accounts >> │ │ └── profile.html >> │ └── hello.html >> ├── utils >> │ └── mapper.py >> └── wsgi.py >> >> Conceptually templates isn’t a package (even though with namespace packages >> it kinda is) and I’d want to load profile.html by doing something like: >> >> importlib.resources.get_bytes(“warehouse”, “templates/accounts/profile.html”) > > I understand there's a conceptual wart, but I have no problem dropping an > empty __init__.py file in those subdirectories and then using: > > importlib.resources.get_bytes('warehouse.templates.accounts', 'profile.html') > > And given how much easier it makes life from an implementation and description > standpoint, I think it's a fine compromise. I think it actually makes things *harder* from an implementation and description standpoint. You’re thinking in terms of implementation for the FileLoader, but say for a PostgreSQLLoader now I have to create mock packages for warehouse.templates and warehouse.templates.accounts whereas if we treat the resource path not as a file path, but as a key for an object store where “/“ is slightly special then my PostgreSQL loader only need to have a “warehouse” package, and then a table that essentially does something like: package | resource key | data -------------------------------------------------- warehouse | templates/accounts/profile.html | … In the FileLoader we’d obviously treat the / as path separators and create directory entries, but in reality it’s just a key: value store. I already implemented one of these functions in a way that allows the / separator and I would have had to have gone out of my way to disallow it rather than allow it. --- Donald Stufft PGP: 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 DCFA _______________________________________________ Import-SIG mailing list [email protected] https://mail.python.org/mailman/listinfo/import-sig