|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 31, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "from typing import TypedDict\n", |
| 10 | + "\n" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 33, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "a = TypedDict(\"EntityType\", {\"Words\": str, \"Total Length\": int})" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "code", |
| 24 | + "execution_count": 39, |
| 25 | + "metadata": {}, |
| 26 | + "outputs": [], |
| 27 | + "source": [ |
| 28 | + "a.__repr__ = lambda x: \"A\"" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 48, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [ |
| 36 | + { |
| 37 | + "ename": "TypeError", |
| 38 | + "evalue": "dict expected at most 1 argument, got 2", |
| 39 | + "output_type": "error", |
| 40 | + "traceback": [ |
| 41 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 42 | + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", |
| 43 | + "\u001b[0;32m/var/folders/y0/fwkpk2ps087b_2qxvhjstrfr0000gn/T/ipykernel_57262/3336926982.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mTypedDict2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"EntityType\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m\"Words\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Total Length\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 44 | + "\u001b[0;31mTypeError\u001b[0m: dict expected at most 1 argument, got 2" |
| 45 | + ] |
| 46 | + } |
| 47 | + ], |
| 48 | + "source": [ |
| 49 | + "class TypedDict2(TypedDict):\n", |
| 50 | + " def __init__(self, typename, fields):\n", |
| 51 | + " super().__init__(typename, fields)\n", |
| 52 | + " def __repr__(self) -> str:\n", |
| 53 | + " return f\"CustomTypedDict({super().__repr__()})\"\n", |
| 54 | + " \n", |
| 55 | + " \n", |
| 56 | + "TypedDict2(\"EntityType\", {\"Words\": str, \"Total Length\": int})" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": null, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "\"Volume_per_Unit\"\n", |
| 66 | + "\n", |
| 67 | + "\"VolumeperUnit\"" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": 49, |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [ |
| 75 | + { |
| 76 | + "data": { |
| 77 | + "text/plain": [ |
| 78 | + "{'Words': str, 'Total Length': int}" |
| 79 | + ] |
| 80 | + }, |
| 81 | + "execution_count": 49, |
| 82 | + "metadata": {}, |
| 83 | + "output_type": "execute_result" |
| 84 | + } |
| 85 | + ], |
| 86 | + "source": [ |
| 87 | + "TypedDict2({\"Words\": str, \"Total Length\": int})" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": 50, |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "class Test(TypedDict):\n", |
| 97 | + " Words: str\n", |
| 98 | + " Total_Length: int" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": 47, |
| 104 | + "metadata": {}, |
| 105 | + "outputs": [ |
| 106 | + { |
| 107 | + "name": "stdout", |
| 108 | + "output_type": "stream", |
| 109 | + "text": [ |
| 110 | + "<class '__main__.Test'>\n" |
| 111 | + ] |
| 112 | + } |
| 113 | + ], |
| 114 | + "source": [ |
| 115 | + "print(Test)" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": null, |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [], |
| 123 | + "source": [] |
| 124 | + } |
| 125 | + ], |
| 126 | + "metadata": { |
| 127 | + "kernelspec": { |
| 128 | + "display_name": "BlockScience", |
| 129 | + "language": "python", |
| 130 | + "name": "python3" |
| 131 | + }, |
| 132 | + "language_info": { |
| 133 | + "codemirror_mode": { |
| 134 | + "name": "ipython", |
| 135 | + "version": 3 |
| 136 | + }, |
| 137 | + "file_extension": ".py", |
| 138 | + "mimetype": "text/x-python", |
| 139 | + "name": "python", |
| 140 | + "nbconvert_exporter": "python", |
| 141 | + "pygments_lexer": "ipython3", |
| 142 | + "version": "3.9.12" |
| 143 | + } |
| 144 | + }, |
| 145 | + "nbformat": 4, |
| 146 | + "nbformat_minor": 2 |
| 147 | +} |
0 commit comments