Skip to content

Commit 83ac43e

Browse files
Improve examples
1 parent c083740 commit 83ac43e

4 files changed

Lines changed: 64 additions & 7 deletions

File tree

.github/workflows/build.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ jobs:
5555
pip install -e .
5656
pytest --doctest-modules --junitxml=junit/pytest-results-${{ matrix.python-version }}.xml --cov=$PROJECT_NAME --cov-report=xml tests/
5757
58-
- name: Test examples
59-
run: |
60-
for f in ./examples/*.py; do echo "Processing $f file..." && python $f; done
61-
6258
- name: Run linters
6359
run: |
6460
echo "Running linters - if build fails here, please be patient!"

examples-summary.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Generates a README.md file for the examples folder.
3+
"""
4+
5+
import glob
6+
import importlib
7+
import sys
8+
9+
examples = [file for file in glob.glob("./examples/*.py")]
10+
examples.sort()
11+
sys.path.append("./examples")
12+
13+
with open("./examples/README.md", mode="wt", encoding="utf8 ") as examples_readme:
14+
examples_readme.write(
15+
"<!-- generated file, to update use: python examples-summary.py -->\n\n"
16+
)
17+
examples_readme.write("""# Examples""")
18+
19+
for file_path in examples:
20+
if "__init__" in file_path:
21+
continue
22+
23+
module_name = file_path.replace("./examples/", "").replace(".py", "")
24+
25+
module = importlib.import_module(module_name)
26+
27+
if not module.__doc__:
28+
continue
29+
30+
examples_readme.write(f"\n\n## {module_name}.py\n")
31+
examples_readme.write(str(module.__doc__))

examples/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!-- generated file, to update use: python examples-summary.py -->
2+
3+
# Examples
4+
5+
## example-01.py
6+
7+
This example illustrates a basic usage of the Container class to register
8+
two types, and automatic resolution achieved through types inspection.
9+
10+
Two services are registered as "transient" services, meaning that a new instance is
11+
created whenever needed.
12+
13+
14+
## example-02.py
15+
16+
This example illustrates a basic usage of the Container class to register
17+
a concrete type by base type, and its activation by base type.
18+
19+
This pattern helps writing code that is decoupled (e.g. business layer logic separated
20+
from exact implementations of data access logic).
21+
22+
23+
## example-03.py
24+
25+
This example illustrates how to configure a singleton object.

tests/test_examples.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import glob
2-
import subprocess
2+
import importlib
3+
import sys
34

45
import pytest
56

67
examples = [file for file in glob.glob("./examples/*.py")]
78

89

10+
sys.path.append("./examples")
11+
12+
913
@pytest.mark.parametrize("file_path", examples)
1014
def test_example(file_path: str):
11-
output = subprocess.run(["python", file_path])
12-
assert output.returncode == 0
15+
module_name = file_path.replace("./examples/", "").replace(".py", "")
16+
# assertions are in imported
17+
importlib.import_module(module_name)

0 commit comments

Comments
 (0)