4.6. Match Constant
A constant value pattern works like the literal but for certain named
constants. Note that it must be a qualified (dotted) name, given the
possible ambiguity with a capture pattern. It looks like Color.RED
and only matches values equal to the corresponding value. It never
binds.
4.6.1. Problem
>>> from enum import Enum
>>>
>>>
>>> class Color(Enum):
... RED = 'r'
... GREEN = 'g'
... BLUE = 'b'
>>>
>>> color = Color.RED
>>>
>>> if color is Color.RED:
... print('red')
... elif color is Color.GREEN:
... print('green')
... elif color is Color.BLUE:
... print('blue')
...
red
4.6.2. Solution
>>> from enum import Enum
>>>
>>>
>>> class Color(Enum):
... RED = 'r'
... GREEN = 'g'
... BLUE = 'b'
>>>
>>> color = Color.RED
>>>
>>> match color:
... case Color.RED: print('red')
... case Color.GREEN: print('green')
... case Color.BLUE: print('blue')
...
red
4.6.3. Case Study
import requests
DATA = 'https://python3.info'
resp = requests.get(DATA)
if resp.status_code == 200:
html = resp.text
elif resp.status_code == 301:
raise ConnectionResetError('Redirect')
elif resp.status_code == 403:
raise PermissionError('Forbidden')
else:
raise ConnectionError('Invalid status code')
print(html[:110])
# <!DOCTYPE html>
# <html class="writer-html5" lang="en" data-content_root="./">
# <head>
# <meta charset="utf-8" />
from enum import Enum
import requests
class HTTPStatus(Enum):
OK = 200
MOVED_PERMANENTLY = 301
FORBIDDEN = 403
DATA = 'https://python3.info'
resp = requests.get(DATA)
status_code = HTTPStatus(resp.status_code)
if status_code is HTTPStatus.OK:
html = resp.text
elif status_code is HTTPStatus.MOVED_PERMANENTLY:
raise ConnectionResetError('Redirect')
elif status_code is HTTPStatus.FORBIDDEN:
raise PermissionError('Forbidden')
else:
raise ConnectionError('Invalid status code')
print(html[:110])
# <!DOCTYPE html>
# <html class="writer-html5" lang="en" data-content_root="./">
# <head>
# <meta charset="utf-8" />
from enum import Enum
import requests
class HTTPStatus(Enum):
OK = 200
MOVED_PERMANENTLY = 301
FORBIDDEN = 403
DATA = 'https://python3.info'
resp = requests.get(DATA)
match HTTPStatus(resp.status_code):
case HTTPStatus.OK: html = resp.text
case HTTPStatus.MOVED_PERMANENTLY: raise ConnectionResetError('Redirect')
case HTTPStatus.FORBIDDEN: raise PermissionError('Forbidden')
case _: raise ConnectionError('Invalid status code')
print(html[:110])
# <!DOCTYPE html>
# <html class="writer-html5" lang="en" data-content_root="./">
# <head>
# <meta charset="utf-8" />
from http import HTTPStatus
import requests
DATA = 'https://python3.info'
resp = requests.get(DATA)
match HTTPStatus(resp.status_code):
case HTTPStatus.OK: html = resp.text
case HTTPStatus.MOVED_PERMANENTLY: raise ConnectionResetError('Redirect')
case HTTPStatus.FORBIDDEN: raise PermissionError('Forbidden')
case _: raise ConnectionError('Invalid status code')
print(html[:110])
# <!DOCTYPE html>
# <html class="writer-html5" lang="en" data-content_root="./">
# <head>
# <meta charset="utf-8" />
4.6.4. Use Case - 1
SetUp:
>>> from enum import IntEnum, StrEnum
>>> import requests
Usage:
>>> class Color(StrEnum):
... RED = '#FF0000'
... GREEN = '#00FF00'
... BLUE = '#0000FF'
>>>
>>>
>>> mycolor = '#FF0000'
>>>
>>> match mycolor:
... case Color.RED: print('red')
... case Color.BLUE: print('blue')
... case Color.GREEN: print('green')
...
red
4.6.5. Use Case - 2
>>> class HTTPStatus(IntEnum):
... OK = 200
... REDIRECT = 300
... SERVER_ERROR = 500
>>>
>>>
>>> resp = requests.get('https://python3.info')
>>>
>>> match resp.status_code:
... case HTTPStatus.OK: print('ok')
... case HTTPStatus.MOVED_PERMANENTLY: print('redirect')
... case HTTPStatus.SERVER_ERROR: print('error')
...
ok
4.6.6. Use Case - 3
>>> from http import HTTPStatus
>>> import requests
>>>
>>>
>>> resp = requests.get('https://python3.info')
>>>
>>> match resp.status_code:
... case HTTPStatus.OK: print('ok')
... case HTTPStatus.MOVED_PERMANENTLY: print('redirect')
... case HTTPStatus.SERVER_ERROR: print('error')
...
ok
4.6.7. Use Case - 4
Test Setup:
>>> class Keyboard:
... def on_key_press(self): ...
>>>
>>> keyboard = Keyboard()
>>> class Game:
... def quit(self): ...
... def move_left(self): ...
... def move_up(self): ...
... def move_right(self): ...
... def move_down(self): ...
>>>
>>> game = Game()
Use Case:
>>> from enum import Enum
>>>
>>>
>>> class Key(Enum):
... ESC = 27
... ARROW_LEFT = 37
... ARROW_UP = 38
... ARROW_RIGHT = 39
... ARROW_DOWN = 40
>>>
>>> match keyboard.on_key_press():
... case Key.ESC: game.quit()
... case Key.ARROW_LEFT: game.move_left()
... case Key.ARROW_UP: game.move_up()
... case Key.ARROW_RIGHT: game.move_right()
... case Key.ARROW_DOWN: game.move_down()
... case _: raise ValueError(f'Unrecognized key')
Traceback (most recent call last):
ValueError: Unrecognized key