@@ -118,6 +118,8 @@ by simply importing them:
118
118
page = FurnitureProductPage(response)
119
119
item = page.to_item()
120
120
121
+ .. _`pop-recommended-requirements` :
122
+
121
123
Recommended Requirements
122
124
~~~~~~~~~~~~~~~~~~~~~~~~
123
125
@@ -141,45 +143,83 @@ all :class:`~.OverrideRule` by writing the following code inside of
141
143
142
144
.. code-block :: python
143
145
144
- from web_poet import consume_modules
146
+ from web_poet import default_registry
147
+
148
+ REGISTRY = default_registry.export(__package__ )
145
149
146
- # This allows all of the OverrideRules declared inside the package
147
- # using @handle_urls to be properly discovered and loaded.
148
- consume_modules(__package__ )
150
+ This does two things:
149
151
150
- .. note ::
152
+ 1. The :meth: `~.PageObjectRegistry.export ` method returns a new instance of
153
+ :class: `~.PageObjectRegistry ` which contains only the :class: `~.OverrideRule `
154
+ from the given package. This means that if there are other **POPs ** using the
155
+ recommended ``default_registry ``, any :class: `~.OverrideRule ` that are not part
156
+ of the package are not included.
151
157
152
- Remember, code in Python like annotations are only read and executed
158
+ 2. Remember that code in Python like annotations are only read and executed
153
159
when the module it belongs to is imported. Thus, in order for all the
154
160
``@handle_urls `` annotation to properly reflect its data, they need to
155
- be imported recursively via :func: `~.consume_modules `.
161
+ be imported recursively via :func: `~.consume_modules `. Fortunately,
162
+ :meth: `~.PageObjectRegistry.export ` already consumes the ``__package__ ``
163
+ for us.
156
164
157
165
This allows developers to properly access all of the :class: `~.OverrideRule `
158
- declared using the ``@handle_urls `` annotation inside the **POP **. In turn,
159
- this also allows **POPs ** which use ``web_poet.default_registry `` to have all
160
- their rules discovered if they are adhering to using Convention **#3 **
161
- (see :ref: `best-practices `).
166
+ declared using the ``@handle_urls `` annotation inside the **POP **:
162
167
163
- In other words, importing the ``ecommerce_page_objects `` **POP ** to a
164
- project immediately loads all of the rules in **web-poet's **
165
- ``default_registry ``:
168
+ .. code-block :: python
169
+
170
+ import ecommerce_page_objects
171
+
172
+ ecommerce_rules = ecommerce_page_objects.get_overrides()
173
+
174
+ At the same time, this also allows **POPs ** which use ``web_poet.default_registry ``
175
+ to have all their rules discovered if they are adhering to using Convention **#3 **
176
+ (see :ref: `conventions-and-best-practices `). In other words, importing the
177
+ ``ecommerce_page_objects `` **POP ** to a project immediately loads all of the rules
178
+ in **web-poet's ** ``default_registry ``:
166
179
167
180
.. code-block :: python
168
181
169
182
from web_poet import default_registry
170
183
184
+ ecommerce_rules = ecommerce_page_objects.get_overrides()
185
+
171
186
import ecommerce_page_objects
172
187
173
- # All the rules are now available.
174
- rules = default_registry.get_overrides()
188
+ # All the rules are also available once ecommerce_page_objects is imported .
189
+ all_rules = default_registry.get_overrides()
175
190
176
191
If this recommended requirement is followed properly, there's no need to
177
192
call ``consume_modules("ecommerce_page_objects") `` before performing the
178
193
:meth: `~.PageObjectRegistry.get_overrides `, since all the :class: `~.OverrideRule `
179
- were already discovered upon **POP ** importation.
194
+ were already discovered upon **POP ** importation.
195
+
196
+ Lastly, when trying to repackage multiple **POPs ** into a single unifying **POP **
197
+ which contains all of the :class: `~.OverrideRule `, it can easily be packaged
198
+ as:
199
+
200
+ .. code-block :: python
201
+
202
+ from web_poet import PageObjectRegistry
203
+
204
+ import base_A_package
205
+ import base_B_package
206
+
207
+ # If on Python 3.9+
208
+ combined_reg = base_A_package.REGISTRY | base_B_package.REGISTRY
209
+
210
+ # If on lower Python versions
211
+ combined_reg = {** base_A_package.REGISTRY , ** base_B_package.REGISTRY }
212
+
213
+ REGISTRY = PageObjectRegistry(combined_reg)
180
214
181
- .. _`best-practices` :
215
+ Note that you can also opt to use only a subset of the :class: `~.OverrideRule `
216
+ by selecting the specific ones in ``combined_reg `` before creating a new
217
+ :class: `~.PageObjectRegistry ` instance. An **inclusion ** rule is preferred than
218
+ an **exclusion ** rule (see **Tip #4 ** in the :ref: `conventions-and-best-practices `).
219
+ You can use :meth: `~.PageObjectRegistry.search_overrides ` when selecting the
220
+ rules.
182
221
222
+ .. _`conventions-and-best-practices` :
183
223
184
224
Conventions and Best Practices
185
225
------------------------------
0 commit comments