Integrated proposed fixes for _decode_field.  I guess I should have put a bit more effort into evaluating the logic to not break existing tests.  Also integrated _encode_field, which I didn't touch previously.
@@ -37,41 +37,29 @@ def _encode_field(field, data, scaling):
value = data[field.name]
if isinstance(value, str):
- value = field.choice_string_to_number(value)
-
- if scaling:
+ 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):
-
- #disable this if block, and use "potential fix" to always use raw signal value for decoded field
- if scaling:
- value = (field.scale * value + field.offset)
-
- #decode choices applies to raw signal value
if decode_choices:
try:
- decoded_field = field.choices[value]
+ return field.choices[value]
except (KeyError, TypeError):
- decoded_field = value
- else:
-
- #potential fix to always use raw value
- #if scaling:
- # value = (field.scale * value + field.offset)
-
- decoded_field = value
+ pass
- return decoded_field
+ if scaling:
+ return (field.scale * value + field.offset)
+ else:
+ return value
def encode_data(data, fields, formats, scaling):
