CabrilloRoboticsClub / cabrillo_rov_2023

Cabrillo College Robotics Club repo for the MATE ROV 2023 competition
GNU Affero General Public License v3.0
10 stars 5 forks source link

Term widget command filter match (switch) statement (HELP WANTED) #211

Closed steph1111 closed 4 months ago

steph1111 commented 4 months ago

In the terminal widget, I am trying to emulate some of the more common terminal shortcuts (ctrl-a, ctrl-u, etc) and thus need to determine if the key sequence pressed is one of these options. I would like this to be a match statement of the form:

match(seq):
   case "ctrl-a":
      # do stuff
   case "ctrl-u":
      # do stuff

But this fails with the following errors. See commit b103449 for code.

Traceback (most recent call last):
  File "/home/steph/Documents/robotics/mate_2024/cabrillo_rov_2023/src/seahawk/seahawk_deck/dash_widgets/term_widget.py", line 194, in eventFilter
    case qtg.QKeySequence("Ctrl+C"):          # ctrl-c: Terminate process
TypeError: QKeySequence() accepts 0 positional sub-patterns (1 given)

However if I write it in the form:

if seq == "ctrl-a"
   # do stuff
elif seq == "ctrl-u"
   # do stuff

This works with no issues. See commit 4dd329b for code.

steph1111 commented 4 months ago

Update after talking to Mike:

match statements in python are not the same as a switch statement in C, C++, Java, etc. A switch statement checks for equality where as a match statement's primary function is pattern matching. Although their syntax look similar and in some cases they have the same functionality, a match statement is not a switch statement. This is why the code above fails, it should instead be written in the if, elif, elif format (even though its gross :sob: )