]> git.madduck.net Git - etc/vim.git/blob - tests/data/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:

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