Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor decoding, part 4 #267

Merged
merged 5 commits into from
Feb 16, 2024
Merged

Conversation

andlaus
Copy link
Collaborator

@andlaus andlaus commented Feb 15, 2024

This PR is the final part of the decode code refactoring miniseries. It contains the "leftovers" which I've encountered and fixed while implementing the rest of the series and which require the API changes of the rest of the series to be present.

Andreas Lauser <[email protected]>, on behalf of MBition GmbH.
Provider Information

@andlaus andlaus requested a review from kayoub5 February 15, 2024 15:49
for whatever reason, mypy started to complain recently. Maybe this has
something to do with the recent switch to `InquirerPy`...

Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Florian Jost <[email protected]>
i.e., if they are non-byte aligned their value will no longer feature
garbage at the beginning and/or at the end. Besides this, their values
are now integers, which is more intuitive IMO.

Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Florian Jost <[email protected]>
Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Florian Jost <[email protected]>
…_value()`

Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Florian Jost <[email protected]>
@@ -99,7 +99,7 @@ def prompt_single_parameter_value(parameter: Parameter) -> Optional[AtomicOdxTyp
return None
elif parameter.physical_type.base_data_type is not None:
return _convert_string_to_odx_type(
answer.get(parameter.short_name), parameter.physical_type.base_data_type)
cast(str, answer.get(parameter.short_name)), parameter.physical_type.base_data_type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cast(str, answer.get(parameter.short_name)), parameter.physical_type.base_data_type)
str(answer.get(parameter.short_name)), parameter.physical_type.base_data_type)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure: without the cast mypy has the following complaint:

odxtools/cli/browse.py:102: error: Argument 1 to "_convert_string_to_odx_type" has incompatible type "str | bool | list[Any] | None"; expected "str"  [arg-type]

I guess it is better to make it shut up and deal with the exception if something goes wrong anyway instead of making it "work" unconditionally?

@@ -70,7 +70,7 @@ def is_highlow_byte_order(self) -> bool:
return self.is_highlow_byte_order_raw in [None, True]

@staticmethod
def _extract_internal_value(
def _extract_atomic_value(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted a different name: there are/were so many methods with "internal" in their name that I get confused quite easily. (Also, I think that the new value better conveys the fact that this method returns an AtomicOdxType object...)

byte_length = (8 * self.byte_length + bit_position + 7) // 8
result = decode_state.coded_message[byte_position:byte_position + byte_length]
decode_state.cursor_byte_position += byte_length
result, decode_state.cursor_byte_position = DiagCodedType._extract_atomic_value(
Copy link
Collaborator

@kayoub5 kayoub5 Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just me, or a function that takes decode_state directly would be easier to understand and re-use?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the lowest level method which IMO should be usable without a decode_state and even without a DiagCodedType object. (also, DCTs already have a decode_from_pdu() method...)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then why it is in the DiagCodedType class?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because usually (all occasions instead of this one), it is called from there.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(_extract_atomic_value() deals with retrieving a non-byte-aligned object from a blob of binary data, while decode_from_pdu() also deals with the "book keeping" of updating decode_state.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe put the function in the decode state itself?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, did that. Thanks for the suggestion...

thanks to [at]kayoub5 for the proposal.

Signed-off-by: Andreas Lauser <[email protected]>
Signed-off-by: Florian Jost <[email protected]>
@andlaus andlaus merged commit e613b32 into mercedes-benz:main Feb 16, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants