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

extraction based on attributes #428

Closed
yundddd opened this issue Nov 7, 2019 · 6 comments
Closed

extraction based on attributes #428

yundddd opened this issue Nov 7, 2019 · 6 comments

Comments

@yundddd
Copy link

yundddd commented Nov 7, 2019

Is there anyway to filter/extract frames based on attributes?
python -m canmatrix.cli.convert --attr=my_attribute_name:attr_value source.dbc filtered.dbc

@yundddd
Copy link
Author

yundddd commented Nov 8, 2019

looks like a simple function addition for:
def frame_by_attribute(self, attr)
and then
def copy_frame(attr, source_db, target_db):
would do. Not sure how you like to handle the code duplication though.

@ebroecker
Copy link
Owner

ebroecker commented Nov 8, 2019

maybe meanwhile you can do some script yourself:

import canmatrix.formats
import canmatrix.copy

target = canmatrix.CanMatrix()
cm = canmatrix.formats.loadp_flat("some.dbc")
copy_frames = [f for f in cm.frames if f.attributes.get("YOURATTRIBUTE", "") == "YOURVALUE"]

for frame in copy_frames:
  canmatrix.copy.copy_frame(cm, target)

canmatrix.formats.dump({"":target},"out.dbc")

Not Tested !

@yundddd
Copy link
Author

yundddd commented Nov 8, 2019

thanks for the suggestion.

It looks like attribute parsing has some issue with a white space in front of ';' add attributes.get() will not find it.

Also, if I remove the white space, the string attribute got appended with an extra " at the end.

test case:

The following will fail the parsing
BA_ "Bus" BO_ 512 "BDY2" ;
BA_ "Bus" BO_ 513 "BDY2" ;

The following
BA_ "Bus" BO_ 512 "BDY2";
BA_ "Bus" BO_ 513 "BDY2";
turns into
BA_ "Bus" BO_ 512 "BDY2"";
BA_ "Bus" BO_ 513 "BDY2"";

@ebroecker
Copy link
Owner

thanks for this issue, gonna have a look ...

@yundddd
Copy link
Author

yundddd commented Nov 8, 2019

I confirm that the white space issue has been fixed by 961918a on branch iss427.

@ebroecker
Copy link
Owner

my code was not handling default values, so here's an update:

import canmatrix.formats
import canmatrix.copy

target = canmatrix.CanMatrix()
cm = canmatrix.formats.loadp_flat("some.dbc")
copy_frames = [f for f in cm.frames if f.attribute("YOURATTRIBUTE", cm) == "YOURVALUE"]

for frame in copy_frames:
  canmatrix.copy.copy_frame(cm, target)

canmatrix.formats.dump({"":target},"out.dbc")

use frame.attribute("YOURATTRIBUTE", matrix) to get the atrribute value or the default.
https://canmatrix.readthedocs.io/en/latest/api.html#canmatrix.canmatrix.Frame.attribute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants