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

Require keyword arguments be passed by name #80

Merged
merged 2 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions rosidl_runtime_py/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 rosidl_runtime_py/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