Skip to content

Commit 92b0d22

Browse files
committed
replace the bulky if-else block with a match statement
1 parent c2c72fc commit 92b0d22

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

pybricksdev/cli/__init__.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -301,40 +301,38 @@ async def reconnect_hub():
301301
)
302302
)
303303

304-
if response == response_options[ResponseOptions.RECOMPILE_RUN]:
305-
with _get_script_path(args.file) as script_path:
306-
await hub.run(script_path, wait=True)
307-
308-
elif (
309-
response == response_options[ResponseOptions.RECOMPILE_DOWNLOAD]
310-
):
311-
with _get_script_path(args.file) as script_path:
312-
await hub.download(script_path)
313-
314-
elif (
315-
response == response_options[ResponseOptions.CHANGE_TARGET_FILE]
316-
):
317-
args.file.close()
318-
while True:
319-
try:
320-
args.file = open(
321-
await hub.race_disconnect(
322-
hub.race_power_button_press(
323-
questionary.path(
324-
"What file would you like to use?"
325-
).ask_async()
304+
match response_options.index(response):
305+
306+
case ResponseOptions.RECOMPILE_RUN:
307+
with _get_script_path(args.file) as script_path:
308+
await hub.run(script_path, wait=True)
309+
310+
case ResponseOptions.RECOMPILE_DOWNLOAD:
311+
with _get_script_path(args.file) as script_path:
312+
await hub.download(script_path)
313+
314+
case ResponseOptions.CHANGE_TARGET_FILE:
315+
args.file.close()
316+
while True:
317+
try:
318+
args.file = open(
319+
await hub.race_disconnect(
320+
hub.race_power_button_press(
321+
questionary.path(
322+
"What file would you like to use?"
323+
).ask_async()
324+
)
326325
)
327326
)
328-
)
329-
break
330-
except FileNotFoundError:
331-
print("The file was not found. Please try again.")
332-
# send the new target file to the hub
333-
with _get_script_path(args.file) as script_path:
334-
await hub.download(script_path)
335-
336-
else:
337-
return
327+
break
328+
except FileNotFoundError:
329+
print("The file was not found. Please try again.")
330+
# send the new target file to the hub
331+
with _get_script_path(args.file) as script_path:
332+
await hub.download(script_path)
333+
334+
case _:
335+
return
338336

339337
except HubPowerButtonPressedError:
340338
# This means the user pressed the button on the hub to re-start the

0 commit comments

Comments
 (0)