Glossary ******** .. glossary:: Bluetooth Wireless technology used to communicate with :term:`Ozobot` directly from PC, phone or tablet. Look `here `__ for more detailed info. Cooperative scheduling Type of scheduling without preemption. Look `here `__ for more detailed info. If you would like to see how this scheduling is implemented in :term:`Ozobot`, you can have a look at :mod:`ozobot.asyncio` module. Coroutines Routine which is capable of remembering its internal state. It does not need a call stack to carry its internal state (Stack-less coroutines). It can be terminated at one point and continues at the same point with the next call. :term:`Cooperative scheduling` is usually based on :term:`Python` coroutines with :py:mod:`~ozobot.asyncio` module used for scheduling orchestration. Garbage Collector As :term:`Python` does not have a deallocation function unlike languages such as C or C++, there is a system called garbage collector. This system is responsible for finding :term:`Python` objects which are not in use and releasing them to free memory. The influence of this functionality is very important especially for :term:`Python` running directly inside the robot, as collection and deallocation of memory may take some time. Sometimes, it is beneficial to tweak this process from outside by :term:`Python` module :py:mod:`gc`. Look `here `__ for more detailed info. OzoBlockly One of the possible languages which may be used to program :term:`ozobot`. Language comes from `Google blockly project `_ and is modified to be able to generate code for :term:`ozobot`. See `OzoBlockly web page `_ for more info. Ozobot Everyone already knows `this nice guy `__ ;-) Python `Python `_ is a popular programming language aimed at ability to solve algorithmic tasks in an efficient and simple way. This programming language may be present on your PC, can be running in your web browser or can even run in tiny embedded devices such as some :term:`Ozobot` models. Look `here `__ for more detailed info. Pyodide `Pyodide `_ is a web variant of :term:`Python`. This variant of python runs directly inside a web browser and communicates with the robot over :term:`Bluetooth`. Documentation legend ******************** This part of documentation makes only links with brief explanation of some tags used inside documentation. .. _python_any: Any type -------- As :term:`Python` resolves types in runtime, it may happen that a function returns or accepts any argument type. In this case it does not matter if the argument is :class:`int`, :class:`str` or something else ... it can be anything. .. _python_optional: Optional type ------------- :term:`Python` also supports the possibility to define some arguments just optionally. Argument can be used, or some default value is used, or it may even happen that there is no default value defined. .. code-block:: python def my_generator(cnt = 8): # cnt argument is optional. a = 1 # When it is omitted, then value 8 is used for i in range(cnt): yield a a *= 2 .. _python_type: Type type --------- This looks like a typo in this chapter name, however, it's true that type definition in python is also an object and can be accessed in runtime. .. code-block:: python >>> def my_new_iterable(tp): ... return tp(i for i in range(6)) ... >>> print(my_new_iterable(list)) [0, 1, 2, 3, 4, 5] >>> print(my_new_iterable(tuple)) (0, 1, 2, 3, 4, 5) .. _python_iterable: Iterable type ------------- Iterable type in :term:`Python` is a type which can be used as an iterator in a ``for`` cycle. Iterable type is e.g. returned by function :func:`range`. Also, some basic types such as `list `_ or `tuple `_ are iterators. A special kind of iterable types are `python generators `__. .. code-block:: python for i in ( 1, 2, 4, 8, 16 ): # Tuple is iterable type. print('iteration of tuple', i) for i in range(6): # Function range returns generator print('iteration of range generator', i) # which can be used in a for cycle. def my_generator(cnt): a = 1 for i in range(cnt): yield a a *= 2 for i in my_generator(5): # Generator is iterable as well print('iteration of my generator', i) # This generator allows to get the same numbers # in a for cycle as does my_iterable1 When an :ref:`iterable ` type appears in documentation, its argument is the type of expected elements obtained during iteration. For example :ref:`iterable ` **(** :class:`int` | :class:`float` **)** means that the tuple may contain integers or floats (or both). .. _python_sequence: Sequence type ------------- Sequence type is an :ref:`iterable ` type which also knows its length. Typical sequence types are e.g. :ref:`list `, :ref:`tuple `, :class:`bytes` or :class:`str`. .. _python_list: List type --------- List in documentation means basic :term:`Python` list, like in the following example: .. code-block:: python my_list_1 = [ 1, 2, 'hello', 'list' ] # Creates list containing values 1, 2, 'hello', 'list' my_list_2 = [ a for a in range(6) ] # Creates list containing values 0, 1, 2, 3, 4, 5 Just note that :ref:`list ` is a type of :ref:`sequence `, so it means that it is :ref:`iterable ` with defined length. Where :ref:`list ` type appears in documentation, its argument is the type of expected list elements. For example :ref:`list ` **(** :class:`int` | :class:`float` **)** means that the list may contain integers or floats (or both), e.g. like ``[ 1, 2, 3.5, 8.2 ]``. .. _python_tuple: Tuple type ---------- Tuple is a type similar to `list `_, but it's more simple and more efficient to use. However unlike `list `_ it also hs its own limitations, the most visible being the fact that tuple is like a "constant" list. This means that once it is created, you can not change its items and its size. .. code-block:: python my_tuple_1 = ( 1, 2, 'hello', 'list' ) # Creates list containing values 1, 2, 'hello', 'list' my_tuple_2 = tuple( a for a in range(6) ) # Creates list containing values 0, 1, 2, 3, 4, 5 Just note that :ref:`tuple ` is a type of :ref:`sequence `, so it means that it is :ref:`iterable ` with defined length. When :ref:`tuple ` type appears in documentation, its argument is the type of expected tuple elements. For example, :ref:`tuple ` **(** :class:`int` | :class:`float` **)** means that the tuple may contain integers or floats (or both), e.g. like ``( 1, 2, 3.5, 8.2 )``. .. _python_named_tuple: Named tuple type ---------------- Named tuple is a type similar to `tuple `_ (we can even say that it's derived from it), but it has the additional feature that each member can be accessed also by its name. .. code-block:: python >>> from ozobot.collections import namedtuple >>> MyTuple = namedtuple('MyTuple', ('member_1', 'member_2')) >>> my_tuple = MyTuple( 1, 2 ) >>> print('{} == {}'.format(my_tuple[0], my_tuple.member_1)) 1 == 1 >>> print('{} == {}'.format(my_tuple[1], my_tuple.member_2)) 2 == 2 .. _python_function: Function type ------------- In :term:`Python` everything is represented by objects. Functions and class methods are not an exception. .. code-block:: python def my_callback(num): print('My number is', num) def my_caller(function): for i in range(10): function(i) # We can use function as an argument of other function my_caller(my_callback) When you see a :ref:`function ` tag in documentation, you can see it either without any arguments (arguments or return are not specified), or you can see which arguments are expected and what the function shall return. For example, a function expecting one :class:`int` and one :class:`float` and returning :class:`str` will be shown like :ref:`function ` (:class:`int`, :class:`float`) → :class:`str`. .. _python_coroutine: Coroutine type -------------- :term:`Coroutines` as functions are also represented by a custom type which is often used by :term:`Cooperative scheduling` in Python. Such routine may represent some task or subtask in scheduler: .. code-block:: python from ozobot import get_robot, asyncio, Lights, SurfaceColor # This is a coroutine representing a task to be running in scheduler async def task(delay_ms): bot = get_robot(coro = True) lights = ((Lights.FRONT_1, 0), (Lights.FRONT_2, 1), (Lights.FRONT_3, 2), (Lights.FRONT_4, 3), (Lights.FRONT_5, 4)) colors = (ozobot.SurfaceColor.BLACK, ozobot.SurfaceColor.BLACK, ozobot.SurfaceColor.BLACK, ozobot.SurfaceColor.BLACK, ozobot.SurfaceColor.BLACK, ozobot.SurfaceColor.RED, ozobot.SurfaceColor.YELLOW, ozobot.SurfaceColor.GREEN, ozobot.SurfaceColor.BLUE, ozobot.SurfaceColor.MAGENTA) cnt = len(colors) for i in range(cnt * 4): for l in lights: await bot.light_effects.aset_light_color(colors[(l[1] + i) % cnt], l[0]) await asyncio.sleep_ms(delay_ms) await bot.light_effects.aset_light_color(ozobot.SurfaceColor.BLACK) # Here we get the coroutine itself and give it to scheduler for running my_task = task(300) asyncio.run(my_task) Indexes / Search **************** * :ref:`genindex` * :ref:`modindex` * :ref:`search`