From 8bd305f176b41d5844825f21959e5e6a5517b243 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 9 Sep 2024 20:06:40 +0200 Subject: [PATCH] Entity ID TLV eq custom method and AbstractTlvBase __repr__ --- CHANGELOG.md | 11 +++++++++++ spacepackets/cfdp/tlv/base.py | 7 ++++++- spacepackets/cfdp/tlv/tlv.py | 1 + tests/cfdp/tlvslvs/test_entity_id.py | 11 +++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b8ada9..0e2d433 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/). # [unreleased] +# [v0.24.2] 2024-09-09 + +## Fixed + +- Custom `EntityIdTlv` `__eq__` implementation which only compares the numerical value + of the entity ID TLVs + +## Added + +- `AbstractTlvBase` `__repr__` implementation + # [v0.24.1] 2024-04-23 ## Reverted diff --git a/spacepackets/cfdp/tlv/base.py b/spacepackets/cfdp/tlv/base.py index dd229d9..504cf2d 100644 --- a/spacepackets/cfdp/tlv/base.py +++ b/spacepackets/cfdp/tlv/base.py @@ -25,7 +25,12 @@ def tlv_type(self) -> TlvType: def value(self) -> bytes: pass - def __eq__(self, other: AbstractTlvBase): + def __repr__(self) -> str: + return f"Tlv(tlv_type={self.tlv_type!r}, value=0x[{self.value.hex(sep=',')}])" + + def __eq__(self, other: object): + if not isinstance(other, AbstractTlvBase): + return False return self.tlv_type == other.tlv_type and self.value == other.value def check_type(self, tlv_type: TlvType): diff --git a/spacepackets/cfdp/tlv/tlv.py b/spacepackets/cfdp/tlv/tlv.py index bfc3a20..601d2a2 100644 --- a/spacepackets/cfdp/tlv/tlv.py +++ b/spacepackets/cfdp/tlv/tlv.py @@ -505,6 +505,7 @@ def _set_fields(cls, instance: FileStoreResponseTlv, data: bytes): class EntityIdTlv(AbstractTlvBase): """This helper class has a :py:meth:`__eq__` implementation which only compares the numerical value of the entity IDs""" + TLV_TYPE = TlvType.ENTITY_ID def __init__(self, entity_id: bytes): diff --git a/tests/cfdp/tlvslvs/test_entity_id.py b/tests/cfdp/tlvslvs/test_entity_id.py index 1d4df1d..2a4405e 100644 --- a/tests/cfdp/tlvslvs/test_entity_id.py +++ b/tests/cfdp/tlvslvs/test_entity_id.py @@ -41,3 +41,14 @@ def test_invalid_type(self): entity_id_tlv_tlv.tlv_type = TlvType.FILESTORE_REQUEST with self.assertRaises(TlvTypeMissmatch): EntityIdTlv.from_tlv(cfdp_tlv=entity_id_tlv_tlv) + + def test_custom_eq(self): + self.entity_id_tlv = EntityIdTlv(entity_id=bytes([0x00, 0x01])) + self.entity_id_tlv_same = EntityIdTlv(entity_id=bytes([0x01])) + self.assertEqual(self.entity_id_tlv, self.entity_id_tlv_same) + + def test_repr(self): + repr_str = repr(self.entity_id_tlv) + self.assertEqual( + repr_str, f"Tlv(tlv_type={TlvType.ENTITY_ID!r}, value=0x[00,01,02,03])" + )