[libgpiod][PATCH 10/22] bindings: python: convert lines to offsets in LineRequest

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Remove `_check_line_name` in favor of a new function, `_line_to_offset`,
that converts a line reference to an offset.

This new function helps narrow types and simplifies the iteration logic
that is used to build objects to interface with `_ext.Request`.

Signed-off-by: Vincent Fazio <vfazio@xxxxxxxxxxx>
---
 bindings/python/gpiod/line_request.py | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/bindings/python/gpiod/line_request.py b/bindings/python/gpiod/line_request.py
index 6c57f612a4167061945e798e93f069689723b583..a1ca64cfd82c32be5de3fc53f6c981026911bb9c 100644
--- a/bindings/python/gpiod/line_request.py
+++ b/bindings/python/gpiod/line_request.py
@@ -91,14 +91,15 @@ class LineRequest:
         """
         return self.get_values([line])[0]
 
-    def _check_line_name(self, line: Union[int, str]) -> bool:
-        if isinstance(line, str):
-            if line not in self._name_map:
-                raise ValueError("unknown line name: {}".format(line))
-
-            return True
-
-        return False
+    def _line_to_offset(self, line: Union[int, str]) -> int:
+        if isinstance(line, int):
+            return line
+        else:
+            _line: Union[int, None]
+            if (_line := self._name_map.get(line)) is None:
+                raise ValueError(f"unknown line name: {line}")
+            else:
+                return _line
 
     def get_values(
         self, lines: Optional[Iterable[Union[int, str]]] = None
@@ -119,10 +120,7 @@ class LineRequest:
 
         lines = lines or self._lines
 
-        offsets = [
-            self._name_map[line] if self._check_line_name(line) else line
-            for line in lines
-        ]
+        offsets = [self._line_to_offset(line) for line in lines]
 
         buf = [None] * len(lines)
 
@@ -152,10 +150,7 @@ class LineRequest:
         self._check_released()
         self._req = cast(_ext.Request, self._req)
 
-        mapped = {
-            self._name_map[line] if self._check_line_name(line) else line: values[line]
-            for line in values
-        }
+        mapped = {self._line_to_offset(line): value for line, value in values.items()}
 
         self._req.set_values(mapped)
 
@@ -186,7 +181,7 @@ class LineRequest:
                 lines = (lines,)
 
             for line in lines:
-                offset = self._name_map[line] if self._check_line_name(line) else line
+                offset = self._line_to_offset(line)
                 line_settings[offset] = settings
 
         for offset in self.offsets:

-- 
2.34.1




[Index of Archives]     [Linux SPI]     [Linux Kernel]     [Linux ARM (vger)]     [Linux ARM MSM]     [Linux Omap]     [Linux Arm]     [Linux Tegra]     [Fedora ARM]     [Linux for Samsung SOC]     [eCos]     [Linux Fastboot]     [Gcc Help]     [Git]     [DCCP]     [IETF Announce]     [Security]     [Linux MIPS]     [Yosemite Campsites]

  Powered by Linux