Code Monkey home page Code Monkey logo

Comments (3)

eerimoq avatar eerimoq commented on June 1, 2024

Hello,

this looks like a bug that should be fixed. I suggest the implementation below, as it also scales values if the value has no label. Would be great if you can submit a PR with the fix.

Looks like DBC has the same bug, so that will be fixes as well.

Thanks.

def _encode_field(field, data, scaling):
    value = data[field.name]

    if isinstance(value, str):
        return field.choice_string_to_number(value)
    elif scaling:
        if field.is_float:
            return (value - field.offset) / field.scale
        else:
            value = (Decimal(value) - Decimal(field.offset)) / Decimal(field.scale)

            return value.to_integral()
    else:
        return value

def _decode_field(field, value, decode_choices, scaling):
    if decode_choices:
        try:
            return field.choices[value]
        except (KeyError, TypeError):
            pass

    if scaling:
        return (field.scale * value + field.offset)
    else:
        return value

from cantools.

richnetdesign avatar richnetdesign commented on June 1, 2024

I appreciate the suggested implementation provided for the code, I was a bit too sleepy to fully think through the fix at the time. I'm glad to see all the tests are passing now.

In regards to a PR, I assume you want me to clean up my test to match the style of your code? I will be sure to submit later today.

Thanks.

from cantools.

eerimoq avatar eerimoq commented on June 1, 2024

Yes, move the KCD definitions to a file in the tests/files folder.

from cantools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.