From 4566223f587058bf4652e443e093296ba57fc06d Mon Sep 17 00:00:00 2001 From: haixuanTao Date: Wed, 15 Nov 2023 12:17:13 +0100 Subject: [PATCH] =?UTF-8?q?Removing=20`match`=20statement=20with=20`if-els?= =?UTF-8?q?e`=20as=20many=20people=20still=20uses=20 DoraStatus: - match dora_event["type"]: - case "INPUT": - ret, frame = self.video_capture.read() - if ret: - frame = cv2.resize(frame, (CAMERA_WIDTH, CAMERA_HEIGHT)) + event_type = dora_event["type"] + if event_type == "INPUT": + ret, frame = self.video_capture.read() + if ret: + frame = cv2.resize(frame, (CAMERA_WIDTH, CAMERA_HEIGHT)) - ## Push an error image in case the camera is not available. - else: - frame = np.zeros((CAMERA_HEIGHT, CAMERA_WIDTH, 3), dtype=np.uint8) - cv2.putText( - frame, - "No Webcam was found at index %d" % (CAMERA_INDEX), - (int(30), int(30)), - font, - 0.75, - (255, 255, 255), - 2, - 1, - ) - - send_output( - "image", - pa.array(frame.ravel()), - dora_event["metadata"], + ## Push an error image in case the camera is not available. + else: + frame = np.zeros((CAMERA_HEIGHT, CAMERA_WIDTH, 3), dtype=np.uint8) + cv2.putText( + frame, + "No Webcam was found at index %d" % (CAMERA_INDEX), + (int(30), int(30)), + font, + 0.75, + (255, 255, 255), + 2, + 1, ) - case "STOP": - print("received stop") - case other: - print("received unexpected event:", other) + + send_output( + "image", + pa.array(frame.ravel()), + dora_event["metadata"], + ) + elif event_type == "INPUT": + print("received stop") + else: + print("received unexpected event:", event_type) if time.time() - self.start_time < 20: return DoraStatus.CONTINUE diff --git a/examples/python-ros2-dataflow/control_node.py b/examples/python-ros2-dataflow/control_node.py index 0437b871..7540c092 100755 --- a/examples/python-ros2-dataflow/control_node.py +++ b/examples/python-ros2-dataflow/control_node.py @@ -12,22 +12,22 @@ for i in range(500): if event is None: break if event["type"] == "INPUT": - match event["id"]: - case "turtle_pose": - print( - f"""Pose: {event["value"].tolist()}""".replace("\r", "").replace( - "\n", " " - ) + event_id = event["id"] + if event_id == "turtle_pose": + print( + f"""Pose: {event["value"].tolist()}""".replace("\r", "").replace( + "\n", " " ) - case "tick": - direction = { - "linear": { - "x": 1.0 + random.random(), - }, - "angular": {"z": (random.random() - 0.5) * 5}, - } + ) + elif event_id == "tick": + direction = { + "linear": { + "x": 1.0 + random.random(), + }, + "angular": {"z": (random.random() - 0.5) * 5}, + } - node.send_output( - "direction", - pa.array([direction]), - ) + node.send_output( + "direction", + pa.array([direction]), + ) diff --git a/examples/python-ros2-dataflow/random_turtle.py b/examples/python-ros2-dataflow/random_turtle.py index 24fa1c2a..d466ab1e 100755 --- a/examples/python-ros2-dataflow/random_turtle.py +++ b/examples/python-ros2-dataflow/random_turtle.py @@ -43,20 +43,20 @@ for i in range(500): event = dora_node.next() if event is None: break - match event["kind"]: - # Dora event - case "dora": - match event["type"]: - case "INPUT": - match event["id"]: - case "direction": - twist_writer.publish(event["value"]) + event_kind = event["kind"] + # Dora event + if event_kind == "dora": + event_type = event["type"] + if event_type == "INPUT": + event_id = event["id"] + if event_id == "direction": + twist_writer.publish(event["value"]) # ROS2 Event - case "external": - pose = event.inner()[0].as_py() - if i == CHECK_TICK: - assert ( - pose["x"] != 5.544444561004639 - ), "turtle should not be at initial x axis" - dora_node.send_output("turtle_pose", event.inner()) + elif event_kind == "external": + pose = event.inner()[0].as_py() + if i == CHECK_TICK: + assert ( + pose["x"] != 5.544444561004639 + ), "turtle should not be at initial x axis" + dora_node.send_output("turtle_pose", event.inner())