|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "6677c98d", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Intro to Pydantic\n", |
| 9 | + "\n" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 23, |
| 15 | + "id": "9f8f418d", |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [ |
| 18 | + { |
| 19 | + "data": { |
| 20 | + "text/plain": [ |
| 21 | + "'1.10.11'" |
| 22 | + ] |
| 23 | + }, |
| 24 | + "execution_count": 23, |
| 25 | + "metadata": {}, |
| 26 | + "output_type": "execute_result" |
| 27 | + } |
| 28 | + ], |
| 29 | + "source": [ |
| 30 | + "# check pydantic version\n", |
| 31 | + "from pydantic import __version__\n", |
| 32 | + "\n", |
| 33 | + "# this was written for <v2\n", |
| 34 | + "__version__" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 18, |
| 40 | + "id": "758c0e5d", |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "from pydantic import BaseModel\n", |
| 45 | + "from datetime import datetime\n", |
| 46 | + "\n", |
| 47 | + "class User(BaseModel):\n", |
| 48 | + " id: int\n", |
| 49 | + " name = 'Jithin James'\n", |
| 50 | + " creation_time = datetime.now()" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": 21, |
| 56 | + "id": "ba7e8bd5", |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [ |
| 59 | + { |
| 60 | + "data": { |
| 61 | + "text/plain": [ |
| 62 | + "User(id=123, name='Jithin James', creation_time=datetime.datetime(2023, 8, 18, 17, 30, 37, 425005))" |
| 63 | + ] |
| 64 | + }, |
| 65 | + "execution_count": 21, |
| 66 | + "metadata": {}, |
| 67 | + "output_type": "execute_result" |
| 68 | + } |
| 69 | + ], |
| 70 | + "source": [ |
| 71 | + "user = User(id='123')\n", |
| 72 | + "user" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": 20, |
| 78 | + "id": "037fc368", |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [ |
| 81 | + { |
| 82 | + "ename": "ValidationError", |
| 83 | + "evalue": "1 validation error for User\nid\n value is not a valid integer (type=type_error.integer)", |
| 84 | + "output_type": "error", |
| 85 | + "traceback": [ |
| 86 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 87 | + "\u001b[0;31mValidationError\u001b[0m Traceback (most recent call last)", |
| 88 | + "Cell \u001b[0;32mIn[20], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mUser\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mid\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m123.43\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", |
| 89 | + "File \u001b[0;32m~/.pyenv/versions/3.10.12/envs/notes/lib/python3.10/site-packages/pydantic/main.py:341\u001b[0m, in \u001b[0;36mpydantic.main.BaseModel.__init__\u001b[0;34m()\u001b[0m\n", |
| 90 | + "\u001b[0;31mValidationError\u001b[0m: 1 validation error for User\nid\n value is not a valid integer (type=type_error.integer)" |
| 91 | + ] |
| 92 | + } |
| 93 | + ], |
| 94 | + "source": [ |
| 95 | + "User(id='123.43')" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": 22, |
| 101 | + "id": "955f4f4e", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [ |
| 104 | + { |
| 105 | + "data": { |
| 106 | + "text/plain": [ |
| 107 | + "'{\"id\": 123, \"name\": \"Jithin James\", \"creation_time\": \"2023-08-18T17:30:37.425005\"}'" |
| 108 | + ] |
| 109 | + }, |
| 110 | + "execution_count": 22, |
| 111 | + "metadata": {}, |
| 112 | + "output_type": "execute_result" |
| 113 | + } |
| 114 | + ], |
| 115 | + "source": [ |
| 116 | + "user.json()" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "id": "dd639848", |
| 122 | + "metadata": {}, |
| 123 | + "source": [ |
| 124 | + "The generated model support these methods\n", |
| 125 | + "\n", |
| 126 | + "- `dict()`: Returns a dictionary of the model's fields and values.\n", |
| 127 | + "- `json()`: Returns a JSON string representation of the model's dictionary.\n", |
| 128 | + "- `copy()`: Returns a copy (by default, shallow copy) of the model.\n", |
| 129 | + "- `parse_obj()`: A utility for loading any object into a model with error handling if the object is not a dictionary.\n", |
| 130 | + "- `parse_raw()`: A utility for loading strings of numerous formats.\n", |
| 131 | + "- `parse_file()`: Similar to `parse_raw()`, but for file paths.\n", |
| 132 | + "- `from_orm()`: Loads data into a model from an arbitrary class.\n", |
| 133 | + "- `schema()`: Returns a dictionary representing the model as JSON Schema.\n", |
| 134 | + "- `schema_json()`: Returns a JSON string representation of the model's schema.\n", |
| 135 | + "- `construct()`: A class method for creating models without running validation.\n", |
| 136 | + "- `__fields_set__`: Set of names of fields which were set when the model instance was initialized.\n", |
| 137 | + "- `__fields__`: A dictionary of the model's fields.\n", |
| 138 | + "- `__config__`: The configuration class for the model." |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": null, |
| 144 | + "id": "5117bd31", |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [] |
| 148 | + } |
| 149 | + ], |
| 150 | + "metadata": { |
| 151 | + "kernelspec": { |
| 152 | + "display_name": "Python 3 (ipykernel)", |
| 153 | + "language": "python", |
| 154 | + "name": "python3" |
| 155 | + }, |
| 156 | + "language_info": { |
| 157 | + "codemirror_mode": { |
| 158 | + "name": "ipython", |
| 159 | + "version": 3 |
| 160 | + }, |
| 161 | + "file_extension": ".py", |
| 162 | + "mimetype": "text/x-python", |
| 163 | + "name": "python", |
| 164 | + "nbconvert_exporter": "python", |
| 165 | + "pygments_lexer": "ipython3", |
| 166 | + "version": "3.10.12" |
| 167 | + } |
| 168 | + }, |
| 169 | + "nbformat": 4, |
| 170 | + "nbformat_minor": 5 |
| 171 | +} |
0 commit comments