-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pretty_index for printing index and subindex
* Change print statements to use pretty_index() * Updates from code review
- Loading branch information
Showing
8 changed files
with
73 additions
and
27 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
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,22 @@ | ||
"""Additional utility functions for canopen.""" | ||
from typing import Optional, Union | ||
|
||
|
||
def pretty_index(index: Optional[Union[int, str]], | ||
sub: Optional[Union[int, str]] = None): | ||
"""Format an index and subindex as a string.""" | ||
|
||
index_str = "" | ||
if isinstance(index, int): | ||
index_str = f"0x{index:04X}" | ||
elif index: | ||
index_str = f"{index!r}" | ||
|
||
sub_str = "" | ||
if isinstance(sub, int): | ||
# Need 0x prefix if index is not present | ||
sub_str = f"{'0x' if not index_str else ''}{sub:02X}" | ||
elif sub: | ||
sub_str = f"{sub!r}" | ||
|
||
return ":".join(s for s in (index_str, sub_str) if s) |
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,20 @@ | ||
import unittest | ||
from canopen.utils import pretty_index | ||
|
||
|
||
class TestUtils(unittest.TestCase): | ||
|
||
def test_pretty_index(self): | ||
self.assertEqual(pretty_index(0x12ab), "0x12AB") | ||
self.assertEqual(pretty_index(0x12ab, 0xcd), "0x12AB:CD") | ||
self.assertEqual(pretty_index(0x12ab, ""), "0x12AB") | ||
self.assertEqual(pretty_index("test"), "'test'") | ||
self.assertEqual(pretty_index("test", 0xcd), "'test':CD") | ||
self.assertEqual(pretty_index(None), "") | ||
self.assertEqual(pretty_index(""), "") | ||
self.assertEqual(pretty_index("", ""), "") | ||
self.assertEqual(pretty_index(None, 0xab), "0xAB") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |