<commit_before>Action Handlers
================

Action handlers describe how your service mutates its internal state in
response to the arrival of an action from the queue. They are defined as
a function of two arguments: ``action_type`` and ``payload``. ``Action_type`` is a string that
classifies the event and ``payload`` is a dictionary representing the
data associated with the event. For example,


.. code-block:: python

    def action_handler(action_type, payload, dispatcher):
        # if the payload represents a new recipe to add to the list
        if action_type == 'create_recipe':
            # create a new instance of the recipe model
            recipe = Recipe(**payload)
            # save the new model
            recipe.save()
        # otherwise if the payload is the id of a recipe to be deleted
        elif action_type == 'delete_recipe':
            # find the matching recipe
            recipe = Recipe.query.first(Recipe.id == payload)
            # remove the recipe from the database
            recipe.remove()


Action Handlers are defined within the service:

.. code-block:: python


    class MyService(Service):

        def action_handler(self, action_type, payload, dispatcher):
            # ...


Reusing and Combining Action Handlers
---------------------------------------

As your services get more complex, you'll want to split your action handler into
separate functions which each get called. Nautilus provides a function called
``combine_action_handlers`` which serves just this case:

.. autofunction:: nautilus.network.amqp.combine_action_handlers

.. code-block:: python

    from nautilus.network import combine_action_handlers

    def action_handler1(action_type, payload):
        print("first handler fired!")

    def action_handler2(action_type, payload):
        print("second handler fired!")

    combined_handler = combine_action_handlers(
        action_handler1,
        action_handler2
    )

In order to do this within your service, you'll need to wrap it in a property:

.. code-block:: python

    from nautilus import Service

    class MyService(Service):

        @property
        def action_handler(self):
        
            # assuming action_handlers 1 and 2 were defined somewhere
            return combine_action_handlers(
                action_handler1,
                action_handler2
            )
<commit_msg>minor docs touchup<commit_after>Action Handlers
================

Action handlers describe how your service mutates its internal state in
response to the arrival of an action from the queue. They are defined as
a function of two arguments: ``action_type`` and ``payload``. ``Action_type`` is a string that
classifies the event and ``payload`` is a dictionary representing the
data associated with the event. For example,


.. code-block:: python

    def action_handler(action_type, payload, dispatcher):
        # if the payload represents a new recipe to add to the list
        if action_type == 'create_recipe':
            # create a new instance of the recipe model
            recipe = Recipe(**payload)
            # save the new model
            recipe.save()
        # otherwise if the payload is the id of a recipe to be deleted
        elif action_type == 'delete_recipe':
            # find the matching recipe
            recipe = Recipe.query.first(Recipe.id == payload)
            # remove the recipe from the database
            recipe.remove()


Action Handlers are defined within the service:

.. code-block:: python


    class MyService(Service):

        def action_handler(self, action_type, payload, dispatcher):
            # ...


Reusing and Combining Action Handlers
---------------------------------------

As your services get more complex, you'll want to split your action handler into
separate functions which each get called. Nautilus provides a function called
``combine_action_handlers`` which serves just this case:

.. autofunction:: nautilus.network.amqp.combine_action_handlers

.. code-block:: python

    from nautilus.network import combine_action_handlers

    def action_handler1(action_type, payload):
        print("first handler fired!")

    def action_handler2(action_type, payload):
        print("second handler fired!")

    combined_handler = combine_action_handlers(
        action_handler1,
        action_handler2
    )

In order to do this within your service, you'll need to wrap it in a property:

.. code-block:: python

    from nautilus import Service
    from nautilus.network import combine_action_handlers

    class MyService(Service):

        @property
        def action_handler(self):
            # assuming action_handlers 1 and 2 were defined as above
            return combine_action_handlers(
                action_handler1,
                action_handler2
            )

{'fraction_non_alphanumeric': 0.08477722772277228, 'fraction_numerical': 0.0033003300330033004, 'mean_word_length': 2.6403903903903903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2861039', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1185, 'n_words': 483}