File: frozen.rst

package info (click to toggle)
python-mido 1.3.3-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 920 kB
  • sloc: python: 4,006; makefile: 127; sh: 4
file content (58 lines) | stat: -rw-r--r-- 1,418 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
.. SPDX-FileCopyrightText: 2017 Ole Martin Bjorndalen <ombdalen@gmail.com>
..
.. SPDX-License-Identifier: CC-BY-4.0

Frozen Messages
---------------

.. versionadded:: 1.2

Since Mido messages are *mutable* (can change) they can not be hashed or
put in dictionaries. This makes it hard to use them for things like
Markov chains.

In these situations you can use *frozen messages*:

.. code-block:: python

    from mido.frozen import FrozenMessage

    msg = FrozenMessage('note_on')
    d = {msg: 'interesting'}

*Frozen messages* are used and behave in exactly the same way as normal
messages with one exception: **attributes are not settable**.

There are also variants for meta messages (``FrozenMetaMessage`` and
``FrozenUnknownMetaMessage``).

You can *freeze* and *thaw* messages with:

.. code-block:: python

    from mido.frozen import freeze_message, thaw_message

    frozen = freeze_message(msg)
    thawed = thaw_message(frozen)

``thaw_message()`` will always return a *copy*. Passing a *frozen message*
to ``freeze_message()`` will return the original message.

Both functions return ``None`` if you pass ``None`` which is handy for
things like:

.. code-block:: python

    msg = freeze_message(port.receive())

    for msg in map(freeze_message, port):
        ...

To check if a message is *frozen*:

.. code-block:: python

    from mido.frozen import is_frozen

    if is_frozen(msg):
        ...