Skip to content

Commit af73dce

Browse files
committed
Use conditional statements to reduce number of branches
1 parent 762accf commit af73dce

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

adafruit_usb_host_mouse/__init__.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
SUBCLASS_RESERVED = 0x00
4848

4949

50-
def find_and_init_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT): # noqa: PLR0912
50+
def find_and_init_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT):
5151
"""
5252
Scan for an attached mouse connected via USB host.
5353
If one is found return a tuple containing the parameters needed to initalize an
@@ -66,12 +66,11 @@ def find_and_init_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT): #
6666
"""
6767
mouse_interface_index, mouse_endpoint_address = None, None
6868
mouse_device = None
69-
if subclass == SUBCLASS_BOOT:
70-
deviceType = "boot"
71-
find_endpoint = adafruit_usb_host_descriptors.find_boot_mouse_endpoint
72-
else:
73-
deviceType = "report"
74-
find_endpoint = adafruit_usb_host_descriptors.find_report_mouse_endpoint
69+
deviceType, find_endpoint = (
70+
("boot", adafruit_usb_host_descriptors.find_boot_mouse_endpoint)
71+
if subclass == SUBCLASS_BOOT
72+
else ("report", adafruit_usb_host_descriptors.find_report_mouse_endpoint)
73+
)
7574

7675
# scan for connected USB device and loop over any found
7776
print(f"scanning usb ({deviceType})")
@@ -109,10 +108,7 @@ def find_and_init_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT): #
109108
mouse_was_attached = []
110109
if mouse_device is not None:
111110
# detach the kernel driver if needed
112-
if subclass == SUBCLASS_BOOT:
113-
possible_interfaces = [mouse_interface_index]
114-
else:
115-
possible_interfaces = [0, 1, 2]
111+
possible_interfaces = [mouse_interface_index] if subclass == SUBCLASS_BOOT else [0, 1, 2]
116112

117113
for intf in possible_interfaces:
118114
if mouse_device.is_kernel_driver_active(intf):
@@ -123,6 +119,7 @@ def find_and_init_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT): #
123119
mouse_device.set_configuration()
124120

125121
# load the mouse cursor bitmap
122+
mouse_tg = None
126123
if isinstance(cursor_image, str):
127124
mouse_bmp = OnDiskBitmap(cursor_image)
128125

@@ -132,9 +129,6 @@ def find_and_init_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT): #
132129
# create a TileGrid for the mouse, using its bitmap and pixel_shader
133130
mouse_tg = TileGrid(mouse_bmp, pixel_shader=mouse_bmp.pixel_shader)
134131

135-
else:
136-
mouse_tg = None
137-
138132
return (
139133
(mouse_device, mouse_interface_index, mouse_endpoint_address, mouse_was_attached),
140134
mouse_tg,
@@ -144,7 +138,7 @@ def find_and_init_mouse(cursor_image=DEFAULT_CURSOR, subclass=SUBCLASS_BOOT): #
144138
return None
145139

146140

147-
def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR, scale=1): # noqa: PLR0912
141+
def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR, scale=1):
148142
"""
149143
Scan for an attached boot mouse connected via USB host.
150144
If one is found initialize an instance of :class:`BootMouse` class
@@ -163,7 +157,7 @@ def find_and_init_boot_mouse(cursor_image=DEFAULT_CURSOR, scale=1): # noqa: PLR
163157
return None
164158

165159

166-
def find_and_init_report_mouse(cursor_image=DEFAULT_CURSOR, scale=1): # noqa: PLR0912
160+
def find_and_init_report_mouse(cursor_image=DEFAULT_CURSOR, scale=1):
167161
"""
168162
Scan for an attached report mouse connected via USB host.
169163
If one is found initialize an instance of :class:`ReportMouse` class

0 commit comments

Comments
 (0)