From 0a3b1069ccb892291669bafb1b88c40391809e3d Mon Sep 17 00:00:00 2001 From: Silcantar <1491897+Silcantar@users.noreply.github.com> Date: Tue, 22 Sep 2026 11:51:06 -0500 Subject: [PATCH] - Add __iter__ method to AliasEnum. - Refactor get_data_length. --- src/AuDaGe/iec.py | 18 ++++++++++++------ tests/test_iec.py | 4 +++- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/AuDaGe/iec.py b/src/AuDaGe/iec.py index 0b68724..f75ac1f 100644 --- a/src/AuDaGe/iec.py +++ b/src/AuDaGe/iec.py @@ -141,6 +141,9 @@ class AliasEnumType(EnumType): """ return member in cls.__members__ + def __iter__(cls): + return cls.__members__ + # Disable Ruff warnings about duplicate values in this enum. # ruff: disable[PIE796] @@ -179,6 +182,7 @@ class IECTypes(Enum, metaclass=AliasEnumType): CHAR = IECType(8) WCHAR = IECType(16) + # ruff: enable[PIE796] @@ -358,14 +362,16 @@ def get_data_length( return IECTypes[data_type].value.get_length(unit) except KeyError: pass - try: - match = data_type.strip()[0:3].lower() - if match == "arr": + if data_type.lower().startswith("arr"): + try: return IECArray(data_type).get_length(unit) - if match == "str": + except ValueError: + pass + if data_type.lower().startswith("str"): + try: return IECString(data_type).get_length(unit) - except ValueError: - pass + except ValueError: + pass try: return udt_lengths[data_type] * DataUnit.WORD // unit except KeyError: diff --git a/tests/test_iec.py b/tests/test_iec.py index a1c5f98..ac2f38a 100644 --- a/tests/test_iec.py +++ b/tests/test_iec.py @@ -29,7 +29,9 @@ class TestIEC(unittest.TestCase): ("UNKNOWN_UDT", DataUnit.WORD, -1), ] for data_type in data_types: - self.assertEqual(get_data_length(data_type[0], data_type[1]), data_type[2]) + self.assertEqual( + get_data_length(data_type[0], data_type[1]), data_type[2], data_type + ) def test_iec_definitions(self): definitions = iec_definitions()