-
Notifications
You must be signed in to change notification settings - Fork 8
Replaced asserts with exceptions #15
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
Merged
caternuson
merged 6 commits into
adafruit:master
from
ArthurDent62:ArthurDent62-exceptionNotAssert
Feb 11, 2019
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f74ac7
Merge pull request #2 from adafruit/master
ArthurDent62 096b603
Merge pull request #3 from adafruit/master
ArthurDent62 162df6c
Changed assert to exceptions
ArthurDent62 3877382
changed asserts into raised exceptions
ArthurDent62 689bd4b
removed 8 spaces
ArthurDent62 1e6dfbe
styling the library
ArthurDent62 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,7 +65,7 @@ class TLC5947: | |
|
||
:param auto_write: This is a boolean that defaults to True and will automatically | ||
write out all the channel values to the chip as soon as a | ||
single one is updated. If you set to false to disable then | ||
single one is updated. If you set to False to disable then | ||
you MUST call write after every channel update or when you | ||
deem necessary to update the chip state. | ||
|
||
|
@@ -76,7 +76,7 @@ class TLC5947: | |
on the driver directly connected to the controller are 0 to | ||
23, and for each driver add 24 to the port number printed. | ||
The more drivers are chained, the more viable it is to set | ||
auto_write=false, and call write explicitly after updating | ||
auto_write=False, and call write explicitly after updating | ||
all the channels. | ||
""" | ||
|
||
|
@@ -104,7 +104,8 @@ def duty_cycle(self): | |
|
||
@duty_cycle.setter | ||
def duty_cycle(self, val): | ||
assert 0 <= val <= 65535 | ||
if (val < 0) or (val > 65535): | ||
raise ValueError("PWM intensity {0} outside supported range [0;65535]".format(val)) | ||
# Convert to 12-bit value (quantization error will occur!). | ||
val = (val >> 4) & 0xFFF | ||
self._tlc5947._set_gs_value(self._channel, val) | ||
|
@@ -128,6 +129,8 @@ def frequency(self, val): | |
|
||
|
||
def __init__(self, spi, latch, *, auto_write=True, num_drivers=1): | ||
if num_drivers < 1: | ||
raise ValueError("need at least one driver; {0} is not supported.".format(num_drivers)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Capitalize first word in message.
Same for all messages. |
||
self._spi = spi | ||
self._latch = latch | ||
self._latch.switch_to_output(value=False) | ||
|
@@ -165,7 +168,9 @@ def write(self): | |
def _get_gs_value(self, channel): | ||
# pylint: disable=no-else-return | ||
# Disable should be removed when refactor can be tested | ||
assert 0 <= channel < _CHANNELS * self._n | ||
if (channel < 0) or (channel >= _CHANNELS * self._n): | ||
raise ValueError( | ||
"channel {0} not available with {1} board(s).".format(channel, self._n)) | ||
# Invert channel position as the last channel needs to be written first. | ||
# I.e. is in the first position of the shift registr. | ||
channel = _CHANNELS * self._n - 1 - channel | ||
|
@@ -190,8 +195,12 @@ def _get_gs_value(self, channel): | |
raise RuntimeError('Unsupported bit offset!') | ||
|
||
def _set_gs_value(self, channel, val): | ||
assert 0 <= channel < _CHANNELS * self._n | ||
assert 0 <= val <= 4095 | ||
if (channel < 0) or (channel >= _CHANNELS * self._n): | ||
raise ValueError( | ||
"channel {0} not available with {1} board(s).".format(channel, self._n)) | ||
if (val < 0) or (val > 4095): | ||
raise ValueError("PWM intensity {0} outside supported range [0;4095]".format(val)) | ||
|
||
# Invert channel position as the last channel needs to be written first. | ||
# I.e. is in the first position of the shift registr. | ||
channel = _CHANNELS * self._n - 1 - channel | ||
|
@@ -247,8 +256,7 @@ def __getitem__(self, key): | |
""" | ||
if key < 0: # allow reverse adressing with negative index | ||
key = key + _CHANNELS * self._n | ||
assert 0 <= key < _CHANNELS * self._n | ||
return self._get_gs_value(key) | ||
return self._get_gs_value(key) # does parameter checking | ||
|
||
def __setitem__(self, key, val): | ||
"""Set the 12-bit PWM value (0-4095) for the specified channel (0-max). | ||
|
@@ -259,6 +267,4 @@ def __setitem__(self, key, val): | |
""" | ||
if key < 0: # allow reverse adressing with negative index | ||
key = key + _CHANNELS * self._n | ||
assert 0 <= key < _CHANNELS * self._n | ||
assert 0 <= val <= 4095 | ||
self._set_gs_value(key, val) | ||
self._set_gs_value(key, val) # does parameter checking |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
()
s. The "pythonic" syntax is:Same for all other conditionals.