Skip to content

Commit 8607fd7

Browse files
committed
Add illustration of text resources in packages
1 parent 34906a5 commit 8607fd7

File tree

6 files changed

+33
-0
lines changed

6 files changed

+33
-0
lines changed

source-code/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ was used to develop it.
3535
1. `error-handling`: simple illustration of error handling.
3636
1. `pyinstaller`: illustration of how to use `pyinstaller` to create
3737
standalone executables.
38+
1. `text-resources`: illustration on how to include text resources in
39+
a package and conveniently access them.

source-code/text-resources/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Text resources
2+
3+
It can be convenient to bundle text files in a package for convenient
4+
distribution and access.
5+
6+
7+
## What is it?
8+
9+
1. `show_text.py`: script that reads a text file from the package, and
10+
prints it to standard output.
11+
1. `lib/utils': package with a text file.
12+
1. `lib/utils/text_data.txt`: text file that contains some text.
13+
1. `run_show_text.sh`: Bash script that runs the `show_text.py` script. It
14+
sets the `PYTHONPATH` environment variable to include the `lib` directory to
15+
find the `utils` package.

source-code/text-resources/lib/utils/__init__.py

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This is a text
2+
with multiple lines
3+
to see how text resources
4+
work.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
export PYTHONPATH=lib
4+
5+
python show_text.py
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python
2+
3+
import importlib.resources
4+
5+
6+
text = importlib.resources.read_text('utils', 'text_data.txt')
7+
print(text)

0 commit comments

Comments
 (0)