Skip to content

Commit 7448b35

Browse files
committed
Enforce method implementation in bindings
Add abstract methods to attribute base class in python bindings. This should fix certain codacy errors E1101 and R1714. Signed-off-by: Travis F. Collins <[email protected]>
1 parent a0b6944 commit 7448b35

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

bindings/python/iio.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from os import strerror as _strerror
4646
from platform import system as _system
4747
import weakref
48+
import abc
4849

4950
if "Windows" in _system():
5051
from ctypes import get_last_error
@@ -674,7 +675,7 @@ def iio_strerror(err, buf, length):
674675

675676

676677
version = _get_lib_version()
677-
backends = [_get_backend(x).decode("ascii") for x in range(0, _get_backends_count())]
678+
backends = [_get_backend(b).decode("ascii") for b in range(0, _get_backends_count())]
678679

679680

680681
class _attr(object):
@@ -686,6 +687,14 @@ def __init__(self, name, filename=None):
686687
def __str__(self):
687688
return self._name
688689

690+
@abc.abstractmethod
691+
def _read(self):
692+
pass
693+
694+
@abc.abstractmethod
695+
def _write(self, value):
696+
pass
697+
689698
name = property(
690699
lambda self: self._name, None, None, "The name of this attribute.\n\ttype=str"
691700
)
@@ -696,8 +705,8 @@ def __str__(self):
696705
"The filename in sysfs to which this attribute is bound.\n\ttype=str",
697706
)
698707
value = property(
699-
lambda self: self.__read(),
700-
lambda self, x: self.__write(x),
708+
lambda self: self._read(),
709+
lambda self, x: self._write(x),
701710
None,
702711
"Current value of this attribute.\n\ttype=str",
703712
)
@@ -725,12 +734,12 @@ def __init__(self, channel, name):
725734
)
726735
self._channel = channel
727736

728-
def _attr__read(self):
737+
def _read(self):
729738
buf = create_string_buffer(1024)
730739
_c_read_attr(self._channel, self._name_ascii, buf, len(buf))
731740
return buf.value.decode("ascii")
732741

733-
def _attr__write(self, value):
742+
def _write(self, value):
734743
_c_write_attr(self._channel, self._name_ascii, value.encode("ascii"))
735744

736745

@@ -754,12 +763,12 @@ def __init__(self, device, name):
754763
super(DeviceAttr, self).__init__(name)
755764
self._device = device
756765

757-
def _attr__read(self):
766+
def _read(self):
758767
buf = create_string_buffer(1024)
759768
_d_read_attr(self._device, self._name_ascii, buf, len(buf))
760769
return buf.value.decode("ascii")
761770

762-
def _attr__write(self, value):
771+
def _write(self, value):
763772
_d_write_attr(self._device, self._name_ascii, value.encode("ascii"))
764773

765774

@@ -782,12 +791,12 @@ def __init__(self, device, name):
782791
"""
783792
super(DeviceDebugAttr, self).__init__(device, name)
784793

785-
def _attr__read(self):
794+
def _read(self):
786795
buf = create_string_buffer(1024)
787796
_d_read_debug_attr(self._device, self._name_ascii, buf, len(buf))
788797
return buf.value.decode("ascii")
789798

790-
def _attr__write(self, value):
799+
def _write(self, value):
791800
_d_write_debug_attr(self._device, self._name_ascii, value.encode("ascii"))
792801

793802

@@ -809,12 +818,12 @@ def __init__(self, device, name):
809818
"""
810819
super(DeviceBufferAttr, self).__init__(device, name)
811820

812-
def _attr__read(self):
821+
def _read(self):
813822
buf = create_string_buffer(1024)
814823
_d_read_buffer_attr(self._device, self._name_ascii, buf, len(buf))
815824
return buf.value.decode("ascii")
816825

817-
def _attr__write(self, value):
826+
def _write(self, value):
818827
_d_write_buffer_attr(self._device, self._name_ascii, value.encode("ascii"))
819828

820829

@@ -1436,10 +1445,7 @@ def find_device(self, name_or_id):
14361445
returns: type=iio.Device or type=iio.Trigger
14371446
The IIO Device
14381447
"""
1439-
return next(
1440-
(x for x in self.devices if name_or_id == x.name or name_or_id == x.id),
1441-
None,
1442-
)
1448+
return next((x for x in self.devices if name_or_id in [x.name, x.id]), None,)
14431449

14441450
name = property(
14451451
lambda self: self._name, None, None, "Name of this IIO context.\n\ttype=str"

0 commit comments

Comments
 (0)