-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add method for getting the string value of error codes * feat: update logging to print string error codes * feat: update logging to print string error codes for gripper * rename get_enum_string -> enum_to_str and add type hints * formatting: make pre-commit happy * pre-commit: fix import order
- Loading branch information
Showing
3 changed files
with
33 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Type | ||
|
||
|
||
def enum_to_str(enum_class: Type, value: int) -> str: | ||
"""Converts a ROS2 enum value to its string name. | ||
Args: | ||
enum_class: The ROS2 message class containing the enum constants. | ||
value: The integer value of the enum. | ||
Returns: | ||
str: The name of the enum constant, or the value with "UNKNOWN NAME" if not found. | ||
""" | ||
mapping = {} | ||
# Iterate over all attributes in the enum class | ||
for attr_name in dir(enum_class): | ||
# Consider only uppercase attributes (common convention for enums) | ||
if attr_name.isupper(): | ||
attr_value = getattr(enum_class, attr_name) | ||
# Check if the attribute is an integer (enum values are typically int) | ||
if isinstance(attr_value, int): | ||
mapping[attr_value] = attr_name | ||
return mapping.get(value, f"{value} :UNKNOWN NAME") |