Skip to content

Commit

Permalink
Require keyword arguments be passed by name (ros2/rosidl_python#80)
Browse files Browse the repository at this point in the history
* Require keyword arguments be passed by name

Also fix style by breaking after open parenthesis.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* Update style

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
  • Loading branch information
jacobperron committed Sep 19, 2019
1 parent 73a4044 commit e9be333
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
42 changes: 30 additions & 12 deletions rosidl_runtime_py/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ def __represent_ordereddict(dumper, data):
return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', items)


def message_to_yaml(msg: Any, truncate_length: int = None,
no_arr: bool = False, no_str: bool = False) -> str:
def message_to_yaml(
msg: Any,
*,
truncate_length: int = None,
no_arr: bool = False,
no_str: bool = False
) -> str:
"""
Convert a ROS message to a YAML string.
Expand All @@ -85,8 +90,13 @@ def message_to_yaml(msg: Any, truncate_length: int = None,
)


def message_to_csv(msg: Any, truncate_length: int = None,
no_arr: bool = False, no_str: bool = False) -> str:
def message_to_csv(
msg: Any,
*,
truncate_length: int = None,
no_arr: bool = False,
no_str: bool = False
) -> str:
"""
Convert a ROS message to string of comma-separated values.
Expand Down Expand Up @@ -123,7 +133,7 @@ def to_string(val, field_type=None):
val += '...'
r = str(val)
else:
r = message_to_csv(val, truncate_length, no_arr, no_str)
r = message_to_csv(val, truncate_length=truncate_length, no_arr=no_arr, no_str=no_str)
return r
result = ''

Expand All @@ -141,10 +151,12 @@ def to_string(val, field_type=None):
# Convert a msg to an OrderedDict. We do this instead of implementing a generic __dict__() method
# in the msg because we want to preserve order of fields from the .msg file(s).
def message_to_ordereddict(
msg: Any,
truncate_length: int = None,
no_arr: bool = False,
no_str: bool = False) -> OrderedDict:
msg: Any,
*,
truncate_length: int = None,
no_arr: bool = False,
no_str: bool = False
) -> OrderedDict:
"""
Convert a ROS message to an OrderedDict.
Expand All @@ -163,15 +175,21 @@ def message_to_ordereddict(
value = getattr(msg, field_name, None)

value = _convert_value(
value, field_type,
value, field_type=field_type,
truncate_length=truncate_length, no_arr=no_arr, no_str=no_str)
# Remove leading underscore from field name
d[field_name[1:]] = value
return d


def _convert_value(value, field_type=None,
truncate_length=None, no_arr=False, no_str=False):
def _convert_value(
value,
*,
field_type=None,
truncate_length=None,
no_arr=False,
no_str=False
):

if isinstance(value, bytes):
if truncate_length is not None and len(value) > truncate_length:
Expand Down
12 changes: 6 additions & 6 deletions test/rosidl_runtime_py/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def test_primitives():
msgs.extend(message_fixtures.get_msg_strings())
msgs.extend(message_fixtures.get_msg_unbounded_sequences())
for m in msgs:
message_to_csv(m, 100)
message_to_csv(m, None)
message_to_ordereddict(m, 100)
message_to_ordereddict(m, None)
message_to_yaml(m, 100)
message_to_yaml(m, None)
message_to_csv(m, truncate_length=100)
message_to_csv(m, truncate_length=None)
message_to_ordereddict(m, truncate_length=100)
message_to_ordereddict(m, truncate_length=None)
message_to_yaml(m, truncate_length=100)
message_to_yaml(m, truncate_length=None)


def test_convert_primitives():
Expand Down

0 comments on commit e9be333

Please sign in to comment.