Skip to content

Commit d69034b

Browse files
add content
1 parent 5dc38d7 commit d69034b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+5015
-2818
lines changed

Chapter2/functools.ipynb

Lines changed: 334 additions & 23 deletions
Large diffs are not rendered by default.

Chapter2/pydantic.ipynb

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,69 @@
2121
"id": "0f82baff",
2222
"metadata": {},
2323
"source": [
24-
"Dataclasses require manual implementation of validation.\n",
24+
"Dataclasses require manual implementation of validation."
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 11,
30+
"id": "5b663960-6035-414d-87de-c3b61194ce1f",
31+
"metadata": {},
32+
"outputs": [
33+
{
34+
"name": "stdout",
35+
"output_type": "stream",
36+
"text": [
37+
"Validation error: Age must be a valid integer, unable to parse string as an integer\n"
38+
]
39+
}
40+
],
41+
"source": [
42+
"from dataclasses import dataclass\n",
2543
"\n",
26-
"On the other hand, Pydantic offers built-in validation that automatically validates data and provides informative error messages. This makes Pydantic particularly useful when working with data from external sources.\n"
44+
"@dataclass\n",
45+
"class Dog:\n",
46+
" name: str\n",
47+
" age: int\n",
48+
"\n",
49+
" def __post_init__(self):\n",
50+
" if not isinstance(self.name, str):\n",
51+
" raise ValueError(\"Name must be a string\")\n",
52+
" \n",
53+
" try:\n",
54+
" self.age = int(self.age)\n",
55+
" except (ValueError, TypeError):\n",
56+
" raise ValueError(\"Age must be a valid integer, unable to parse string as an integer\")\n",
57+
"\n",
58+
"# Usage\n",
59+
"try:\n",
60+
" dog = Dog(name=\"Bim\", age=\"ten\")\n",
61+
"except ValueError as e:\n",
62+
" print(f\"Validation error: {e}\")"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"id": "719ee8e6-1035-457f-a04a-72117521f8e9",
68+
"metadata": {},
69+
"source": [
70+
"On the other hand, Pydantic offers built-in validation that automatically validates data and provides informative error messages. This makes Pydantic particularly useful when working with data from external sources."
2771
]
2872
},
2973
{
3074
"cell_type": "code",
31-
"execution_count": null,
75+
"execution_count": 7,
3276
"id": "9bc629b5",
3377
"metadata": {},
3478
"outputs": [
3579
{
36-
"ename": "ValidationError",
37-
"evalue": "1 validation error for Dog\nage\n Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='ten', input_type=str]\n For further information visit https://errors.pydantic.dev/2.5/v/int_parsing",
38-
"output_type": "error",
39-
"traceback": [
40-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n",
41-
"\u001b[0;31mValidationError\u001b[0m Traceback (most recent call last)\n",
42-
"Cell \u001b[0;32mIn[3], line 9\u001b[0m\n",
43-
"\u001b[1;32m 5\u001b[0m names: \u001b[38;5;28mstr\u001b[39m\n",
44-
"\u001b[1;32m 6\u001b[0m age: \u001b[38;5;28mint\u001b[39m\n",
45-
"\u001b[0;32m----> 9\u001b[0m dog \u001b[38;5;241m=\u001b[39m \u001b[43mDog\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnames\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mBim\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mage\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mten\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
46-
"\n",
47-
"File \u001b[0;32m~/book/venv/lib/python3.11/site-packages/pydantic/main.py:164\u001b[0m, in \u001b[0;36mBaseModel.__init__\u001b[0;34m(__pydantic_self__, **data)\u001b[0m\n",
48-
"\u001b[1;32m 162\u001b[0m \u001b[38;5;66;03m# `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks\u001b[39;00m\n",
49-
"\u001b[1;32m 163\u001b[0m __tracebackhide__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
50-
"\u001b[0;32m--> 164\u001b[0m \u001b[43m__pydantic_self__\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__pydantic_validator__\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalidate_python\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mself_instance\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m__pydantic_self__\u001b[49m\u001b[43m)\u001b[49m\n",
51-
"\n",
52-
"\u001b[0;31mValidationError\u001b[0m: 1 validation error for Dog\n",
80+
"name": "stdout",
81+
"output_type": "stream",
82+
"text": [
83+
"Validation error: 1 validation error for Dog\n",
5384
"age\n",
5485
" Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='ten', input_type=str]\n",
55-
" For further information visit https://errors.pydantic.dev/2.5/v/int_parsing"
86+
" For further information visit https://errors.pydantic.dev/2.5/v/int_parsing\n"
5687
]
5788
}
5889
],
@@ -61,11 +92,13 @@
6192
"\n",
6293
"\n",
6394
"class Dog(BaseModel):\n",
64-
" names: str\n",
95+
" name: str\n",
6596
" age: int\n",
6697
"\n",
67-
"\n",
68-
"dog = Dog(names=\"Bim\", age=\"ten\")"
98+
"try:\n",
99+
" dog = Dog(name=\"Bim\", age=\"ten\")\n",
100+
"except ValueError as e:\n",
101+
" print(f\"Validation error: {e}\")"
69102
]
70103
},
71104
{
@@ -153,7 +186,7 @@
153186
],
154187
"metadata": {
155188
"kernelspec": {
156-
"display_name": "venv",
189+
"display_name": "Python 3 (ipykernel)",
157190
"language": "python",
158191
"name": "python3"
159192
},

Chapter5/query.sql

Lines changed: 0 additions & 8 deletions
This file was deleted.

Chapter5/test_freezegun.py

Lines changed: 0 additions & 9 deletions
This file was deleted.
29.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)