Skip to content

Instantly share code, notes, and snippets.

@altendky
Forked from anonymous/-
Last active April 23, 2016 18:41
Show Gist options
  • Save altendky/b2e4ad619ec738b462d49f1b1082201b to your computer and use it in GitHub Desktop.
Save altendky/b2e4ad619ec738b462d49f1b1082201b to your computer and use it in GitHub Desktop.
diff --git a/canmatrix/canmatrix.py b/canmatrix/canmatrix.py
index 12b0eed..82eed9e 100644
--- a/canmatrix/canmatrix.py
+++ b/canmatrix/canmatrix.py
@@ -116,69 +116,47 @@ class Signal(object):
# def __init__(self, name, startbit, signalsize, is_little_endian, is_signed=False, factor=1, offset=0, min=0, max=0, unit="", receiver=[], multiplex=None):
def __init__(self, name, **kwargs):
- if 'startBit' in kwargs:
- self._startbit = int(kwargs["startBit"])
- else:
- self._startbit = 0
-
- if 'signalSize' in kwargs:
- self._signalsize = int(kwargs["signalSize"])
- else:
- self._signalsize = 0
-
- if 'is_little_endian' in kwargs:
- self._is_little_endian = kwargs["is_little_endian"]
- else:
- self._is_little_endian = True
-
- if 'is_signed' in kwargs:
- self._is_signed = kwargs["is_signed"]
- else:
- self._is_signed = True
-
- if 'factor' in kwargs:
- self._factor = float(kwargs["factor"])
- else:
- self._factor = float(1)
-
- if 'offset' in kwargs:
- self._offset = float(kwargs["offset"])
- else:
- self._offset = float(0)
-
- if 'min' in kwargs:
- self._min = float(kwargs["min"])
- else:
- self._min = float(0)
-
- if 'max' in kwargs:
- self._max = float(kwargs["max"])
- else:
- self._max = float(0)
-
- if 'unit' in kwargs:
- self._unit = kwargs["unit"]
- else:
- self._unit = ""
+ def multiplex(value):
+ if value is not None and value != 'Multiplexor':
+ multiplex = int(value)
+ else:
+ multiplex = value
+ return multiplex
+
+ args = [
+ ('startBit', '_startbit', int, 0),
+ ('signalSize', '_signalsize', int, 0),
+ ('is_little_endian', '_is_little_endian', bool, True),
+ ('is_signed', '_is_signed', bool, True),
+ ('factor', '_factor', float, 1),
+ ('offset', '_offset', float, 0),
+ ('min', '_min', float, 0),
+ ('max', '_max', float, 0),
+ ('unit', '_unit', str, ""),
+ ('receiver', '_receiver', None, []),
+ ('comment', '_comment', str, None),
+ ('multiplex', '_multiplex', multiplex, None)
+ ]
+
+ for arg_name, destination, function, default in args:
+ try:
+ value = kwargs[arg_name]
+ except KeyError:
+ value = default
+ else:
+ kwargs.pop(arg_name)
- if 'receiver' in kwargs:
- self._receiver = kwargs["receiver"]
- else:
- self._receiver = []
+ if function is not None:
+ value = function(value)
- if 'comment' in kwargs:
- self._comment = kwargs["comment"]
- else:
- self._comment = None
+ setattr(self, destination, value)
- if 'multiplex' in kwargs:
- if kwargs["multiplex"] is not None and kwargs["multiplex"] != 'Multiplexor':
- multiplex = int(kwargs["multiplex"])
- else:
- multiplex = kwargs["multiplex"]
- self._multiplex = multiplex
- else:
- self._multiplex = None
+ if len(kwargs) > 0:
+ raise TypeError('{}() got unexpected argument{} {}'.format(
+ self.__class__.__name__,
+ 's' if len(kwargs) > 1 else '',
+ ', '.join(kwargs.keys())
+ ))
self._name = name
self._attributes = {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment