Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the message __repr__ for python look nicer. #60

Merged
merged 5 commits into from
May 31, 2019

Conversation

clalancette
Copy link
Contributor

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

Note that this is currently targeting the fix-array-publish branch; we'll need to retarget once that one is merged. Also note that this is kind of an RFC; while it is quite ugly, it is a cosmetic issue, and touching the generated python code may be too risky for Dashing at this point. I'll run CI presently, but opinions welcome.

@clalancette clalancette added the in review Waiting for review (Kanban column) label May 30, 2019
@@ -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]@
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using this runtime condition (which magically changes the logic for all fields as soon as an array field is added) use the types stored for each slot in SLOT_TYPES.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, good point. I've done this in 33e0ff6

for s in self.__slots__:
field = getattr(self, s, None)
if type(field) == array.array:
t = repr(field.tolist())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this operation is fairly expensive (in terms of memory usage) I would suggest to keep using repr(field) and then process the string to remove the array(typecode, ) part.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this in 079d988.

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>
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
@clalancette clalancette changed the base branch from fix-array-publish to master May 30, 2019 21:19
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
@clalancette
Copy link
Contributor Author

clalancette commented May 30, 2019

Since this touches all of the generated Python code, I'm going to do a full CI run on Linux: Build Status

clalancette and others added 2 commits May 30, 2019 21:51
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
Copy link
Member

@dirk-thomas dirk-thomas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of comments - both addressed in 8e901fa.

rosidl_generator_py/resource/_msg.py.em Outdated Show resolved Hide resolved
rosidl_generator_py/resource/_msg.py.em Outdated Show resolved Hide resolved
Copy link
Member

@jacobperron jacobperron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks okay to me. But when I publish certain messages with the master branch, I don't see the "array(" prefix, for example:

$ ros2 topic pub chatter action_msgs/msg/GoalStatusArray
publisher: beginning loop
publishing #1: action_msgs.msg.GoalStatusArray(status_list=[])

Why is it that only some messages are affected?

@jacobperron
Copy link
Member

I guess we may only see the array prefix with arrays of basic types.

@dirk-thomas
Copy link
Member

Why is it that only some messages are affected?

array.array is only used for numeric types: http://design.ros2.org/articles/idl_interface_definition.html#type-mapping

@dirk-thomas dirk-thomas merged commit f78c207 into master May 31, 2019
@delete-merged-branch delete-merged-branch bot deleted the fix-python-array-repr branch May 31, 2019 00:28
@clalancette clalancette added this to Needs Backport in Dashing Patch Release 1 Jun 12, 2019
clalancette added a commit that referenced this pull request Jun 12, 2019
* Make the message __repr__ for python look nicer.

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>

* Use string processing for arrays.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* Use the SLOT_TYPES for printing.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* Flake8 fix.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* some update

Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
clalancette added a commit that referenced this pull request Jun 12, 2019
* Make the message __repr__ for python look nicer.

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>

* Use string processing for arrays.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* Use the SLOT_TYPES for printing.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* Flake8 fix.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>

* some update

Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
@clalancette clalancette moved this from Needs Backport to Released in Dashing Patch Release 1 Jun 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in review Waiting for review (Kanban column)
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants