Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Mixins documentation #133

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ Contents
item-validation
settings
actions
mixins
changelog

109 changes: 109 additions & 0 deletions docs/source/mixins.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
.. _mixins:

======
Mixins
======

What's a Mixin in Python?
-------------------------

A mixin is a special kind of multiple inheritance. There are two main situations where mixins are used:

* You want to provide a lot of optional features for a class.
* You want to use one particular feature in a lot of different classes.

That's a little definition, but if you want more info you can see that `StackOverFlow post`_, or this `blog`_

.. _`StackOverFLow post`: https://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful
.. _`blog`: https://easyaspython.com/mixins-for-fun-and-profit-cb9962760556

With Spidermon can use four mixins: `StatsMonitorMixin`_, `ValidationMonitorMixin`_, `SpiderMonitorMixin`_, `JobMonitorMixin`_ mixins

.. _`StatsMonitorMixin`:

StatsMonitorMixin
-----------------

`StatsMonitorMixin code`_

.. _`StatsMonitorMixin code`: https://github.com/scrapinghub/spidermon/blob/master/spidermon/contrib/monitors/mixins/stats.py


We have an example in the next example:

.. code-block:: python

# monitors.py
# (...other monitors...)

@monitors.name('Item validation')
class ItemValidationMonitor(Monitor, StatsMonitorMixin):

@monitors.name('No item validation errors')
def test_no_item_validation_errors(self):
validation_errors = getattr(
self.stats, 'spidermon/validation/fields/errors', 0
)
self.assertEqual(
validation_errors,
0,
msg='Found validation errors in {} fields'.format(
validation_errors)
)


In this example, the `ItemValidationMonitor` have inheritance of `Monitor` class. The `Monitor` class have the data property
and in this data property is an `stats` dict. Then, if we don't want to use the mixin, we can do something like:

.. code-block:: python

@monitors.name('Item validation')
class ItemValidationMonitor(Monitor):

@monitors.name('No item validation errors')
def test_no_item_validation_errors(self):
validation_errors = getattr(
self.data.stats, 'spidermon/validation/fields/errors', 0
)
...
...
...

So, why use mixin? Mixin is used to provide optional features for a class, for this example, we can use the mixin to create another class with the property `data.stats` but if is not configured, an exception will rise.


.. _`JobMonitorMixin`:

JobMonitorMixin
---------------

`JobMonitorMixin code`_

.. _`JobMonitorMixin code`: https://github.com/scrapinghub/spidermon/blob/master/spidermon/contrib/monitors/mixins/job.py

This is similar to `StatsMonitorMixin`_. If the object don't have the property `data.job`, it will raise an exception

.. _`SpiderMonitorMixin`:

SpiderMonitorMixin
------------------

`SpiderMonitorMixin code`_

.. _`SpiderMonitorMixin code`: https://github.com/scrapinghub/spidermon/blob/master/spidermon/contrib/monitors/mixins/spider.py

This class use `StatsMonitorMixin`_ and `JobMonitorMixin`_ . `SpiderMonitorMixin` add the `crawler`, `spider` and `responses` property for be use like the examples above.

This mixin create an `_response` property that is an object for `ResponsesInfo` class. This class has the stats for the response, you can get the number of all codes for requests,
informational, successfuls, redirections, bad requests, internal server errors, others and errors.

.. _`ValidationMonitorMixin`:

ValidationMonitorMixin
----------------------

`ValidationMonitorMixin code`_

.. _`ValidationMonitorMixin code`: https://github.com/scrapinghub/spidermon/blob/master/spidermon/contrib/monitors/mixins/validation.py

This class use `StatsMonitorMixin`_ and add the `_validation` property.