|
100 | 100 | },
|
101 | 101 | {
|
102 | 102 | "cell_type": "code",
|
103 |
| - "execution_count": 1, |
| 103 | + "execution_count": null, |
104 | 104 | "id": "49c502e7",
|
105 | 105 | "metadata": {},
|
106 |
| - "outputs": [ |
107 |
| - { |
108 |
| - "ename": "ValidationError", |
109 |
| - "evalue": "2 validation errors for Song\nrelease_date\n Input should be less than 2024-05-10 [type=less_than, input_value=datetime.date(2024, 6, 1), input_type=date]\n For further information visit https://errors.pydantic.dev/2.5/v/less_than\nbeats_per_minute\n Field required [type=missing, input_value={'title': 'Believer', 'ar...2024, 6, 1), 'bpm': 125}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.5/v/missing", |
110 |
| - "output_type": "error", |
111 |
| - "traceback": [ |
112 |
| - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
113 |
| - "\u001b[0;31mValidationError\u001b[0m Traceback (most recent call last)", |
114 |
| - "Cell \u001b[0;32mIn[1], line 14\u001b[0m\n\u001b[1;32m 10\u001b[0m beats_per_minute: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m=\u001b[39m Field(multiple_of\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m5\u001b[39m)\n\u001b[1;32m 13\u001b[0m \u001b[38;5;66;03m# Example usage\u001b[39;00m\n\u001b[0;32m---> 14\u001b[0m song1 \u001b[38;5;241m=\u001b[39m \u001b[43mSong\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 15\u001b[0m \u001b[43m \u001b[49m\u001b[43mtitle\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mBeliever\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 16\u001b[0m \u001b[43m \u001b[49m\u001b[43martist\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mImagine Dragons\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 17\u001b[0m \u001b[43m \u001b[49m\u001b[43mduration\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m3.67\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 18\u001b[0m \u001b[43m \u001b[49m\u001b[43mrelease_date\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdate\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2024\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m6\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 19\u001b[0m \u001b[43m \u001b[49m\u001b[43mbpm\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m125\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 20\u001b[0m \u001b[43m)\u001b[49m\n", |
115 |
| - "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\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\u001b[1;32m 163\u001b[0m __tracebackhide__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\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", |
116 |
| - "\u001b[0;31mValidationError\u001b[0m: 2 validation errors for Song\nrelease_date\n Input should be less than 2024-05-10 [type=less_than, input_value=datetime.date(2024, 6, 1), input_type=date]\n For further information visit https://errors.pydantic.dev/2.5/v/less_than\nbeats_per_minute\n Field required [type=missing, input_value={'title': 'Believer', 'ar...2024, 6, 1), 'bpm': 125}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.5/v/missing" |
117 |
| - ] |
118 |
| - } |
119 |
| - ], |
| 106 | + "outputs": [], |
120 | 107 | "source": [
|
121 | 108 | "from pydantic import BaseModel, Field\n",
|
122 | 109 | "from datetime import date\n",
|
|
125 | 112 | "class Song(BaseModel):\n",
|
126 | 113 | " title: str\n",
|
127 | 114 | " artist: str\n",
|
128 |
| - " duration: float = Field(gt=0.0)\n", |
129 |
| - " release_date: date = Field(lt=date.today())\n", |
130 |
| - " beats_per_minute: int = Field(multiple_of=5)\n", |
| 115 | + " duration: float = Field(gt=0.0) # greater than 0\n", |
| 116 | + " release_date: date = Field(lt=date.today()) # before today\n", |
| 117 | + " beats_per_minute: int = Field(multiple_of=5) # multiple of 5\n", |
131 | 118 | "\n",
|
132 | 119 | "\n",
|
133 | 120 | "song1 = Song(\n",
|
134 | 121 | " title=\"Believer\",\n",
|
135 | 122 | " artist=\"Imagine Dragons\",\n",
|
136 |
| - " duration=3.67,\n", |
137 |
| - " release_date=date(2024, 6, 1), \n", |
138 |
| - " bpm=125,\n", |
| 123 | + " duration=0,\n", |
| 124 | + " release_date=date(2024, 6, 1),\n", |
| 125 | + " beats_per_minute=125,\n", |
139 | 126 | ")"
|
140 | 127 | ]
|
141 | 128 | },
|
142 | 129 | {
|
143 |
| - "cell_type": "code", |
144 |
| - "execution_count": 2, |
145 |
| - "id": "d05c1ff0", |
| 130 | + "cell_type": "markdown", |
| 131 | + "id": "e406a6af", |
146 | 132 | "metadata": {},
|
147 |
| - "outputs": [ |
148 |
| - { |
149 |
| - "ename": "ValidationError", |
150 |
| - "evalue": "2 validation errors for Song\nduration\n Input should be greater than 0 [type=greater_than, input_value=0, input_type=int]\n For further information visit https://errors.pydantic.dev/2.5/v/greater_than\nbeats_per_minute\n Field required [type=missing, input_value={'title': 'Thunder', 'art...017, 4, 27), 'bpm': 165}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.5/v/missing", |
151 |
| - "output_type": "error", |
152 |
| - "traceback": [ |
153 |
| - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
154 |
| - "\u001b[0;31mValidationError\u001b[0m Traceback (most recent call last)", |
155 |
| - "Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m song2 \u001b[38;5;241m=\u001b[39m \u001b[43mSong\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 2\u001b[0m \u001b[43m \u001b[49m\u001b[43mtitle\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mThunder\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[43m \u001b[49m\u001b[43martist\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mImagine Dragons\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 4\u001b[0m \u001b[43m \u001b[49m\u001b[43mduration\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 5\u001b[0m \u001b[43m \u001b[49m\u001b[43mrelease_date\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdate\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2017\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m27\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 6\u001b[0m \u001b[43m \u001b[49m\u001b[43mbpm\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m165\u001b[39;49m\n\u001b[1;32m 7\u001b[0m \u001b[43m)\u001b[49m\n", |
156 |
| - "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\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\u001b[1;32m 163\u001b[0m __tracebackhide__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\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", |
157 |
| - "\u001b[0;31mValidationError\u001b[0m: 2 validation errors for Song\nduration\n Input should be greater than 0 [type=greater_than, input_value=0, input_type=int]\n For further information visit https://errors.pydantic.dev/2.5/v/greater_than\nbeats_per_minute\n Field required [type=missing, input_value={'title': 'Thunder', 'art...017, 4, 27), 'bpm': 165}, input_type=dict]\n For further information visit https://errors.pydantic.dev/2.5/v/missing" |
158 |
| - ] |
159 |
| - } |
160 |
| - ], |
161 | 133 | "source": [
|
162 |
| - "song2 = Song(\n", |
163 |
| - " title=\"Thunder\",\n", |
164 |
| - " artist=\"Imagine Dragons\",\n", |
165 |
| - " duration=0,\n", |
166 |
| - " release_date=date(2017, 4, 27),\n", |
167 |
| - " bpm=165\n", |
168 |
| - ")" |
| 134 | + "```bash\n", |
| 135 | + "ValidationError: 2 validation errors for Song\n", |
| 136 | + "duration\n", |
| 137 | + " Input should be greater than 0 [type=greater_than, input_value=0, input_type=int]\n", |
| 138 | + " For further information visit https://errors.pydantic.dev/2.5/v/greater_than\n", |
| 139 | + "release_date\n", |
| 140 | + " Input should be less than 2024-05-10 [type=less_than, input_value=datetime.date(2024, 6, 1), input_type=date]\n", |
| 141 | + " For further information visit https://errors.pydantic.dev/2.5/v/less_than\n", |
| 142 | + "```" |
169 | 143 | ]
|
170 | 144 | },
|
171 | 145 | {
|
|
0 commit comments