From 836562693cdd3af4ed861db7770daef040d4d920 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Sun, 26 Nov 2017 11:46:19 +0100 Subject: [PATCH] Implement __repr__ instead of __str__ for python Value MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Implement the __repr__ method for the various Value classes as it provides a fallback for __str__ and provides both a non-ambiguous and human-readable string representation of those objects. The Array and Map values' implementation is made more compact by using list comprehensions, as is done for the Fields API. Signed-off-by: Jérémie Galarneau --- bindings/python/bt2/bt2/values.py | 38 ++++++++++--------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/bindings/python/bt2/bt2/values.py b/bindings/python/bt2/bt2/values.py index 735aeed9..0db998d1 100644 --- a/bindings/python/bt2/bt2/values.py +++ b/bindings/python/bt2/bt2/values.py @@ -166,8 +166,8 @@ class _NumericValue(_Value, _BasicCopy): def __float__(self): return float(self._value) - def __str__(self): - return str(self._value) + def __repr__(self): + return repr(self._value) def __lt__(self, other): if not isinstance(other, numbers.Number): @@ -359,8 +359,8 @@ class BoolValue(_Value, _BasicCopy): def __bool__(self): return self._value - def __str__(self): - return str(self._value) + def __repr__(self): + return repr(self._value) def _value_to_bool(self, value): if isinstance(value, BoolValue): @@ -497,6 +497,9 @@ class StringValue(_BasicCopy, collections.abc.Sequence, _Value): def __bool__(self): return bool(self._value) + def __repr__(self): + repr(self._value) + def __str__(self): return self._value @@ -614,16 +617,8 @@ class ArrayValue(_Container, collections.abc.MutableSequence, _Value): return self - def __str__(self): - strings = [] - - for elem in self: - if isinstance(elem, StringValue): - strings.append(repr(elem._value)) - else: - strings.append(str(elem)) - - return '[{}]'.format(', '.join(strings)) + def __repr__(self): + return '[{}]'.format(', '.join([repr(v) for v in self])) def insert(self, value): raise NotImplementedError @@ -726,18 +721,9 @@ class MapValue(_Container, collections.abc.MutableMapping, _Value): status = native_bt.value_map_insert(self._ptr, key, ptr) self._handle_status(status) - def __str__(self): - strings = [] - - for key, elem in self.items(): - if isinstance(elem, StringValue): - value = repr(elem._value) - else: - value = str(elem) - - strings.append('{}: {}'.format(repr(key), value)) - - return '{{{}}}'.format(', '.join(strings)) + def __repr__(self): + items = ['{}: {}'.format(repr(k), repr(v)) for k, v in self.items()] + return '{{{}}}'.format(', '.join(items)) _TYPE_TO_OBJ = { -- 2.34.1