[libgpiod][PATCH 7/9] bindings: python: examples: apply linter recommendations

Vincent Fazio
2026-04-01T00:18:10Z

* Sort imports
* Use f-strings where possible
* Remove usage of `tuple` for config key values

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
bindings/python/examples/async_watch_line_value.py | 9 ++++-----
bindings/python/examples/find_line_by_name.py | 7 ++++---
bindings/python/examples/get_chip_info.py | 2 +-
bindings/python/examples/get_line_info.py | 12 +++++-------
bindings/python/examples/get_line_value.py | 3 +--
bindings/python/examples/get_multiple_line_values.py | 7 +++----
.../python/examples/reconfigure_input_to_output.py | 5 ++---
bindings/python/examples/toggle_line_value.py | 4 ++--
.../python/examples/toggle_multiple_line_values.py | 12 ++++--------
bindings/python/examples/watch_line_rising.py | 7 +++----
bindings/python/examples/watch_line_value.py | 10 +++++-----
.../python/examples/watch_multiple_line_values.py | 12 +++++-------
12 files changed, 39 insertions(+), 51 deletions(-)

diff --git a/bindings/python/examples/async_watch_line_value.py b/bindings/python/examples/async_watch_line_value.py
index 640a4c5..68c8b28 100755
--- a/bindings/python/examples/async_watch_line_value.py
+++ b/bindings/python/examples/async_watch_line_value.py
@@ -4,11 +4,10 @@

"""Minimal example of asynchronously watching for edges on a single line."""

-import gpiod
import select
-
from datetime import timedelta

+import gpiod
from gpiod.edge_event import EdgeEvent
from gpiod.line import Bias, Edge

@@ -48,9 +47,9 @@ def async_watch_line_value(chip_path: str, line_offset: int, done_fd: int) -> No
# handle any edge events
for event in request.read_edge_events():
print(
- "offset: {} type: {:<7} event #{}".format(
- event.line_offset, edge_type_str(event), event.line_seqno
- )
+ f"offset: {event.line_offset}"
+ f" type: {edge_type_str(event):<7}"
+ f" event #{event.line_seqno}"
)

diff --git a/bindings/python/examples/find_line_by_name.py b/bindings/python/examples/find_line_by_name.py
index 4ed3848..9523c1c 100755
--- a/bindings/python/examples/find_line_by_name.py
+++ b/bindings/python/examples/find_line_by_name.py
@@ -4,10 +4,11 @@

"""Minimal example of finding a line with the given name."""

-import gpiod
import os
from collections.abc import Generator

+import gpiod
+

def generate_gpio_chips() -> Generator[str]:
for entry in os.scandir("/dev/"):
@@ -22,13 +23,13 @@ def find_line_by_name(line_name: str) -> None:
with gpiod.Chip(path) as chip:
try:
offset = chip.line_offset_from_id(line_name)
- print("{}: {} {}".format(line_name, chip.get_info().name, offset))
+ print(f"{line_name}: {chip.get_info().name} {offset}")
return
except OSError:
# An OSError is raised if the name is not found.
continue

- print("line '{}' not found".format(line_name))
+ print(f"line '{line_name}' not found")

if __name__ == "__main__":
diff --git a/bindings/python/examples/get_chip_info.py b/bindings/python/examples/get_chip_info.py
index 037e424..addad5c 100755
--- a/bindings/python/examples/get_chip_info.py
+++ b/bindings/python/examples/get_chip_info.py
@@ -10,7 +10,7 @@ import gpiod
def get_chip_info(chip_path: str) -> None:
with gpiod.Chip(chip_path) as chip:
info = chip.get_info()
- print("{} [{}] ({} lines)".format(info.name, info.label, info.num_lines))
+ print(f"{info.name} [{info.label}] ({info.num_lines} lines)")

if __name__ == "__main__":
diff --git a/bindings/python/examples/get_line_info.py b/bindings/python/examples/get_line_info.py
index 8e7283e..4d9ba76 100755
--- a/bindings/python/examples/get_line_info.py
+++ b/bindings/python/examples/get_line_info.py
@@ -12,13 +12,11 @@ def get_line_info(chip_path: str, line_offset: int) -> None:
info = chip.get_line_info(line_offset)
is_input = info.direction == gpiod.line.Direction.INPUT
print(
- "line {:>3}: {:>12} {:>12} {:>8} {:>10}".format(
- info.offset,
- info.name or "unnamed",
- info.consumer or "unused",
- "input" if is_input else "output",
- "active-low" if info.active_low else "active-high",
- )
+ f"line {info.offset:>3}:"
+ f" {info.name or 'unnamed':>12}"
+ f" {info.consumer or 'unused':>12}"
+ f" {'input' if is_input else 'output':>8}"
+ f" {'active-low' if info.active_low else 'active-high':>10}"
)

diff --git a/bindings/python/examples/get_line_value.py b/bindings/python/examples/get_line_value.py
index 5ca952b..3984802 100755
--- a/bindings/python/examples/get_line_value.py
+++ b/bindings/python/examples/get_line_value.py
@@ -5,7 +5,6 @@
"""Minimal example of reading a single line."""

import gpiod
-
from gpiod.line import Direction

@@ -16,7 +15,7 @@ def get_line_value(chip_path: str, line_offset: int) -> None:
config={line_offset: gpiod.LineSettings(direction=Direction.INPUT)},
) as request:
value = request.get_value(line_offset)
- print("{}={}".format(line_offset, value))
+ print(f"{line_offset}={value}")

if __name__ == "__main__":
diff --git a/bindings/python/examples/get_multiple_line_values.py b/bindings/python/examples/get_multiple_line_values.py
index f70369f..66148a3 100755
--- a/bindings/python/examples/get_multiple_line_values.py
+++ b/bindings/python/examples/get_multiple_line_values.py
@@ -7,7 +7,6 @@
from collections.abc import Iterable

import gpiod
-
from gpiod.line import Direction

@@ -15,12 +14,12 @@ def get_multiple_line_values(chip_path: str, line_offsets: Iterable[int]) -> Non
with gpiod.request_lines(
chip_path,
consumer="get-multiple-line-values",
- config={tuple(line_offsets): gpiod.LineSettings(direction=Direction.INPUT)},
+ config={line_offsets: gpiod.LineSettings(direction=Direction.INPUT)},
) as request:
vals = request.get_values()

- for offset, val in zip(line_offsets, vals):
- print("{}={} ".format(offset, val), end="")
+ for offset, val in zip(line_offsets, vals, strict=True):
+ print(f"{offset}={val} ", end="")
print()

diff --git a/bindings/python/examples/reconfigure_input_to_output.py b/bindings/python/examples/reconfigure_input_to_output.py
index feb5f0b..a2224e8 100755
--- a/bindings/python/examples/reconfigure_input_to_output.py
+++ b/bindings/python/examples/reconfigure_input_to_output.py
@@ -5,7 +5,6 @@
"""Example of a bi-directional line requested as input and then switched to output."""

import gpiod
-
from gpiod.line import Direction, Value

@@ -18,7 +17,7 @@ def reconfigure_input_to_output(chip_path: str, line_offset: int) -> None:
) as request:
# read the current line value
value = request.get_value(line_offset)
- print("{}={} (input)".format(line_offset, value))
+ print(f"{line_offset}={value} (input)")
# switch the line to an output and drive it low
request.reconfigure_lines(
config={
@@ -29,7 +28,7 @@ def reconfigure_input_to_output(chip_path: str, line_offset: int) -> None:
)
# report the current driven value
value = request.get_value(line_offset)
- print("{}={} (output)".format(line_offset, value))
+ print(f"{line_offset}={value} (output)")

if __name__ == "__main__":
diff --git a/bindings/python/examples/toggle_line_value.py b/bindings/python/examples/toggle_line_value.py
index bc948de..bfd1585 100755
--- a/bindings/python/examples/toggle_line_value.py
+++ b/bindings/python/examples/toggle_line_value.py
@@ -4,9 +4,9 @@

"""Minimal example of toggling a single line."""

-import gpiod
import time

+import gpiod
from gpiod.line import Direction, Value

@@ -30,7 +30,7 @@ def toggle_line_value(chip_path: str, line_offset: int) -> None:
},
) as request:
while True:
- print("{}={}".format(line_offset, value_str[value]))
+ print(f"{line_offset}={value_str[value]}")
time.sleep(1)
value = toggle_value(value)
request.set_value(line_offset, value)
diff --git a/bindings/python/examples/toggle_multiple_line_values.py b/bindings/python/examples/toggle_multiple_line_values.py
index 74ff587..8c973f8 100755
--- a/bindings/python/examples/toggle_multiple_line_values.py
+++ b/bindings/python/examples/toggle_multiple_line_values.py
@@ -4,9 +4,9 @@

"""Minimal example of toggling multiple lines."""

-import gpiod
import time

+import gpiod
from gpiod.line import Direction, Value

@@ -24,18 +24,14 @@ def toggle_multiple_line_values(
request = gpiod.request_lines(
chip_path,
consumer="toggle-multiple-line-values",
- config={
- tuple(line_values.keys()): gpiod.LineSettings(direction=Direction.OUTPUT)
- },
+ config={line_values.keys(): gpiod.LineSettings(direction=Direction.OUTPUT)},
output_values=line_values,
)

while True:
- print(
- " ".join("{}={}".format(l, value_str[v]) for (l, v) in line_values.items())
- )
+ print(" ".join(f"{l}={value_str[v]}" for (l, v) in line_values.items())) # noqa: E741
time.sleep(1)
- for l, v in line_values.items():
+ for l, v in line_values.items(): # noqa: E741
line_values[l] = toggle_value(v)
request.set_values(line_values)

diff --git a/bindings/python/examples/watch_line_rising.py b/bindings/python/examples/watch_line_rising.py
index 2f7f25f..59977c4 100755
--- a/bindings/python/examples/watch_line_rising.py
+++ b/bindings/python/examples/watch_line_rising.py
@@ -5,7 +5,6 @@
"""Minimal example of watching for rising edges on a single line."""

import gpiod
-
from gpiod.line import Edge

@@ -19,9 +18,9 @@ def watch_line_rising(chip_path: str, line_offset: int) -> None:
# Blocks until at least one event is available
for event in request.read_edge_events():
print(
- "line: {} type: Rising event #{}".format(
- event.line_offset, event.line_seqno
- )
+ f"line: {event.line_offset}"
+ " type: Rising"
+ f" event #{event.line_seqno}"
)

diff --git a/bindings/python/examples/watch_line_value.py b/bindings/python/examples/watch_line_value.py
index 9b84d29..3140ea2 100755
--- a/bindings/python/examples/watch_line_value.py
+++ b/bindings/python/examples/watch_line_value.py
@@ -4,9 +4,9 @@

"""Minimal example of watching for edges on a single line."""

-import gpiod
-
from datetime import timedelta
+
+import gpiod
from gpiod.edge_event import EdgeEvent
from gpiod.line import Bias, Edge

@@ -37,9 +37,9 @@ def watch_line_value(chip_path: str, line_offset: int) -> None:
# Blocks until at least one event is available
for event in request.read_edge_events():
print(
- "line: {} type: {:<7} event #{}".format(
- event.line_offset, edge_type_str(event), event.line_seqno
- )
+ f"line: {event.line_offset}"
+ f" type: {edge_type_str(event):<7}"
+ f" event #{event.line_seqno}"
)

diff --git a/bindings/python/examples/watch_multiple_line_values.py b/bindings/python/examples/watch_multiple_line_values.py
index d0ee35a..2c308bb 100755
--- a/bindings/python/examples/watch_multiple_line_values.py
+++ b/bindings/python/examples/watch_multiple_line_values.py
@@ -23,17 +23,15 @@ def watch_multiple_line_values(chip_path: str, line_offsets: Iterable[int]) -> N
with gpiod.request_lines(
chip_path,
consumer="watch-multiple-line-values",
- config={tuple(line_offsets): gpiod.LineSettings(edge_detection=Edge.BOTH)},
+ config={line_offsets: gpiod.LineSettings(edge_detection=Edge.BOTH)},
) as request:
while True:
for event in request.read_edge_events():
print(
- "offset: {} type: {:<7} event #{} line event #{}".format(
- event.line_offset,
- edge_type_str(event),
- event.global_seqno,
- event.line_seqno,
- )
+ f"offset: {event.line_offset}"
+ f" type: {edge_type_str(event):<7}"
+ f" event #{event.global_seqno}"
+ f" line event #{event.line_seqno}"
)

--
2.43.0