Skip to content

Instantly share code, notes, and snippets.

@mahenzon
Created April 9, 2023 17:20
Show Gist options
  • Save mahenzon/9dcd569f1eec227d319fe3cee6962f94 to your computer and use it in GitHub Desktop.
Save mahenzon/9dcd569f1eec227d319fe3cee6962f94 to your computer and use it in GitHub Desktop.
Python Pattern Matching
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Pattern Matching"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### JS example\n",
"\n",
"```js\n",
"\n",
"const superhero = 'Spider-Man';\n",
"switch (superhero) {\n",
" case 'Batman':\n",
" console.log('🦇 The Dark Knight!');\n",
" break;\n",
" case 'Wonder Woman':\n",
" console.log('👸 The Amazon Princess!');\n",
" break;\n",
" default:\n",
" console.log('💥 There are so many superheroes!');\n",
"}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"superheroes = {\n",
" 'Batman': '🦇 The Dark Knight!',\n",
" 'Wonder Woman': '👸 The Amazon Princess!',\n",
"}\n",
"\n",
"\n",
"def guess_hero(superhero):\n",
" text = superheroes.get(superhero, 'unknown')\n",
" print(text)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"🦇 The Dark Knight!\n",
"unknown\n"
]
}
],
"source": [
"guess_hero('Batman')\n",
"guess_hero('Spider-man')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def guess_hero(superhero):\n",
" match superhero:\n",
" case 'Batman':\n",
" print('🦇 The Dark Knight!')\n",
" case 'Wonder Woman':\n",
" print('👸 The Amazon Princess!')\n",
" case _:\n",
" print('unknown')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"🦇 The Dark Knight!\n",
"unknown\n"
]
}
],
"source": [
"guess_hero('Batman')\n",
"guess_hero('Spider-man')"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"def http_error(code):\n",
" match code:\n",
" case 400:\n",
" return \"Bad Request\"\n",
" case 401 | 403 | 444:\n",
" return \"Not Allowed\"\n",
" case 404:\n",
" return \"Not Found\"\n",
" case 418:\n",
" return \"I'm a teapot\"\n",
" case _:\n",
" return \"Something's wrong with the internet\""
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"400: Bad Request\n",
"404: Not Found\n",
"500: Something's wrong with the internet\n"
]
}
],
"source": [
"res_400 = http_error(400)\n",
"print(\"400:\", res_400)\n",
"res_404 = http_error(404)\n",
"print(\"404:\", res_404)\n",
"res_500 = http_error(500)\n",
"print(\"500:\", res_500)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"401: Not Allowed\n",
"403: Not Allowed\n",
"444: Not Allowed\n"
]
}
],
"source": [
"res_401 = http_error(401)\n",
"print(\"401:\", res_401)\n",
"res_403 = http_error(403)\n",
"print(\"403:\", res_403)\n",
"res_444 = http_error(444)\n",
"print(\"444:\", res_444)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"def process_data(data_size):\n",
" match data_size:\n",
" case 0:\n",
" print(\"There's not data at all\")\n",
" case n if n < 10:\n",
" print(\"There's some data, but not enough\")\n",
" case n if 10 <= n <= 100:\n",
" print(\"Data available, but need more\")\n",
" case n:\n",
" print(\"Enough data, size:\", n)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"There's not data at all\n",
"There's some data, but not enough\n",
"Data available, but need more\n",
"Data available, but need more\n",
"Enough data, size: 1000\n"
]
}
],
"source": [
"process_data(0)\n",
"process_data(7)\n",
"process_data(12)\n",
"process_data(50)\n",
"process_data(1000)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"from enum import IntEnum\n",
"\n",
"\n",
"class Action(IntEnum):\n",
" START = 0\n",
" MIGRATE = 1"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"def start():\n",
" print(\"starting\")\n",
" \n",
"\n",
"def migrate():\n",
" print(\"migrating\")"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"def run_action(action):\n",
" match action:\n",
" case Action.START:\n",
" start()\n",
" case Action.MIGRATE:\n",
" migrate()\n",
" case _:\n",
" print(\"unknown command\")"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"starting\n"
]
}
],
"source": [
"run_action(Action.START)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"migrating\n"
]
}
],
"source": [
"run_action(Action.MIGRATE)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"unknown command\n"
]
}
],
"source": [
"run_action(-1)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Point(x=2, y=3)\n"
]
}
],
"source": [
"from dataclasses import dataclass\n",
"\n",
"\n",
"\n",
"@dataclass\n",
"class Point:\n",
" x: int\n",
" y: int\n",
"\n",
" \n",
"print(Point(2, 3))"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"def comment_point(point):\n",
" match point:\n",
" case Point(0, 0):\n",
" print(\"Point at the center!\")\n",
" case Point(0, y):\n",
" print(\"On x axis, y =\", y)\n",
" case Point(x, 0):\n",
" print(\"On y axis, x =\", x)\n",
" case Point(x=x, y=y) if x < 10:\n",
" print(f\"X small = {x}, {y=}\")\n",
" case Point(x=x, y=y):\n",
" print(f\"Any point at {x=}, {y=}\")\n",
" case _:\n",
" print(\"Not a point\")"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Not a point\n",
"Point at the center!\n",
"On x axis, y = 7\n",
"On y axis, x = 3\n",
"X small = 5, y=9\n",
"Any point at x=15, y=9\n"
]
}
],
"source": [
"comment_point(123)\n",
"comment_point(Point(0, 0))\n",
"comment_point(Point(0, 7))\n",
"comment_point(Point(3, 0))\n",
"comment_point(Point(5, 9))\n",
"comment_point(Point(15, 9))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment