Skip to content

Commit 31f15c0

Browse files
committed
Merge branch 'release/3.41.0' into master
2 parents e257fd9 + ba57466 commit 31f15c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+566
-598
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Chained Factories pattern
2+
=========================
3+
4+
This example demonstrates "Chained Factories" pattern.
5+
6+
The idea of the pattern is in wrapping ``Factory`` into another ``Factory`` that adds
7+
additional arguments.
8+
9+
.. code-block:: python
10+
11+
base_factory = providers.Factory(
12+
SomeClass,
13+
base_argument=1,
14+
)
15+
16+
concrete_factory = providers.Factory(
17+
base_factory,
18+
extra_argument=2,
19+
)
20+
21+
22+
if __name__ == '__main__':
23+
instance = concrete_factory()
24+
# Same as: # instance = SomeClass(base_argument=1, extra_argument=2)
25+
26+
Sample code
27+
-----------
28+
29+
Listing of the pattern example:
30+
31+
.. literalinclude:: ../../examples/miniapps/factory-patterns/chained_factories.py
32+
:language: python
33+
34+
Arguments priority
35+
------------------
36+
37+
Passing of the arguments works the same way like for any other :ref:`factory-provider`.
38+
39+
.. code-block:: python
40+
41+
# 1. Keyword arguments of upper level factory are added to lower level factory
42+
chained_dict_factory = providers.Factory(
43+
providers.Factory(dict, arg1=1),
44+
arg2=2,
45+
)
46+
print(chained_dict_factory()) # prints: {'arg1': 1, 'arg2': 2}
47+
48+
# 2. Keyword arguments of upper level factory have priority
49+
chained_dict_factory = providers.Factory(
50+
providers.Factory(dict, arg1=1),
51+
arg1=2,
52+
)
53+
print(chained_dict_factory()) # prints: {'arg1': 2}
54+
55+
# 3. Keyword arguments provided from context have the most priority
56+
chained_dict_factory = providers.Factory(
57+
providers.Factory(dict, arg1=1),
58+
arg1=2,
59+
)
60+
print(chained_dict_factory(arg1=3)) # prints: {'arg1': 3}
61+
62+
63+
Credits
64+
-------
65+
66+
The "Chained Factories" pattern was suggested by the ``Dependency Injector`` users.
67+
68+
.. disqus::

docs/examples-other/chained_factories.rst

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Factory of Factories pattern
2+
============================
3+
4+
This example demonstrates "Factory of Factories" pattern.
5+
6+
The idea of the pattern is in creating a ``Factory`` that creates another ``Factory`` and adds
7+
additional arguments.
8+
9+
.. code-block:: python
10+
11+
base_factory = providers.Factory(
12+
providers.Factory
13+
SomeClass,
14+
base_argument=1,
15+
)
16+
17+
concrete_factory = providers.Factory(
18+
OtherClass,
19+
instance=base_factory(extra_argument=1),
20+
)
21+
22+
23+
if __name__ == '__main__':
24+
instance = concrete_factory()
25+
# Same as: # instance = SomeClass(base_argument=1, extra_argument=2)
26+
27+
Sample code
28+
-----------
29+
30+
Listing of the pattern example:
31+
32+
.. literalinclude:: ../../examples/miniapps/factory-patterns/factory_of_factories.py
33+
:language: python
34+
35+
Arguments priority
36+
------------------
37+
38+
Passing of the arguments works the same way like for any other :ref:`factory-provider`.
39+
40+
.. code-block:: python
41+
42+
# 1. Keyword arguments of upper level factory are added to lower level factory
43+
factory_of_dict_factories = providers.Factory(
44+
providers.Factory,
45+
dict,
46+
arg1=1,
47+
)
48+
dict_factory = factory_of_dict_factories(arg2=2)
49+
print(dict_factory()) # prints: {'arg1': 1, 'arg2': 2}
50+
51+
# 2. Keyword arguments of upper level factory have priority
52+
factory_of_dict_factories = providers.Factory(
53+
providers.Factory,
54+
dict,
55+
arg1=1,
56+
)
57+
dict_factory = factory_of_dict_factories(arg1=2)
58+
print(dict_factory()) # prints: {'arg1': 2}
59+
60+
# 3. Keyword arguments provided from context have the most priority
61+
factory_of_dict_factories = providers.Factory(
62+
providers.Factory,
63+
dict,
64+
arg1=1,
65+
)
66+
dict_factory = factory_of_dict_factories(arg1=2)
67+
print(dict_factory(arg1=3)) # prints: {'arg1': 3}
68+
69+
Credits
70+
-------
71+
72+
The "Factory of Factories" pattern was suggested by the ``Dependency Injector`` users.
73+
74+
.. disqus::

docs/examples-other/factory_of_factories.rst

Lines changed: 0 additions & 20 deletions
This file was deleted.

docs/examples-other/index.rst

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ Other examples
22
==============
33

44
.. meta::
5-
:keywords: Python,DI,Dependency injection,IoC,Inversion of Control
6-
:description: Current section of documentation is designed to provide
7-
several example mini applications that are built on the top
8-
of inversion of control principle and powered by
9-
"Dependency Injector" framework.
5+
:keywords: Python,Dependency Injection,Inversion of Control,Container,Example,Application,
6+
Framework
7+
:description: This sections contains assorted Dependency Injector examples.
108

119
This sections contains assorted ``Dependency Injector`` examples.
1210

1311
.. toctree::
1412
:maxdepth: 2
1513

16-
use_cases_miniapp
17-
password_hashing_miniapp
18-
chained_factories
19-
factory_of_factories
14+
use-cases
15+
password-hashing
16+
chained-factories
17+
factory-of-factories
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Password hashing example
2+
========================
3+
4+
.. meta::
5+
:keywords: Python,Dependency Injection,Inversion of Control,Container,Example,Application,
6+
Framework,Callable
7+
:description: This example demonstrates a usage of the Callable provider.
8+
9+
This example demonstrates an injection of the ``Callable`` provider.
10+
11+
The source code is available on the `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/password-hashing>`_.
12+
13+
Sample code
14+
-----------
15+
16+
Listing of the pattern example:
17+
18+
.. literalinclude:: ../../examples/miniapps/password-hashing/example.py
19+
:language: python
20+
21+
Run the example
22+
---------------
23+
24+
Instructions for running:
25+
26+
.. code-block:: bash
27+
28+
python example.py
29+
30+
.. disqus::

docs/examples-other/password_hashing_miniapp.rst

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/examples-other/use-cases.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Use cases example
2+
=================
3+
4+
.. meta::
5+
:keywords: Python,Dependency Injection,Inversion of Control,Container,Example,Application,
6+
Framework,DependenciesContainer
7+
:description: This example demonstrates a usage of the DependenciesContainer provider.
8+
9+
This example demonstrates a usage of the ``DependenciesContainer`` provider.
10+
11+
The source code is available on the `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/decoupled-packages>`_.
12+
13+
Application structure
14+
---------------------
15+
16+
Example application has next structure:
17+
18+
.. code-block:: bash
19+
20+
21+
./
22+
└── example/
23+
├── __init__.py
24+
├── __main__.py
25+
├── adapters.py
26+
├── containers.py
27+
└── usecases.py
28+
29+
Containers
30+
----------
31+
32+
Listing of the ``example/containers.py``:
33+
34+
.. literalinclude:: ../../examples/miniapps/use-cases/example/containers.py
35+
:language: python
36+
37+
Main module
38+
-----------
39+
40+
Listing of the ``example/__main__.py``:
41+
42+
.. literalinclude:: ../../examples/miniapps/use-cases/example/__main__.py
43+
:language: python
44+
45+
46+
Run the application
47+
-------------------
48+
49+
Instructions for running in the "test" mode:
50+
51+
.. code-block:: bash
52+
53+
python run.py test example@example.com
54+
55+
Instructions for running in the "prod" mode:
56+
57+
.. code-block:: bash
58+
59+
python run.py prod example@example.com
60+
61+
Adapters and use cases
62+
----------------------
63+
64+
Listing of the ``example/adapters.py``:
65+
66+
.. literalinclude:: ../../examples/miniapps/use-cases/example/adapters.py
67+
:language: python
68+
69+
Listing of the ``example/usecases.py``:
70+
71+
.. literalinclude:: ../../examples/miniapps/use-cases/example/usecases.py
72+
:language: python
73+
74+
.. disqus::

docs/examples-other/use_cases_miniapp.rst

Lines changed: 0 additions & 55 deletions
This file was deleted.

docs/examples/decoupled-packages.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _decoupled-packages:
2+
13
Decoupled packages example (multiple containers)
24
================================================
35

0 commit comments

Comments
 (0)