|
21 | 21 | "id": "0f82baff",
|
22 | 22 | "metadata": {},
|
23 | 23 | "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", |
25 | 43 | "\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." |
27 | 71 | ]
|
28 | 72 | },
|
29 | 73 | {
|
30 | 74 | "cell_type": "code",
|
31 |
| - "execution_count": null, |
| 75 | + "execution_count": 7, |
32 | 76 | "id": "9bc629b5",
|
33 | 77 | "metadata": {},
|
34 | 78 | "outputs": [
|
35 | 79 | {
|
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", |
53 | 84 | "age\n",
|
54 | 85 | " 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" |
56 | 87 | ]
|
57 | 88 | }
|
58 | 89 | ],
|
|
61 | 92 | "\n",
|
62 | 93 | "\n",
|
63 | 94 | "class Dog(BaseModel):\n",
|
64 |
| - " names: str\n", |
| 95 | + " name: str\n", |
65 | 96 | " age: int\n",
|
66 | 97 | "\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}\")" |
69 | 102 | ]
|
70 | 103 | },
|
71 | 104 | {
|
|
153 | 186 | ],
|
154 | 187 | "metadata": {
|
155 | 188 | "kernelspec": {
|
156 |
| - "display_name": "venv", |
| 189 | + "display_name": "Python 3 (ipykernel)", |
157 | 190 | "language": "python",
|
158 | 191 | "name": "python3"
|
159 | 192 | },
|
|
0 commit comments