Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created November 21, 2024 18:07
Show Gist options
  • Save akimboyko/83c1666efa5747ba20872d8566c3b33e to your computer and use it in GitHub Desktop.
Save akimboyko/83c1666efa5747ba20872d8566c3b33e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "e8226c4d-873d-494c-86b6-a6bcdb9d2ae2",
"metadata": {},
"source": [
"# Python 3.11"
]
},
{
"cell_type": "markdown",
"id": "9ef9b86f-8690-4866-baca-e7ca8c4576d0",
"metadata": {},
"source": [
"## Exception notes"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c41cdb90-53e5-439c-9110-4a4162b01453",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported type",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[8], line 8\u001b[0m\n\u001b[1;32m 5\u001b[0m type_error\u001b[38;5;241m.\u001b[39madd_note(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCustom note for this exception\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m\n\u001b[0;32m----> 8\u001b[0m \u001b[43msome_implementation\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[8], line 3\u001b[0m, in \u001b[0;36msome_implementation\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msome_implementation\u001b[39m() \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124munsupported type\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m type_error:\n\u001b[1;32m 5\u001b[0m type_error\u001b[38;5;241m.\u001b[39madd_note(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCustom note for this exception\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[0;31mTypeError\u001b[0m: unsupported type",
"\u001b[0mCustom note for this exception"
]
}
],
"source": [
"def some_implementation() -> None:\n",
" try:\n",
" raise TypeError(\"unsupported type\")\n",
" except TypeError as type_error:\n",
" type_error.add_note(\"Custom note for this exception\")\n",
" raise\n",
"\n",
"some_implementation()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "9bd24395-1638-45e1-a2cb-041105563bb6",
"metadata": {},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[9], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdiv_by\u001b[39m(n):\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m100\u001b[39m \u001b[38;5;241m/\u001b[39m n\n\u001b[0;32m----> 4\u001b[0m \u001b[43mdiv_by\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[9], line 2\u001b[0m, in \u001b[0;36mdiv_by\u001b[0;34m(n)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdiv_by\u001b[39m(n):\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241;43m100\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mn\u001b[49m\n",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
"def div_by(n):\n",
" return 100 / n\n",
"\n",
"div_by(0)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e6c4c624-07f3-45bc-9f12-1d0c56be85cd",
"metadata": {},
"source": [
"### Fine-grained error locations in tracebacks\n",
"\n",
"```python\n",
"Traceback (most recent call last):\n",
" File \"<python-input-3>\", line 4, in <module>\n",
" div_by(0)\n",
" ~~~~~~^^^\n",
" File \"<python-input-3>\", line 2, in div_by\n",
" return 100 / n\n",
" ~~~~^~~\n",
"ZeroDivisionError: division by zero\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "104178ba-6c7c-4aff-98d4-ab495de2e184",
"metadata": {},
"source": [
"## Arbitrary literal string type\n",
"\n",
"```python\n",
"def run_query(sql: LiteralString) -> ...\n",
" ...\n",
"\n",
"def caller(\n",
" arbitrary_string: str,\n",
" query_string: LiteralString,\n",
" table_name: LiteralString,\n",
") -> None:\n",
" run_query(\"SELECT * FROM students\") # ok\n",
" run_query(query_string) # ok\n",
" run_query(\"SELECT * FROM \" + table_name) # ok\n",
" run_query(arbitrary_string) # type checker error\n",
" run_query( # type checker error\n",
" f\"SELECT * FROM students WHERE name = {arbitrary_string}\"\n",
" )\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "802b4ac7-2e61-40cb-a32c-659520124003",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'(1,1) -> (1,0)'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from dataclasses import dataclass\n",
"from typing import Self, Type\n",
"\n",
"\n",
"@dataclass\n",
"class Point2D:\n",
" x: int\n",
" y: int\n",
"\n",
" def move_down(self: Self) -> Self:\n",
" return Point(self.x, self.y - 1)\n",
"\n",
" def __str__(self: Self):\n",
" return f\"({self.x},{self.y})\"\n",
"\n",
"@dataclass\n",
"class Point3D:\n",
" x: int\n",
" y: int\n",
" z: int\n",
"\n",
" def move_down(self: Self) -> Self:\n",
" return Point(self.x, self.y - 1, self.z)\n",
"\n",
" def __str__(self: Self):\n",
" return f\"({self.x},{self.y},{self.z})\"\n",
"\n",
"p1 = Point2D(1, 1)\n",
"p2 = p1.move_down()\n",
"\n",
"f\"{p1} -> {p2}\""
]
},
{
"cell_type": "markdown",
"id": "67c3bda0-0197-4904-b668-b2f6f8a23146",
"metadata": {},
"source": [
"## `StrEnum`\n",
"\n",
"With members that can be used as (and must be) strings."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c2e2cc77-05d6-4b81-b960-75e89ad6b82a",
"metadata": {},
"outputs": [],
"source": [
"from enum import StrEnum, auto\n",
"\n",
"# strenum==0.4.15 no more needed\n",
"# from strenum import StrEnum\n",
"\n",
"class KpiName(StrEnum):\n",
" GLOBAL_MODEL = auto() # not need to duplicate \"GLOBAL_MODEL\"\n",
" LOCAL_MODEL = auto()\n",
" GLOBAL_PREDICTION = auto()\n",
" LOCAL_PREDICTION = auto()\n",
" # ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb918856-0f9d-4813-9296-2522ec999649",
"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