-
Notifications
You must be signed in to change notification settings - Fork 22
/
_dbc_signal_value_table.py
102 lines (80 loc) · 2.99 KB
/
_dbc_signal_value_table.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import collections
import six
import typing # NOQA: F401
from nixnet import _funcs
from nixnet import constants
class DbcSignalValueTable(collections.Mapping):
"""Collection for accessing a DBC signal value table."""
def __init__(self, handle):
# type: (int) -> None
self._handle = handle
def __repr__(self):
return '{}(handle={})'.format(type(self).__name__, self._handle)
def __eq__(self, other):
if isinstance(other, self.__class__):
return self._handle == typing.cast(DbcSignalValueTable, other)._handle
else:
return NotImplemented
def __ne__(self, other):
result = self.__eq__(other)
if result is NotImplemented:
return result
else:
return not result
def __hash__(self):
return hash(self._handle)
def __len__(self):
return len(self._value_table)
def __iter__(self):
return self.keys()
def __getitem__(self, key):
# type: (typing.Text) -> typing.Text
"""Return the value.
Args:
Value description.
Returns:
Value
"""
if isinstance(key, six.string_types):
return self._value_table[key]
else:
raise TypeError(key)
def keys(self):
"""Return all value descriptions in the collection.
Yields:
An iterator to all value descriptions in the collection.
"""
return iter(self._value_table.keys())
def values(self):
"""Return all values in the collection.
Yields:
An iterator to all values in the collection.
"""
return iter(self._value_table.values())
def items(self):
"""Return all value descriptions and values in the collection.
Yields:
An iterator to tuple pairs of value descriptions and values in the collection.
"""
return iter(self._value_table.items())
@property
def _value_table(self):
# type: () -> typing.Dict[typing.Text, typing.Text]
mode = constants.GetDbcAttributeMode.VALUE_TABLE_LIST
attribute_size = _funcs.nxdb_get_dbc_attribute_size(self._handle, mode, '')
attribute_info = _funcs.nxdb_get_dbc_attribute(self._handle, mode, '', attribute_size)
table_string = attribute_info[0]
if not table_string:
return {}
table_list = table_string.split(',')
if len(table_list) % 2:
raise ValueError('Value tables require an even number of items: %s' % table_list)
# table_string is of the format 'value1, key1, value2, key2, ...'
# convert to a dict: { 'key1': int('value1'), 'key2': int('value2'), ... }
table_dict = dict(
(key, value)
for value, key in zip(table_list[0::2], table_list[1::2]))
return table_dict