From 0746213c8c739fff27f38200dc811d8ef4ac43e8 Mon Sep 17 00:00:00 2001 From: Vlad Mihai Date: Tue, 2 Oct 2018 10:54:55 +0300 Subject: [PATCH 1/2] Issue#11 Changing asserts to raise errors --- adafruit_tcs34725.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/adafruit_tcs34725.py b/adafruit_tcs34725.py index daf12d4..f92c3a4 100644 --- a/adafruit_tcs34725.py +++ b/adafruit_tcs34725.py @@ -78,6 +78,8 @@ _ENABLE_PON = const(0x01) _GAINS = (1, 4, 16, 60) _CYCLES = (0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60) +_INTEGRATION_TIME_THRESHOLD_LOW = 2.4 +_INTEGRATION_TIME_THRESHOLD_HIGH = 614.4 # pylint: enable=bad-whitespace @@ -172,7 +174,9 @@ def integration_time(self): @integration_time.setter def integration_time(self, val): - assert 2.4 <= val <= 614.4 + if not _INTEGRATION_TIME_THRESHOLD_LOW <= val <= _INTEGRATION_TIME_THRESHOLD_HIGH: + raise ValueError("Integration Time must be between '{0}' and '{1}'".format( + _INTEGRATION_TIME_THRESHOLD_LOW, _INTEGRATION_TIME_THRESHOLD_HIGH)) cycles = int(val / 2.4) self._integration_time = cycles * 2.4 # pylint: disable=attribute-defined-outside-init self._write_u8(_REGISTER_ATIME, 256-cycles) @@ -186,7 +190,8 @@ def gain(self): @gain.setter def gain(self, val): - assert val in _GAINS + if val not in _GAINS: + raise ValueError("Gain should be one of the following values: {0}".format(_GAINS)) self._write_u8(_REGISTER_CONTROL, _GAINS.index(val)) @property @@ -198,7 +203,8 @@ def interrupt(self): @interrupt.setter def interrupt(self, val): - assert not val + if val: + raise ValueError("Interrupt should be set to False in order to clear the interrupt") with self._device: self._device.write(b'\xe6') @@ -262,7 +268,8 @@ def cycles(self, val): if val == -1: self._write_u8(_REGISTER_ENABLE, enable & ~(_ENABLE_AIEN)) else: - assert val in _CYCLES + if val not in _CYCLES: + raise ValueError("Only the following values for cycles are permitted: {0}".format(_CYCLES)) self._write_u8(_REGISTER_ENABLE, enable | _ENABLE_AIEN) self._write_u8(_REGISTER_APERS, _CYCLES.index(val)) From 9376732c8dced54dc573f0698324491ec92cea12 Mon Sep 17 00:00:00 2001 From: VladMihai28 Date: Tue, 2 Oct 2018 11:11:05 +0300 Subject: [PATCH 2/2] Issue#11 Fixing line too long warning --- adafruit_tcs34725.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_tcs34725.py b/adafruit_tcs34725.py index f92c3a4..893ae91 100644 --- a/adafruit_tcs34725.py +++ b/adafruit_tcs34725.py @@ -269,7 +269,7 @@ def cycles(self, val): self._write_u8(_REGISTER_ENABLE, enable & ~(_ENABLE_AIEN)) else: if val not in _CYCLES: - raise ValueError("Only the following values for cycles are permitted: {0}".format(_CYCLES)) + raise ValueError("Only the following cycles are permitted: {0}".format(_CYCLES)) self._write_u8(_REGISTER_ENABLE, enable | _ENABLE_AIEN) self._write_u8(_REGISTER_APERS, _CYCLES.index(val))