]> git.madduck.net Git - etc/vim.git/blob - tests/data/cases/pattern_matching_simple.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Fix parser bug where "type" was misinterpreted as a keyword inside a match (#3950)
[etc/vim.git] / tests / data / cases / pattern_matching_simple.py
1 # flags: --minimum-version=3.10
2 # Cases sampled from PEP 636 examples
3
4 match command.split():
5     case [action, obj]:
6         ...  # interpret action, obj
7
8 match command.split():
9     case [action]:
10         ...  # interpret single-verb action
11     case [action, obj]:
12         ...  # interpret action, obj
13
14 match command.split():
15     case ["quit"]:
16         print("Goodbye!")
17         quit_game()
18     case ["look"]:
19         current_room.describe()
20     case ["get", obj]:
21         character.get(obj, current_room)
22     case ["go", direction]:
23         current_room = current_room.neighbor(direction)
24     # The rest of your commands go here
25
26 match command.split():
27     case ["drop", *objects]:
28         for obj in objects:
29             character.drop(obj, current_room)
30     # The rest of your commands go here
31
32 match command.split():
33     case ["quit"]:
34         pass
35     case ["go", direction]:
36         print("Going:", direction)
37     case ["drop", *objects]:
38         print("Dropping: ", *objects)
39     case _:
40         print(f"Sorry, I couldn't understand {command!r}")
41
42 match command.split():
43     case ["north"] | ["go", "north"]:
44         current_room = current_room.neighbor("north")
45     case ["get", obj] | ["pick", "up", obj] | ["pick", obj, "up"]:
46         ...  # Code for picking up the given object
47
48 match command.split():
49     case ["go", ("north" | "south" | "east" | "west")]:
50         current_room = current_room.neighbor(...)
51         # how do I know which direction to go?
52
53 match command.split():
54     case ["go", ("north" | "south" | "east" | "west") as direction]:
55         current_room = current_room.neighbor(direction)
56
57 match command.split():
58     case ["go", direction] if direction in current_room.exits:
59         current_room = current_room.neighbor(direction)
60     case ["go", _]:
61         print("Sorry, you can't go that way")
62
63 match event.get():
64     case Click(position=(x, y)):
65         handle_click_at(x, y)
66     case KeyPress(key_name="Q") | Quit():
67         game.quit()
68     case KeyPress(key_name="up arrow"):
69         game.go_north()
70     case KeyPress():
71         pass  # Ignore other keystrokes
72     case other_event:
73         raise ValueError(f"Unrecognized event: {other_event}")
74
75 match event.get():
76     case Click((x, y), button=Button.LEFT):  # This is a left click
77         handle_click_at(x, y)
78     case Click():
79         pass  # ignore other clicks
80
81
82 def where_is(point):
83     match point:
84         case Point(x=0, y=0):
85             print("Origin")
86         case Point(x=0, y=y):
87             print(f"Y={y}")
88         case Point(x=x, y=0):
89             print(f"X={x}")
90         case Point():
91             print("Somewhere else")
92         case _:
93             print("Not a point")