Skip to content

Commit

Permalink
better __eq__ impls
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed Dec 22, 2023
1 parent 0c7c42a commit 7d804a9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Fixed

- Metadata PDU typing correction.
- More robust `__eq__` implementations which check the type compared against.

# [v0.21.0] 2023-11-10

Expand Down
20 changes: 12 additions & 8 deletions spacepackets/ccsds/spacepacket.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,10 @@ def __repr__(self):
f" sec_header_flag={self.sec_header_flag!r}, seq_flags={self.seq_flags!r})"
)

def __eq__(self, other: SpacePacketHeader):
return self.pack() == other.pack()
def __eq__(self, other: object):
if isinstance(other, SpacePacketHeader):
return self.pack() == other.pack()
return False


class SpacePacket:
Expand Down Expand Up @@ -404,12 +406,14 @@ def seq_count(self):
def sec_header_flag(self):
return self.sp_header.sec_header_flag

def __eq__(self, other: SpacePacket):
return (
self.sp_header == other.sp_header
and self.sec_header == other.sec_header
and self.user_data == other.user_data
)
def __eq__(self, other: object):
if isinstance(other, SpacePacket):
return (
self.sp_header == other.sp_header
and self.sec_header == other.sec_header
and self.user_data == other.user_data
)
return False


def get_space_packet_id_bytes(
Expand Down
22 changes: 13 additions & 9 deletions spacepackets/ecss/tc.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ def __repr__(self):
f" subservice={self.subservice!r}, ack_flags={self.ack_flags!r} "
)

def __eq__(self, other: PusTcDataFieldHeader):
return self.pack() == other.pack()
def __eq__(self, other: object):
if isinstance(other, PusTcDataFieldHeader):
return self.pack() == other.pack()
return False

@classmethod
def get_header_size(cls):
Expand Down Expand Up @@ -232,19 +234,21 @@ def __str__(self):
f" {self.apid:#05x}, SSC {self.sp_header.seq_count}"
)

def __eq__(self, other: PusTelecommand):
return (
self.sp_header == other.sp_header
and self.pus_tc_sec_header == other.pus_tc_sec_header
and self._app_data == other._app_data
)
def __eq__(self, other: object):
if isinstance(other, PusTelecommand):
return (
self.sp_header == other.sp_header
and self.pus_tc_sec_header == other.pus_tc_sec_header
and self._app_data == other._app_data
)
return False

def to_space_packet(self) -> SpacePacket:
"""Retrieve the generic CCSDS space packet representation. This also calculates the CRC16
before converting the PUS TC to a generic Space Packet"""
self.calc_crc()
user_data = bytearray(self._app_data)
user_data.extend(self._crc16)
user_data.extend(self._crc16) # type: ignore
return SpacePacket(self.sp_header, self.pus_tc_sec_header.pack(), user_data)

def calc_crc(self):
Expand Down
22 changes: 13 additions & 9 deletions spacepackets/ecss/tm.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ def __repr__(self):
f" pus_version={self.pus_version!r})"
)

def __eq__(self, other: PusTmSecondaryHeader):
return self.pack() == other.pack()
def __eq__(self, other: object):
if isinstance(other, PusTmSecondaryHeader):
return self.pack() == other.pack()
return False

@property
def header_size(self) -> int:
Expand Down Expand Up @@ -378,7 +380,7 @@ def to_space_packet(self) -> SpacePacket:
before converting the PUS TC to a generic Space Packet"""
self.calc_crc()
user_data = bytearray(self._source_data)
user_data.extend(self.crc16)
user_data.extend(self.crc16) # type: ignore
return SpacePacket(
self.space_packet_header, self.pus_tm_sec_header.pack(), user_data
)
Expand All @@ -397,12 +399,14 @@ def __repr__(self):
f" sec_header={self.pus_tm_sec_header!r}, tm_data={self.tm_data!r}"
)

def __eq__(self, other: PusTelemetry):
return (
self.space_packet_header == other.space_packet_header
and self.pus_tm_sec_header == other.pus_tm_sec_header
and self._source_data == other._source_data
)
def __eq__(self, other: object):
if isinstance(other, PusTelemetry):
return (
self.space_packet_header == other.space_packet_header
and self.pus_tm_sec_header == other.pus_tm_sec_header
and self._source_data == other._source_data
)
return False

@property
def packet_seq_control(self) -> PacketSeqCtrl:
Expand Down

0 comments on commit 7d804a9

Please sign in to comment.