def asbool(self, property): """ Get the given ``property`` from this device as boolean. A boolean property has either a value of ``'1'`` or of ``'0'``, where ``'1'`` stands for ``True``, and ``'0'`` for ``False``. Any other value causes a :exc:`~exceptions.ValueError` to be raised. ``property`` is a unicode or byte string containing the name of the property. Return ``True``, if the property value is ``'1'`` and ``False``, if the property value is ``'0'``. Any other value raises a :exc:`~exceptions.ValueError`. Raise a :exc:`~exceptions.KeyError`, if the given property is not defined for this device. """ return string_to_bool(self[property])
def asbool(self, attribute): """ Get the given ``attribute`` from this device as a bool. :param attribute: the key for an attribute value :type attribute: unicode or byte string :returns: the value corresponding to ``attribute``, as bool :rtype: bool :raises KeyError: if no value found for ``attribute`` :raises UnicodeDecodeError: if value is not convertible to unicode :raises ValueError: if unicode value can not be converted to a bool A boolean attribute has either a value of ``'1'`` or of ``'0'``, where ``'1'`` stands for ``True``, and ``'0'`` for ``False``. Any other value causes a :exc:`~exceptions.ValueError` to be raised. """ return string_to_bool(self.asstring(attribute))
def test_string_to_bool_false(): assert isinstance(_util.string_to_bool('0'), bool) assert not _util.string_to_bool('0')
def test_string_to_bool_true(): assert isinstance(_util.string_to_bool('1'), bool) assert _util.string_to_bool('1')
def test_string_to_bool_invalid_value(): with pytest.raises(ValueError) as exc_info: _util.string_to_bool('foo') assert str(exc_info.value) == 'Not a boolean value: {0!r}'.format('foo')