Skip to content

Commit 2e8f591

Browse files
author
Roman Mogilatov
committed
Complete of Factory docs, new Factory examples
1 parent d9952d5 commit 2e8f591

File tree

6 files changed

+224
-94
lines changed

6 files changed

+224
-94
lines changed

docs/providers.rst

Lines changed: 101 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,21 @@ Nothing could be better than brief example:
2020
from objects.providers import Factory
2121
2222
23+
class User(object):
24+
25+
"""Example class User."""
26+
27+
2328
# Factory provider creates new instance of specified class on every call.
24-
object_factory = Factory(object)
29+
users_factory = Factory(User)
30+
31+
user1 = users_factory()
32+
user2 = users_factory()
33+
34+
assert user1 is not user2
35+
assert isinstance(user1, User) and isinstance(user2, User)
2536
26-
object_1 = object_factory()
27-
object_2 = object_factory()
2837
29-
assert object_1 is not object_2
30-
assert isinstance(object_1, object) and isinstance(object_2, object)
3138
3239
Factory providers and injections
3340
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -74,58 +81,116 @@ provided by another factories:
7481
from objects.injections import KwArg
7582
7683
77-
class A(object):
84+
class User(object):
7885
79-
"""Example class A.
86+
"""Example class User.
8087
81-
Class A has dependencies on class B and class C objects, that have to be
82-
provided as init arguments.
88+
Class User has dependencies on class Photo and class CreditCard objects,
89+
that have to be provided as init arguments.
8390
"""
8491
85-
def __init__(self, object_b, object_c):
86-
self.object_b = object_b
87-
self.object_c = object_c
88-
super(A, self).__init__()
92+
def __init__(self, main_photo, credit_card):
93+
"""Initializer.
8994
95+
:param main_photo: Photo
96+
:param credit_card: CreditCard
97+
:return:
98+
"""
99+
self.main_photo = main_photo
100+
self.credit_card = credit_card
101+
super(User, self).__init__()
90102
91-
class B(object):
92103
93-
"""Example class B."""
104+
class Photo(object):
94105
106+
"""Example class Photo."""
95107
96-
class C(object):
97108
98-
"""Example class C."""
109+
class CreditCard(object):
99110
111+
"""Example class CreditCard."""
100112
101-
# A, B, C factories:
102-
c_factory = Factory(C)
103-
b_factory = Factory(B)
104-
a_factory = Factory(A,
105-
KwArg('object_b', b_factory),
106-
KwArg('object_c', c_factory))
107113
108-
# Creating several A objects:
109-
object_a_1 = a_factory() # Same as: A(object_b=B(), object_c=C())
110-
object_a_2 = a_factory() # Same as: A(object_b=B(), object_c=C())
114+
# User, Photo and CreditCard factories:
115+
credit_cards_factory = Factory(CreditCard)
116+
photos_factory = Factory(Photo)
117+
users_factory = Factory(User,
118+
KwArg('main_photo', photos_factory),
119+
KwArg('credit_card', credit_cards_factory))
120+
121+
# Creating several User objects:
122+
user1 = users_factory() # Same as: User(main_photo=Photo(),
123+
# credit_card=CreditCard())
124+
user2 = users_factory() # Same as: User(main_photo=Photo(),
125+
# credit_card=CreditCard())
111126
112127
# Making some asserts:
113-
assert object_a_1 is not object_a_2
114-
assert object_a_1.object_b is not object_a_2.object_b
115-
assert object_a_1.object_c is not object_a_2.object_c
128+
assert user1 is not user2
129+
assert user1.main_photo is not user2.main_photo
130+
assert user1.credit_card is not user2.credit_card
131+
132+
133+
Next example shows how ``Factory`` provider deals with positional and keyword
134+
``__init__`` context arguments. In few words, ``Factory`` provider fully
135+
passes positional context arguments to class's ``__init__`` method, but
136+
keyword context arguments have priority on ``KwArg`` injections (this could be
137+
useful for testing). So, please, follow the example below:
138+
139+
.. code-block:: python
116140
141+
"""`Factory` providers with init injections and context arguments example."""
117142
118-
Need to make examples for:
143+
from objects.providers import Factory
144+
from objects.injections import KwArg
145+
146+
147+
class User(object):
148+
149+
"""Example class User."""
150+
151+
def __init__(self, id, main_photo):
152+
"""Initializer.
153+
154+
:param id: int
155+
:param main_photo: Photo
156+
:return:
157+
"""
158+
self.id = id
159+
self.main_photo = main_photo
160+
super(User, self).__init__()
161+
162+
163+
class Photo(object):
164+
165+
"""Example class Photo."""
166+
167+
168+
# User and Photo factories:
169+
photos_factory = Factory(Photo)
170+
users_factory = Factory(User,
171+
KwArg('main_photo', photos_factory))
172+
173+
# Creating several User objects:
174+
user1 = users_factory(1) # Same as: User(1, main_photo=Photo())
175+
user2 = users_factory(2) # Same as: User(1, main_photo=Photo())
176+
177+
# Making some asserts:
178+
assert user1.id == 1
179+
assert user2.id == 2
180+
assert user1 is not user2
181+
assert isinstance(user1.main_photo, Photo)
182+
assert isinstance(user2.main_photo, Photo)
183+
assert user1.main_photo is not user2.main_photo
119184
120-
- Several KwArgs usage. +
121-
- Factory depends on another factory. +
185+
# Context keyword arguments have priority on KwArg injections priority:
186+
photo_mock = Photo()
122187
123-
- KwArg usage with not provider injectable value.
188+
user3 = users_factory(3, main_photo=photo_mock)
124189
125-
- Context positional arguments usage with KwArgs.
126-
- Context keyword arguments priority on KwArgs.
190+
assert user3.id == 3
191+
assert user3 not in (user2, user1)
192+
assert user3.main_photo is photo_mock
127193
128-
- Context keyword arguments usage with KwArgs ???
129194
130195
Factory providers and attribute injections
131196
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

examples/providers/factory.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""`Factory` providers example."""
2+
3+
from objects.providers import Factory
4+
5+
6+
class User(object):
7+
8+
"""Example class User."""
9+
10+
11+
# Factory provider creates new instance of specified class on every call.
12+
users_factory = Factory(User)
13+
14+
user1 = users_factory()
15+
user2 = users_factory()
16+
17+
assert user1 is not user2
18+
assert isinstance(user1, User) and isinstance(user2, User)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""`Factory` providers with init injections and context arguments example."""
2+
3+
from objects.providers import Factory
4+
from objects.injections import KwArg
5+
6+
7+
class User(object):
8+
9+
"""Example class User."""
10+
11+
def __init__(self, id, main_photo):
12+
"""Initializer.
13+
14+
:param id: int
15+
:param main_photo: Photo
16+
:return:
17+
"""
18+
self.id = id
19+
self.main_photo = main_photo
20+
super(User, self).__init__()
21+
22+
23+
class Photo(object):
24+
25+
"""Example class Photo."""
26+
27+
28+
# User and Photo factories:
29+
photos_factory = Factory(Photo)
30+
users_factory = Factory(User,
31+
KwArg('main_photo', photos_factory))
32+
33+
# Creating several User objects:
34+
user1 = users_factory(1) # Same as: User(1, main_photo=Photo())
35+
user2 = users_factory(2) # Same as: User(1, main_photo=Photo())
36+
37+
# Making some asserts:
38+
assert user1.id == 1
39+
assert user2.id == 2
40+
assert user1 is not user2
41+
assert isinstance(user1.main_photo, Photo)
42+
assert isinstance(user2.main_photo, Photo)
43+
assert user1.main_photo is not user2.main_photo
44+
45+
# Context keyword arguments have priority on KwArg injections priority:
46+
photo_mock = Photo()
47+
48+
user3 = users_factory(3, main_photo=photo_mock)
49+
50+
assert user3.id == 3
51+
assert user3 not in (user2, user1)
52+
assert user3.main_photo is photo_mock
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""`Factory` providers with init injections example."""
2+
3+
from objects.providers import Factory
4+
from objects.injections import KwArg
5+
6+
7+
class User(object):
8+
9+
"""Example class User.
10+
11+
Class User has dependencies on class Photo and class CreditCard objects,
12+
that have to be provided as init arguments.
13+
"""
14+
15+
def __init__(self, main_photo, credit_card):
16+
"""Initializer.
17+
18+
:param main_photo: Photo
19+
:param credit_card: CreditCard
20+
:return:
21+
"""
22+
self.main_photo = main_photo
23+
self.credit_card = credit_card
24+
super(User, self).__init__()
25+
26+
27+
class Photo(object):
28+
29+
"""Example class Photo."""
30+
31+
32+
class CreditCard(object):
33+
34+
"""Example class CreditCard."""
35+
36+
37+
# User, Photo and CreditCard factories:
38+
credit_cards_factory = Factory(CreditCard)
39+
photos_factory = Factory(Photo)
40+
users_factory = Factory(User,
41+
KwArg('main_photo', photos_factory),
42+
KwArg('credit_card', credit_cards_factory))
43+
44+
# Creating several User objects:
45+
user1 = users_factory() # Same as: User(main_photo=Photo(),
46+
# credit_card=CreditCard())
47+
user2 = users_factory() # Same as: User(main_photo=Photo(),
48+
# credit_card=CreditCard())
49+
50+
# Making some asserts:
51+
assert user1 is not user2
52+
assert user1.main_photo is not user2.main_photo
53+
assert user1.credit_card is not user2.credit_card

examples/readme2/factory_providers.py

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

examples/readme2/factory_providers_init_injections.py

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

0 commit comments

Comments
 (0)