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

dbc: dump_string: set GenSigStartValue from Signal.raw_initial #569

Merged

Conversation

malsyned
Copy link
Contributor

@malsyned malsyned commented May 24, 2023

I modeled this after the way GenMsgCycleTime is synchronized with Message.cycle_time.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 5064782045

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 16 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.002%) to 93.856%

Files with Coverage Reduction New Missed Lines %
cantools/database/can/formats/dbc.py 16 98.22%
Totals Coverage Status
Change from base Build 5027870252: -0.002%
Covered Lines: 7241
Relevant Lines: 7715

💛 - Coveralls

Copy link
Member

@andlaus andlaus left a comment

Choose a reason for hiding this comment

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

LGTM. @juleq , @zariiii9003 ?

# synchronize the attribute for the signal start value with
# the start value specified by the message object
if signal.raw_initial is None and 'GenSigStartValue' in sig_attributes:
del sig_attributes['GenSigStartValue']
Copy link
Collaborator

Choose a reason for hiding this comment

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

You are modifying signal.dbc.attributes here, because you reassigned sig_attributes on line 802. You should call sig_attributes.update(signal.dbc.attributes) or create a deep copy of signal.dbc.attributes.

Copy link
Contributor Author

@malsyned malsyned May 24, 2023

Choose a reason for hiding this comment

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

I wondered about that, but if that's a bug, it also exists the way message.dbc.attributes['GenMsgCycleTime'] is handled in the current code right above my additions:

    for message in database.messages:
        # retrieve the ordered dictionary of message attributes
        msg_attributes = OrderedDict()
        if message.dbc is not None and message.dbc.attributes is not None:
            msg_attributes = message.dbc.attributes

        # synchronize the attribute for the message cycle time with
        # the cycle time specified by the message object
        if message.cycle_time is None and 'GenMsgCycleTime' in msg_attributes:
            del msg_attributes['GenMsgCycleTime']

That was originally added in fcb2bcb. @andlaus can you comment on what your thinking was there? It looks to me like you were taking the opportunity at DBC write time to synchronize the Message DbcSpecifics with any changes made to the Message.cycle_time value. Since what I'm doing is an analogous operation on Signals, I used the same pattern.

If you still think that's wrong, though, @zariiii9003, I can fix it. Would you want it fixed for message.dbc.attributes['GenMsgCycleTime'] as well?

Copy link
Member

Choose a reason for hiding this comment

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

That was originally added in fcb2bcb. @andlaus can you comment on what your thinking was there? It looks to me like you were taking the opportunity at DBC write time to synchronize the Message DbcSpecifics with any changes made to the Message.cycle_time value.

I don't remember exactly, but I think that was the reason why. I'm pretty sure this can be implemented more elegantly, though.

Since what I'm doing is an analogous operation on Signals, I used the same pattern.

fine with me ;). if I understood @zariiii9003 correctly, he just wants to have this done in a more pythonic fashion?

Copy link
Member

Choose a reason for hiding this comment

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

You are modifying signal.dbc.attributes here, because you reassigned sig_attributes on line 802. You should call sig_attributes.update(signal.dbc.attributes) or create a deep copy of signal.dbc.attributes.

NB that the whole database is already deepcopied before it is written: https://github.com/cantools/cantools/blob/master/cantools/database/can/formats/dbc.py#L1862

Copy link
Collaborator

Choose a reason for hiding this comment

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

In my opinion, dumping the database should have no side effects. The database should remain unchanged.

Copy link
Member

Choose a reason for hiding this comment

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

In my opinion, dumping the database should have no side effects. The database should remain unchanged.

agreed. that's why the database object is deepcopied before doing the modifications. (It's a bit hacky but it has been like this since at least 2020 (cf 89b974cb3b0921a057bff352afe84fa844e23a8d)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that the deepcopy already happens at the top (I'd missed that -- thanks @andlaus) I think your request is already met by this PR in its current form, do you agree @zariiii9003? Does it need anything else before you'd want to merge it?

Copy link
Collaborator

Choose a reason for hiding this comment

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

In my opinion, dumping the database should have no side effects. The database should remain unchanged.

agreed. that's why the database object is deepcopied before doing the modifications. (It's a bit hacky but it has been like this since at least 2020 (cf 89b974cb3b0921a057bff352afe84fa844e23a8d)

I see. That module needs some refactoring 😄

Copy link
Member

Choose a reason for hiding this comment

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

agreed. it is far from the only one, though ;)

@andlaus andlaus merged commit c053be1 into cantools:master May 27, 2023
13 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.

None yet

4 participants