Skip to content

Commit

Permalink
Make the message __repr__ for python look nicer.
Browse files Browse the repository at this point in the history
Before this patch, publishing a message with "ros2 topic pub"
would print something like:

publishing #5: my_msgs.msg.my_msg(axes=array('f', [0.0]))

While that is OK, it is kind of ugly.  This patch hides the
typecode and the "array" so that the output looks like:

publishing #5: my_msgs.msg.my_msg(axes=[0.0])

Which is also how it looked in Crystal.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
  • Loading branch information
clalancette committed May 30, 2019
1 parent a29f80f commit ce9a5ea
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions rosidl_generator_py/resource/_msg.py.em
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,18 @@ if isinstance(type_, AbstractNestedType):
typename = self.__class__.__module__.split('.')
typename.pop()
typename.append(self.__class__.__name__)
@[if 'import array' in imports]@
args = []
for s in self.__slots__:
field = getattr(self, s, None)
if type(field) == array.array:
t = repr(field.tolist())
else:
t = repr(field)
args.append(s[1:] + '=' + t)
@[else]@
args = [s[1:] + '=' + repr(getattr(self, s, None)) for s in self.__slots__]
@[end if]@
return '%s(%s)' % ('.'.join(typename), ', '.join(args))

def __eq__(self, other):
Expand Down

0 comments on commit ce9a5ea

Please sign in to comment.