Skip to content

Commit

Permalink
src/doc/en/developer/coding_basics.rst: Remove advice on how to impor…
Browse files Browse the repository at this point in the history
…t from importlib.resources, fix markup
  • Loading branch information
Matthias Koeppe committed Mar 22, 2024
1 parent 2b2d028 commit 3cfab54
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions src/doc/en/developer/coding_basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,14 @@ included in one of the following places:
The preferred way to access the data from Python is using the
`importlib.resources API <https://importlib-resources.readthedocs.io/en/latest/using.html>`,
in particular the function :func:`importlib.resources.files`.
It should be imported as follows
(see :ref:`section-python-language-standard`)::

import sys

if sys.version_info < (3, 11):
# Use backport package providing Python 3.11 features
from importlib_resources import files
else:
from importlib.resources import files

After this import, you can:

- open a resource for text reading: `fd = files(package).joinpath(resource).open('rt')`
- open a resource for binary reading: `fd = files(package).joinpath(resource).open('rb')`
- read a resource as text: `text = files(package).joinpath(resource).read_text()`
- read a resource as bytes: `bytes = files(package).joinpath(resource).read_bytes()`
- open a xz resource for text reading: `fd = lzma.open(files(package).joinpath(resource).open('rb'), 'rt')`
- open a xz resource for binary reading: `fd = lzma.open(files(package).joinpath(resource).open('rb'), 'rb')`
Using it, you can:

- open a resource for text reading: ``fd = files(package).joinpath(resource).open('rt')``
- open a resource for binary reading: ``fd = files(package).joinpath(resource).open('rb')``
- read a resource as text: ``text = files(package).joinpath(resource).read_text()``
- read a resource as bytes: ``bytes = files(package).joinpath(resource).read_bytes()``
- open an xz-compressed resource for text reading: ``fd = lzma.open(files(package).joinpath(resource).open('rb'), 'rt')``
- open an xz-compressed resource for binary reading: ``fd = lzma.open(files(package).joinpath(resource).open('rb'), 'rb')``

If the file needs to be used outside of Python, then the
preferred way is using the context manager
Expand Down

0 comments on commit 3cfab54

Please sign in to comment.