diff mbox series

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

Message ID 20240927-vfazio-mypy-v1-10-91a7c2e20884@xes-inc.com
State New
Headers show
Series bindings: python: conform to mypy and ruff linter recommendations | expand

Commit Message

Vincent Fazio Sept. 27, 2024, 6:53 p.m. UTC
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@xes-inc.com>
---
 bindings/python/gpiod/line_request.py | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

Comments

Bartosz Golaszewski Oct. 8, 2024, 1:19 p.m. UTC | #1
On Fri, Sep 27, 2024 at 8:57 PM Vincent Fazio <vfazio@xes-inc.com> wrote:
>
> 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@xes-inc.com>
> ---

Looks good.

Bart
diff mbox series

Patch

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: