
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/client_node/3_attributes.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_client_node_3_attributes.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_client_node_3_attributes.py:


Nodes attributes
================

Showcases the Python-like :py:class:`pyaseba.client.Node` interface,
that exposes Aseba variables and functions as Python attributes and methods.

.. GENERATED FROM PYTHON SOURCE LINES 8-12

.. code-block:: Python


    from pyaseba.client import Node
    from pyaseba.client.node import EventSpec, MirroringConfig








.. GENERATED FROM PYTHON SOURCE LINES 13-23

When specialized, py:class:`pyaseba.client.Node` offers a higher-level,
more Python-affine interface:

- Python properties for a selection of Aseba variables.

- Python methods ``node.call_<function>(...)`` as an alternative to
  ``node.call(<function>, ...)``.

In this example, the node exposes properties
``counter`` and ``value``, and method `call_square``.

.. GENERATED FROM PYTHON SOURCE LINES 23-36

.. code-block:: Python



    class MySimpleNode(Node):
        mirroring_config = MirroringConfig(
            events={"event": EventSpec(variables=["counter"])},
            function_include=["square"])
        properties = ['counter', 'value']
        functions = ['square']


    node = MySimpleNode(cached=True)
    node.connect(target="tcp:port=33333", start_mirroring=True)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    True



.. GENERATED FROM PYTHON SOURCE LINES 37-38

Aseba variables are now accessible as attributes.

.. GENERATED FROM PYTHON SOURCE LINES 38-41

.. code-block:: Python


    node.value = 3  # type: ignore[attr-defined]








.. GENERATED FROM PYTHON SOURCE LINES 42-44

Pushing changes from Python to Aseba is postponed until
:py:meth:`pyaseba.client.Node.sync` is called.

.. GENERATED FROM PYTHON SOURCE LINES 44-48

.. code-block:: Python


    node.sync()
    node.value  # type: ignore[attr-defined]





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    3



.. GENERATED FROM PYTHON SOURCE LINES 49-50

Calling

.. GENERATED FROM PYTHON SOURCE LINES 50-53

.. code-block:: Python


    node.call_square(3)  # type: ignore[attr-defined]








.. GENERATED FROM PYTHON SOURCE LINES 54-57

should set ``value`` to
the square of 3. As ``value`` is not synchronized,
we need to query it explicitly.

.. GENERATED FROM PYTHON SOURCE LINES 57-63

.. code-block:: Python


    import time
    time.sleep(1)

    print(node.get("value", cached=False))





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    3




.. GENERATED FROM PYTHON SOURCE LINES 64-65

.. code-block:: Python

    node.close(reset=True)








.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 2.016 seconds)


.. _sphx_glr_download_gallery_client_node_3_attributes.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 3_attributes.ipynb <3_attributes.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 3_attributes.py <3_attributes.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 3_attributes.zip <3_attributes.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
