Skip to content

Commit

Permalink
Better testing to BinaryLabel, error handling for setting numbers tha…
Browse files Browse the repository at this point in the history
…t are too large
  • Loading branch information
slightlynybbled committed May 22, 2018
1 parent ff75ec1 commit 9772e00
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
5 changes: 5 additions & 0 deletions tests/test_BinaryLabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def test_change_value(bl):
assert bl.get() == 10


def test_change_value_too_large(bl):
with pytest.raises(ValueError):
bl.set(0x100)


def test_get_bit(bl):
bl.set(0x55)

Expand Down
2 changes: 1 addition & 1 deletion tk_tools/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.9.4'
__version__ = '0.9.5'
16 changes: 5 additions & 11 deletions tk_tools/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,15 @@ class BinaryLabel(ttk.Label):
:param options: prefix string for identifiers
"""
def __init__(self, parent, value: int=0, prefix: str="", bit_width=8,
truncation_warning=True, **options):
**options):
self._parent = parent
super().__init__(self._parent, **options)

self._value = value
self._prefix = prefix
self._bit_width = bit_width
self._truncation_warning = truncation_warning
self._check_width()
self._text_update()

def _check_width(self):
if self._truncation_warning and\
len(str(bin(self._value)[2:])) > self._bit_width:
logger.warning(
type(self).__name__+": Displayed value is truncated on left"
" side due to insufficient bit width.")

def get(self):
"""
Return the current value
Expand All @@ -224,9 +215,12 @@ def set(self, value: int):
:param value:
:return: None
"""
max_value = int(''.join(['1' for _ in range(self._bit_width)]), 2)

if value > max_value:
raise ValueError('the value {} is larger than the maximum value {}'.format(value, max_value))

self._value = value
self._check_width()
self._text_update()

def _text_update(self):
Expand Down

0 comments on commit 9772e00

Please sign in to comment.