Skip to content

Commit

Permalink
rework all typed dict uses
Browse files Browse the repository at this point in the history
Optimization for returned TypedDict values
  • Loading branch information
mdabrowski1990 committed Oct 24, 2023
1 parent 1de1459 commit 657495a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 77 deletions.
18 changes: 9 additions & 9 deletions uds/can/addressing_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ def decode_packet_ai(cls,
"""
from_data_bytes = cls.decode_ai_data_bytes(addressing_format=addressing_format, ai_data_bytes=ai_data_bytes)
from_can_id = CanIdHandler.decode_can_id(addressing_format=addressing_format, can_id=can_id)
items_names = (AbstractCanAddressingInformation.ADDRESSING_TYPE_NAME,
AbstractCanAddressingInformation.TARGET_ADDRESS_NAME,
AbstractCanAddressingInformation.SOURCE_ADDRESS_NAME,
AbstractCanAddressingInformation.ADDRESS_EXTENSION_NAME)
return {name: from_data_bytes.get(name, None) or from_can_id.get(name, None)
for name in items_names} # type: ignore
return cls.DecodedAIParamsAlias(
addressing_type=from_can_id.get(AbstractCanAddressingInformation.ADDRESSING_TYPE_NAME, None), # type: ignore # noqa: E501
target_address=from_data_bytes.get(AbstractCanAddressingInformation.TARGET_ADDRESS_NAME, None) # type: ignore # noqa: E501
or from_can_id.get(AbstractCanAddressingInformation.TARGET_ADDRESS_NAME, None),
source_address=from_can_id.get(AbstractCanAddressingInformation.SOURCE_ADDRESS_NAME, None), # type: ignore
address_extension=from_data_bytes.get(AbstractCanAddressingInformation.ADDRESS_EXTENSION_NAME, None)) # type: ignore # noqa: E501

@classmethod
def decode_ai_data_bytes(cls,
Expand All @@ -166,12 +166,12 @@ def decode_ai_data_bytes(cls,
ai_data_bytes=ai_data_bytes)
if addressing_format in {CanAddressingFormat.NORMAL_11BIT_ADDRESSING,
CanAddressingFormat.NORMAL_FIXED_ADDRESSING}:
return {}
return cls.DataBytesAIParamsAlias()
if addressing_format == CanAddressingFormat.EXTENDED_ADDRESSING:
return {AbstractCanAddressingInformation.TARGET_ADDRESS_NAME: ai_data_bytes[0]} # type: ignore
return cls.DataBytesAIParamsAlias(target_address=ai_data_bytes[0])
if addressing_format in {CanAddressingFormat.MIXED_11BIT_ADDRESSING,
CanAddressingFormat.MIXED_29BIT_ADDRESSING}:
return {AbstractCanAddressingInformation.ADDRESS_EXTENSION_NAME: ai_data_bytes[0]} # type: ignore
return cls.DataBytesAIParamsAlias(address_extension=ai_data_bytes[0])
raise NotImplementedError(f"Missing implementation for: {addressing_format}")

@classmethod
Expand Down
13 changes: 6 additions & 7 deletions uds/can/extended_addressing_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ def validate_packet_ai(cls,
if not CanIdHandler.is_extended_addressed_can_id(can_id): # type: ignore
raise InconsistentArgumentsError(f"Provided value of CAN ID is not compatible with "
f"Extended Addressing Format. Actual value: {can_id}")
return PacketAIParamsAlias(
addressing_format=CanAddressingFormat.EXTENDED_ADDRESSING,
addressing_type=addressing_type,
can_id=can_id, # type: ignore
target_address=target_address,
source_address=source_address,
address_extension=address_extension)
return PacketAIParamsAlias(addressing_format=CanAddressingFormat.EXTENDED_ADDRESSING,
addressing_type=addressing_type,
can_id=can_id, # type: ignore
target_address=target_address,
source_address=source_address,
address_extension=address_extension)
34 changes: 15 additions & 19 deletions uds/can/frame_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def decode_can_id(cls, addressing_format: CanAddressingFormat, can_id: int) -> C
if addressing_format in (CanAddressingFormat.NORMAL_11BIT_ADDRESSING,
CanAddressingFormat.EXTENDED_ADDRESSING,
CanAddressingFormat.MIXED_11BIT_ADDRESSING):
return {cls.ADDRESSING_TYPE_NAME: None, # type: ignore
cls.TARGET_ADDRESS_NAME: None,
cls.SOURCE_ADDRESS_NAME: None} # no addressing information can be decoded
return cls.CanIdAIAlias(addressing_type=None,
target_address=None,
source_address=None) # no addressing information can be decoded
raise NotImplementedError(f"Unknown addressing format value was provided: {addressing_format}")

@classmethod
Expand All @@ -122,15 +122,13 @@ def decode_normal_fixed_addressed_can_id(cls, can_id: int) -> CanIdAIAlias:
source_address = can_id & 0xFF
can_id_offset = can_id & (~0xFFFF) # value with Target Address and Source Address information erased
if can_id_offset == cls.NORMAL_FIXED_PHYSICAL_ADDRESSING_OFFSET:
addressing_type = AddressingType(AddressingType.PHYSICAL)
return {cls.ADDRESSING_TYPE_NAME: addressing_type, # type: ignore
cls.TARGET_ADDRESS_NAME: target_address,
cls.SOURCE_ADDRESS_NAME: source_address}
return cls.CanIdAIAlias(addressing_type=AddressingType.PHYSICAL,
target_address=target_address,
source_address=source_address)
if can_id_offset == cls.NORMAL_FIXED_FUNCTIONAL_ADDRESSING_OFFSET:
addressing_type = AddressingType(AddressingType.FUNCTIONAL)
return {cls.ADDRESSING_TYPE_NAME: addressing_type, # type: ignore
cls.TARGET_ADDRESS_NAME: target_address,
cls.SOURCE_ADDRESS_NAME: source_address}
return cls.CanIdAIAlias(addressing_type=AddressingType.FUNCTIONAL,
target_address=target_address,
source_address=source_address)
raise NotImplementedError("CAN ID in Normal Fixed Addressing format was provided, but cannot be handled."
f"Actual value: {can_id}")

Expand All @@ -156,15 +154,13 @@ def decode_mixed_addressed_29bit_can_id(cls, can_id: int) -> CanIdAIAlias:
source_address = can_id & 0xFF
can_id_offset = can_id & (~0xFFFF) # value with Target Address and Source Address information erased
if can_id_offset == cls.MIXED_29BIT_PHYSICAL_ADDRESSING_OFFSET:
addressing_type = AddressingType(AddressingType.PHYSICAL)
return {cls.ADDRESSING_TYPE_NAME: addressing_type, # type: ignore
cls.TARGET_ADDRESS_NAME: target_address,
cls.SOURCE_ADDRESS_NAME: source_address}
return cls.CanIdAIAlias(addressing_type=AddressingType.PHYSICAL,
target_address=target_address,
source_address=source_address)
if can_id_offset == cls.MIXED_29BIT_FUNCTIONAL_ADDRESSING_OFFSET:
addressing_type = AddressingType(AddressingType.FUNCTIONAL)
return {cls.ADDRESSING_TYPE_NAME: addressing_type, # type: ignore
cls.TARGET_ADDRESS_NAME: target_address,
cls.SOURCE_ADDRESS_NAME: source_address}
return cls.CanIdAIAlias(addressing_type=AddressingType.FUNCTIONAL,
target_address=target_address,
source_address=source_address)
raise NotImplementedError("CAN ID in Normal Fixed Addressing format was provided, but cannot be handled."
f"Actual value: {can_id}")

Expand Down
39 changes: 18 additions & 21 deletions uds/can/mixed_addressing_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ def validate_packet_ai(cls,
if not CanIdHandler.is_mixed_11bit_addressed_can_id(can_id): # type: ignore
raise InconsistentArgumentsError(f"Provided value of CAN ID is not compatible with "
f"Mixed 11-bit Addressing Format. Actual value: {can_id}")
return PacketAIParamsAlias(
addressing_format=CanAddressingFormat.MIXED_11BIT_ADDRESSING,
addressing_type=addressing_type,
can_id=can_id, # type: ignore
target_address=target_address,
source_address=source_address,
address_extension=address_extension)
return PacketAIParamsAlias(addressing_format=CanAddressingFormat.MIXED_11BIT_ADDRESSING,
addressing_type=addressing_type,
can_id=can_id, # type: ignore
target_address=target_address,
source_address=source_address,
address_extension=address_extension)


class Mixed29BitCanAddressingInformation(AbstractCanAddressingInformation):
Expand Down Expand Up @@ -107,13 +106,12 @@ def validate_packet_ai(cls,
addressing_type=addressing_type,
target_address=target_address, # type: ignore
source_address=source_address) # type: ignore
return PacketAIParamsAlias(
addressing_format=CanAddressingFormat.MIXED_29BIT_ADDRESSING,
addressing_type=addressing_type,
can_id=encoded_can_id,
target_address=target_address,
source_address=source_address,
address_extension=address_extension)
return PacketAIParamsAlias(addressing_format=CanAddressingFormat.MIXED_29BIT_ADDRESSING,
addressing_type=addressing_type,
can_id=encoded_can_id,
target_address=target_address,
source_address=source_address,
address_extension=address_extension)
decoded_info = CanIdHandler.decode_mixed_addressed_29bit_can_id(can_id)
if addressing_type != decoded_info[CanIdHandler.ADDRESSING_TYPE_NAME]: # type: ignore
raise InconsistentArgumentsError(f"Provided value of CAN ID is not compatible with Addressing Type."
Expand All @@ -124,10 +122,9 @@ def validate_packet_ai(cls,
if source_address not in (decoded_info[CanIdHandler.SOURCE_ADDRESS_NAME], None): # type: ignore
raise InconsistentArgumentsError(f"Provided value of CAN ID is not compatible with Source Address."
f"Actual values: can_id={can_id}, source_address={source_address}")
return PacketAIParamsAlias(
addressing_format=CanAddressingFormat.MIXED_29BIT_ADDRESSING,
addressing_type=addressing_type,
can_id=can_id,
target_address=decoded_info[CanIdHandler.TARGET_ADDRESS_NAME], # type: ignore
source_address=decoded_info[CanIdHandler.SOURCE_ADDRESS_NAME], # type: ignore
address_extension=address_extension)
return PacketAIParamsAlias(addressing_format=CanAddressingFormat.MIXED_29BIT_ADDRESSING,
addressing_type=addressing_type,
can_id=can_id,
target_address=decoded_info[CanIdHandler.TARGET_ADDRESS_NAME], # type: ignore
source_address=decoded_info[CanIdHandler.SOURCE_ADDRESS_NAME], # type: ignore
address_extension=address_extension)
39 changes: 18 additions & 21 deletions uds/can/normal_addressing_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ def validate_packet_ai(cls,
if not CanIdHandler.is_normal_11bit_addressed_can_id(can_id): # type: ignore
raise InconsistentArgumentsError(f"Provided value of CAN ID is not compatible with "
f"Normal 11-bit Addressing Format. Actual value: {can_id}")
return PacketAIParamsAlias(
addressing_format=CanAddressingFormat.NORMAL_11BIT_ADDRESSING,
addressing_type=addressing_type,
can_id=can_id, # type: ignore
target_address=target_address,
source_address=source_address,
address_extension=address_extension)
return PacketAIParamsAlias(addressing_format=CanAddressingFormat.NORMAL_11BIT_ADDRESSING,
addressing_type=addressing_type,
can_id=can_id, # type: ignore
target_address=target_address,
source_address=source_address,
address_extension=address_extension)


class NormalFixedCanAddressingInformation(AbstractCanAddressingInformation):
Expand Down Expand Up @@ -111,13 +110,12 @@ def validate_packet_ai(cls,
addressing_type=addressing_type,
target_address=target_address, # type: ignore
source_address=source_address) # type: ignore
return PacketAIParamsAlias(
addressing_format=CanAddressingFormat.NORMAL_FIXED_ADDRESSING,
addressing_type=addressing_type,
can_id=encoded_can_id,
target_address=target_address,
source_address=source_address,
address_extension=address_extension)
return PacketAIParamsAlias(addressing_format=CanAddressingFormat.NORMAL_FIXED_ADDRESSING,
addressing_type=addressing_type,
can_id=encoded_can_id,
target_address=target_address,
source_address=source_address,
address_extension=address_extension)
decoded_info = CanIdHandler.decode_normal_fixed_addressed_can_id(can_id)
if addressing_type != decoded_info[CanIdHandler.ADDRESSING_TYPE_NAME]: # type: ignore
raise InconsistentArgumentsError(f"Provided value of CAN ID is not compatible with Addressing Type."
Expand All @@ -128,10 +126,9 @@ def validate_packet_ai(cls,
if source_address not in (decoded_info[CanIdHandler.SOURCE_ADDRESS_NAME], None): # type: ignore
raise InconsistentArgumentsError(f"Provided value of CAN ID is not compatible with Source Address."
f"Actual values: can_id={can_id}, source_address={source_address}")
return PacketAIParamsAlias(
addressing_format=CanAddressingFormat.NORMAL_FIXED_ADDRESSING,
addressing_type=addressing_type,
can_id=can_id,
target_address=decoded_info[CanIdHandler.TARGET_ADDRESS_NAME], # type: ignore
source_address=decoded_info[CanIdHandler.SOURCE_ADDRESS_NAME], # type: ignore
address_extension=address_extension)
return PacketAIParamsAlias(addressing_format=CanAddressingFormat.NORMAL_FIXED_ADDRESSING,
addressing_type=addressing_type,
can_id=can_id,
target_address=decoded_info[CanIdHandler.TARGET_ADDRESS_NAME], # type: ignore
source_address=decoded_info[CanIdHandler.SOURCE_ADDRESS_NAME], # type: ignore
address_extension=address_extension)

0 comments on commit 657495a

Please sign in to comment.