bt2: make `_EventConst` a mapping
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 7 Apr 2020 00:22:16 +0000 (20:22 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 8 Apr 2020 17:01:12 +0000 (13:01 -0400)
commitf03b6364aec2d77bbb5ef0625cbaea8de4179f63
treeef52ed556e3440404bd5aba69555a18a0ca2211f
parent3e05e718fc5631fc24462032aea0e20ec7f090a3
bt2: make `_EventConst` a mapping

The `_EventConst` class already implemented the __getitem__() method
to make it easy to access a payload, specific context, common context,
or packet context field (in this order) by name, for example:

    print(event['next_prio'])

If two root fields contain fields which share the same name, one of them
has the priority and the other one is not accessible through this
interface.

This patch makes the `_EventConst` class a full mapping protocol,
inheriting `collections.abc.Mapping`. This patch implements the
__iter__() and __len__() methods so as to let `collections.abc.Mapping`
implement __contains__(), keys(), items(), values(), get(), __eq__(),
and __ne__(). Now you can do, for example:

    if 'next_prio' in event:
        print(event['next_prio'])

__iter__() keeps a set of yielded field names so as to avoid repeating
field names.

__len__() simply reuses __iter__().

`test_event.py` is updated to test the new features.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I2485e0df2373005fada0d0732c6269acbce5457c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/3343
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
src/bindings/python/bt2/bt2/event.py
tests/bindings/python/bt2/test_event.py
This page took 0.025484 seconds and 4 git commands to generate.