Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created November 21, 2024 18:03
Show Gist options
  • Select an option

  • Save akimboyko/c0b5ab2eafff1db6c3f8cd0fc03bce16 to your computer and use it in GitHub Desktop.

Select an option

Save akimboyko/c0b5ab2eafff1db6c3f8cd0fc03bce16 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "4a343f59-d4cf-4bac-a96e-08d45df74032",
"metadata": {},
"source": [
"# Python 3.10"
]
},
{
"cell_type": "markdown",
"id": "85b2e318-b2f0-49e0-b692-3d9eaa367de3",
"metadata": {},
"source": [
"## Deconstruction"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "3d053017-960b-4a0d-b8c5-856e1ed1a504",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"11 12\n",
"13 14\n",
"15 16\n",
"17 18\n"
]
}
],
"source": [
"a = 10\n",
"\n",
"print(a)\n",
"\n",
"xy = (11, 12)\n",
"\n",
"print(xy[0], xy[1])\n",
"\n",
"(x, y) = (13, 14)\n",
"\n",
"print(x, y)\n",
"\n",
"[x, y] = [15, 16]\n",
"\n",
"print(x, y)\n",
"\n",
"[x, y, *_] = [17, 18, 19, 20]\n",
"\n",
"print(x, y)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "eea2c848-01be-454a-b3c5-65a2328a8120",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"21 22\n"
]
}
],
"source": [
"from collections import namedtuple\n",
"\n",
"Container = namedtuple(\"Container\", [\"x\", \"y\"])\n",
"\n",
"(x, y) = Container(21, 22)\n",
"\n",
"print(x, y)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "8208c37d-1489-47b3-b818-db040eeca9d9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"run fast\n",
"run fast ['furious']\n"
]
}
],
"source": [
"from dataclasses import dataclass\n",
"from typing import List\n",
"from typing import Set\n",
"\n",
"@dataclass\n",
"class Command:\n",
" command: str\n",
" arguments: List[str] = None\n",
"\n",
" # support object unpack\n",
" def __iter__(self):\n",
" return iter((self.command, self.arguments))\n",
"\n",
"command = Command(command=\"run\", arguments=[\"fast\"])\n",
"\n",
"(c, [speed]) = command\n",
"\n",
"print(c, speed)\n",
"\n",
"command = Command(command=\"run\", arguments=[\"fast\", \"furious\"])\n",
"\n",
"(c, [speed, *attrs]) = command\n",
"\n",
"print(c, speed, attrs)"
]
},
{
"cell_type": "markdown",
"id": "094e874d-45eb-4834-b128-1432ea168662",
"metadata": {},
"source": [
"## Structural Pattern Matching"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "2fbb9a75-cae4-47ad-977c-9524fa1e7377",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<---\n",
"--->\n",
"^^^^\n",
"====\n",
"Unknown direction 'diagonal'.\n"
]
}
],
"source": [
"def translate_horizontal(word: str) -> str:\n",
" match word:\n",
" case \"left\":\n",
" return \"<---\"\n",
" case \"right\":\n",
" return \"--->\"\n",
" case \"up\": \n",
" return \"^^^^\"\n",
" case \"down\": \n",
" return \"====\"\n",
" case other:\n",
" return f\"Unknown direction '{other}'.\"\n",
"\n",
"print(translate_horizontal(\"left\"))\n",
"print(translate_horizontal(\"right\"))\n",
"print(translate_horizontal(\"up\"))\n",
"print(translate_horizontal(\"down\"))\n",
"print(translate_horizontal(\"diagonal\"))"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "a2937f0b-f39c-4bfe-9281-a77cba0858f4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"horizontal left\n",
"horizontal right\n",
"vertical up\n",
"jump with speed=1: 2 3 4\n",
"---> with 50\n"
]
}
],
"source": [
"def translate_all(words: str) -> str:\n",
" match words.split():\n",
" case [\"left\" | \"right\" as c]:\n",
" return f\"horizontal {c}\"\n",
" case [\"up\" | \"down\" as c]:\n",
" return f\"vertical {c}\"\n",
" case [direction, speed]:\n",
" return f\"{translate_horizontal(direction)} with {speed}\"\n",
" case [\"jump\" as c, speed, *rest]:\n",
" return f\"{c} with {speed=!s}: {' '.join(rest)}\"\n",
" case _:\n",
" return f\"Unknown direction '{words}'.\"\n",
"\n",
"print(translate_all(\"left\"))\n",
"print(translate_all(\"right\"))\n",
"print(translate_all(\"up\"))\n",
"print(translate_all(\"jump 1 2 3 4\"))\n",
"print(translate_all(\"right 50\"))"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "ade23109-ce2a-4ed9-ab81-e4ccd911e2f4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"horizontal left\n",
"horizontal right\n",
"vertical up\n",
"jump: 1 2 3 4\n",
"horizontal right with 50\n",
"vertical up with 3\n"
]
}
],
"source": [
"def translate_command(command: Command) -> str:\n",
" match command:\n",
" case Command(command=\"jump\" as c, arguments=[*rest]):\n",
" return f\"{c}: {' '.join(map(str, rest))}\"\n",
" case Command(command=\"left\" | \"right\" as c, arguments=[speed]):\n",
" return f\"horizontal {c} with {speed}\"\n",
" case Command(command=\"up\" | \"down\" as c, arguments=[speed]):\n",
" return f\"vertical {c} with {speed}\"\n",
" case Command(command=\"left\" | \"right\" | \"up\" | \"down\" as c):\n",
" return translate_all(c)\n",
" case _:\n",
" return f\"Unknown {command=}\"\n",
"\n",
"print(translate_command(Command(command=\"left\")))\n",
"print(translate_command(Command(command=\"right\")))\n",
"print(translate_command(Command(command=\"up\")))\n",
"print(translate_command(Command(command=\"jump\", arguments=[1, 2, 3, 4])))\n",
"print(translate_command(Command(command=\"right\", arguments=[50])))\n",
"print(translate_command(Command(command=\"up\", arguments=[3])))"
]
},
{
"cell_type": "markdown",
"id": "9e2fed52-5165-4591-8f38-1b63b0f3f055",
"metadata": {},
"source": [
"## Allow writing union types as `X | Y`\n",
"\n",
"Instead of `Union[X, Y]`"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "f71e72b8-a2fc-4877-b09e-d3693aea92ef",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'a b c'"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def join(xs: List | Set) -> str:\n",
" return \" \".join(xs)\n",
"\n",
"join([\"a\", \"b\", \"c\"])"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "aaf63791-f2b1-4a86-bfcb-2f5e7d7523d2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'A B'"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"join((\"A\", \"B\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "033a3bdd-974c-4ecf-a1cc-389b6123a34c",
"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.13.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment