├── .gitignore ├── 1-decorators.ipynb ├── 2-magic-method_1.ipynb ├── 2-magic-method_2.ipynb ├── 3-generator-iterator.ipynb ├── 4-Singleton.ipynb ├── 5-abstract .ipynb ├── 6-Proxy.ipynb ├── README.md └── code └── 1-decorators.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | .git 3 | -------------------------------------------------------------------------------- /1-decorators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.6.4" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 2 32 | } 33 | -------------------------------------------------------------------------------- /2-magic-method_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# \\_\\_new\\_\\_与\\_\\_init\\_\\_" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 23, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "<__main__.Animal object at 0x000002BB02FE97F0> in new method.\n", 20 | "<__main__.Animal object at 0x000002BB02FE97F0> in init method.\n" 21 | ] 22 | } 23 | ], 24 | "source": [ 25 | "class Animal(object):\n", 26 | " def __new__(cls, *args, **kargs):\n", 27 | " instance = object.__new__(cls, *args, **kargs)\n", 28 | " print(\"{} in new method.\".format(instance))\n", 29 | " return instance\n", 30 | " \n", 31 | " def __init__(self):\n", 32 | " print(\"{} in init method.\".format(self))\n", 33 | " \n", 34 | "\n", 35 | "animal = Animal()" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "# 单例模式" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 10, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "name": "stdout", 52 | "output_type": "stream", 53 | "text": [ 54 | "<__main__.NewInt object at 0x000002BB03001390>\n", 55 | "<__main__.NewInt object at 0x000002BB02FF4080>\n" 56 | ] 57 | } 58 | ], 59 | "source": [ 60 | "class NewInt(object):\n", 61 | " pass\n", 62 | "\n", 63 | "new1 = NewInt()\n", 64 | "new2 = NewInt()\n", 65 | "print(new1)\n", 66 | "print(new2)" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 11, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "<__main__.NewInt object at 0x000002BB02FF6080>\n", 79 | "<__main__.NewInt object at 0x000002BB02FF6080>\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "class NewInt(object):\n", 85 | " _singleton = None\n", 86 | " def __new__(cls, *args, **kwargs):\n", 87 | " if not cls._singleton:\n", 88 | " cls._singleton = object.__new__(cls, *args, **kwargs)\n", 89 | " return cls._singleton\n", 90 | "\n", 91 | "new1 = NewInt()\n", 92 | "new2 = NewInt()\n", 93 | "print(new1)\n", 94 | "print(new2)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "# \\_\\_enter\\_\\_与\\_\\_exit\\_\\_" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": {}, 108 | "outputs": [], 109 | "source": [ 110 | "# file.txt\n", 111 | "\n", 112 | "fp = open(\"file.txt\", \"rb\")\n", 113 | "fp.readline()\n", 114 | "fp.close()" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "metadata": {}, 121 | "outputs": [], 122 | "source": [ 123 | "with open(\"file.txt\", \"rb\") as fp:\n", 124 | " fp.readline()" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 12, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "in init method\n", 137 | "int enter method\n", 138 | "in read\n", 139 | "in exit method\n" 140 | ] 141 | } 142 | ], 143 | "source": [ 144 | "class FileReader(object):\n", 145 | " def __init__(self):\n", 146 | " print(\"in init method\")\n", 147 | "\n", 148 | " def __enter__(self):\n", 149 | " print(\"int enter method\")\n", 150 | " return self\n", 151 | "\n", 152 | " def __exit__(self, exc_type, exc_val, exc_tb):\n", 153 | " print(\"in exit method\")\n", 154 | " del self\n", 155 | "\n", 156 | " def read(self):\n", 157 | " print(\"in read\")\n", 158 | " \n", 159 | "with FileReader() as fr:\n", 160 | " fr.read()" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "# \\_\\_str\\_\\_与\\_\\_repr\\_\\_" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 13, 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "name": "stdout", 177 | "output_type": "stream", 178 | "text": [ 179 | "str: Li now year is 27 years old.\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "class Person(object):\n", 185 | " def __init__(self, name, age):\n", 186 | " self.name = name\n", 187 | " self.age = age\n", 188 | "\n", 189 | " def __str__(self):\n", 190 | " return \"str: {} now year is {} years old.\".format(self.name, self.age)\n", 191 | "\n", 192 | " def __repr__(self):\n", 193 | " return \"repr: {} now year is {} years old.\".format(self.name, self.age)\n", 194 | "\n", 195 | "person = Person(\"Li\", 27)\n", 196 | "print(person)" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "# _\\_setattr\\_\\_、\\_\\_getattr\\_\\_、\\_\\_getattribute\\_\\_与_\\_delattr\\_\\_" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 15, 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "name": "stdout", 213 | "output_type": "stream", 214 | "text": [ 215 | "Li\n", 216 | "27\n" 217 | ] 218 | } 219 | ], 220 | "source": [ 221 | "class Person(object):\n", 222 | " def __init__(self, name, age, home, work):\n", 223 | " self.name = name\n", 224 | " self.age = age\n", 225 | " self.home = home\n", 226 | " self.work = work\n", 227 | "\n", 228 | "person = Person(\"Li\", 27, \"China\", \"Python\")\n", 229 | "print(person.name)\n", 230 | "print(person.age)" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 21, 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "name": "stdout", 240 | "output_type": "stream", 241 | "text": [ 242 | "in getattribute\n", 243 | "Li\n", 244 | "in getattribute\n", 245 | "in getattr\n", 246 | "Not find attribute: age\n" 247 | ] 248 | } 249 | ], 250 | "source": [ 251 | "class Person(object):\n", 252 | " def __init__(self, name):\n", 253 | " self.name = name\n", 254 | "\n", 255 | " def __setattr__(self, key, value):\n", 256 | " object.__setattr__(self, key, value)\n", 257 | "\n", 258 | " def __getattribute__(self, item):\n", 259 | " print(\"in getattribute\")\n", 260 | " return object.__getattribute__(self, item)\n", 261 | "\n", 262 | " def __getattr__(self, item):\n", 263 | " try:\n", 264 | " print(\"in getattr\")\n", 265 | " return object.__getattribute__(self, item)\n", 266 | " except:\n", 267 | " return \"Not find attribute: {}\".format(item)\n", 268 | "\n", 269 | " def __delattr__(self, item):\n", 270 | " print(\"in delattr\")\n", 271 | " object.__delattr__(self, item)\n", 272 | "\n", 273 | "\n", 274 | "person = Person(\"Li\")\n", 275 | "print(person.name)\n", 276 | "print(person.age)" 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": 22, 282 | "metadata": {}, 283 | "outputs": [ 284 | { 285 | "name": "stdout", 286 | "output_type": "stream", 287 | "text": [ 288 | "in getattribute\n", 289 | "27\n", 290 | "in delattr\n", 291 | "in getattribute\n", 292 | "in getattr\n", 293 | "Not find attribute: age\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "person.age = 27\n", 299 | "print(person.age)\n", 300 | "delattr(person, \"age\")\n", 301 | "print(person.age)" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": {}, 308 | "outputs": [], 309 | "source": [] 310 | } 311 | ], 312 | "metadata": { 313 | "kernelspec": { 314 | "display_name": "Python 3", 315 | "language": "python", 316 | "name": "python3" 317 | }, 318 | "language_info": { 319 | "codemirror_mode": { 320 | "name": "ipython", 321 | "version": 3 322 | }, 323 | "file_extension": ".py", 324 | "mimetype": "text/x-python", 325 | "name": "python", 326 | "nbconvert_exporter": "python", 327 | "pygments_lexer": "ipython3", 328 | "version": "3.6.4" 329 | } 330 | }, 331 | "nbformat": 4, 332 | "nbformat_minor": 2 333 | } 334 | -------------------------------------------------------------------------------- /2-magic-method_2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 函数调用" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 2, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "name": "stdout", 17 | "output_type": "stream", 18 | "text": [ 19 | "The result is 4\n" 20 | ] 21 | } 22 | ], 23 | "source": [ 24 | "class Operation(object):\n", 25 | " def __init__(self, x, y):\n", 26 | " self.x = x\n", 27 | " self.y = y\n", 28 | " \n", 29 | " def add(self):\n", 30 | " print(\"The result is {}\".format(self.x + self.y))\n", 31 | " \n", 32 | "opt = Operation(2, 2)\n", 33 | "opt.add()" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": 3, 39 | "metadata": {}, 40 | "outputs": [ 41 | { 42 | "name": "stdout", 43 | "output_type": "stream", 44 | "text": [ 45 | "The result is 6\n" 46 | ] 47 | } 48 | ], 49 | "source": [ 50 | "class Operation(object):\n", 51 | " def __init__(self):\n", 52 | " self.x = None\n", 53 | " self.y = None\n", 54 | " \n", 55 | " def add(self):\n", 56 | " print(\"The result is {}\".format(self.x + self.y))\n", 57 | " \n", 58 | " def __call__(self, x, y):\n", 59 | " self.x = x\n", 60 | " self.y = y\n", 61 | " self.add()\n", 62 | " \n", 63 | "opt = Operation()\n", 64 | "opt(3, 3)" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": {}, 70 | "source": [ 71 | "# 序列和容器" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 19, 77 | "metadata": {}, 78 | "outputs": [ 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "222222\n", 84 | "666666\n" 85 | ] 86 | }, 87 | { 88 | "data": { 89 | "text/plain": [ 90 | "4" 91 | ] 92 | }, 93 | "execution_count": 19, 94 | "metadata": {}, 95 | "output_type": "execute_result" 96 | } 97 | ], 98 | "source": [ 99 | "class Contain(object):\n", 100 | " def __init__(self, data):\n", 101 | " self.data = data\n", 102 | " \n", 103 | " def __contains__(self, x):\n", 104 | " return x in self.data\n", 105 | " \n", 106 | " def __len__(self):\n", 107 | " return len(self.data)\n", 108 | " \n", 109 | "contain = Contain([2,3,4,5])\n", 110 | "\n", 111 | "if 2 in contain:\n", 112 | " print(\"222222\")\n", 113 | " \n", 114 | "if 6 not in contain:\n", 115 | " print(\"666666\")\n", 116 | " \n", 117 | "len(contain)" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "# 算数运算" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": 42, 130 | "metadata": {}, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "the value if 8\n", 137 | "the value if 15\n" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "class Operation(object):\n", 143 | " def __init__(self, value):\n", 144 | " self.value = value\n", 145 | " \n", 146 | " def __add__(self, other):\n", 147 | " return Operation(self.value + other.value)\n", 148 | " \n", 149 | " def __mul__(self, other):\n", 150 | " return Operation(self.value * other.value)\n", 151 | " \n", 152 | " def __str__(self):\n", 153 | " return \"the value if {}\".format(self.value)\n", 154 | " \n", 155 | "a = Operation(3)\n", 156 | "b = Operation(5)\n", 157 | "print(a + b)\n", 158 | "print(a * b)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "# 比较运算" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 45, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "data": { 175 | "text/plain": [ 176 | "True" 177 | ] 178 | }, 179 | "execution_count": 45, 180 | "metadata": {}, 181 | "output_type": "execute_result" 182 | } 183 | ], 184 | "source": [ 185 | "class Cmp(object):\n", 186 | " def __init__(self, value):\n", 187 | " self.value = value\n", 188 | " \n", 189 | " def __eq__(self, other):\n", 190 | " return self.value == other.value\n", 191 | " \n", 192 | " def __gt__(self, other):\n", 193 | " return self.value > other.value\n", 194 | "a = Cmp(3)\n", 195 | "b = Cmp(3)\n", 196 | "a == b" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "# 字段功能" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 61, 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "data": { 213 | "text/plain": [ 214 | "3" 215 | ] 216 | }, 217 | "execution_count": 61, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "class Dictionaries(object):\n", 224 | " def __setitem__(self, key, value):\n", 225 | " self.__dict__[key] = value\n", 226 | " \n", 227 | " def __getitem__(self, key):\n", 228 | " return self.__dict__[key]\n", 229 | " \n", 230 | " def __delitem__(self, key):\n", 231 | " del self.__dict__[key]\n", 232 | "\n", 233 | "diction = Dictionaries()\n", 234 | "diction[\"one\"] = 1\n", 235 | "diction[\"two\"] = 2\n", 236 | "diction[\"three\"] = 3\n", 237 | "diction['three']\n", 238 | "del diction['three']\n", 239 | "diction['three']" 240 | ] 241 | }, 242 | { 243 | "cell_type": "code", 244 | "execution_count": 56, 245 | "metadata": {}, 246 | "outputs": [], 247 | "source": [] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 57, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "ename": "KeyError", 256 | "evalue": "'three'", 257 | "output_type": "error", 258 | "traceback": [ 259 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 260 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", 261 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdiction\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'three'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 262 | "\u001b[1;32m\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m__getitem__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__dict__\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m__delitem__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 263 | "\u001b[1;31mKeyError\u001b[0m: 'three'" 264 | ] 265 | } 266 | ], 267 | "source": [] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 54, 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "data": { 276 | "text/plain": [ 277 | "3" 278 | ] 279 | }, 280 | "execution_count": 54, 281 | "metadata": {}, 282 | "output_type": "execute_result" 283 | } 284 | ], 285 | "source": [] 286 | }, 287 | { 288 | "cell_type": "code", 289 | "execution_count": null, 290 | "metadata": {}, 291 | "outputs": [], 292 | "source": [] 293 | } 294 | ], 295 | "metadata": { 296 | "kernelspec": { 297 | "display_name": "Python 3", 298 | "language": "python", 299 | "name": "python3" 300 | }, 301 | "language_info": { 302 | "codemirror_mode": { 303 | "name": "ipython", 304 | "version": 3 305 | }, 306 | "file_extension": ".py", 307 | "mimetype": "text/x-python", 308 | "name": "python", 309 | "nbconvert_exporter": "python", 310 | "pygments_lexer": "ipython3", 311 | "version": "3.6.4" 312 | }, 313 | "toc": { 314 | "base_numbering": 1, 315 | "nav_menu": {}, 316 | "number_sections": true, 317 | "sideBar": true, 318 | "skip_h1_title": false, 319 | "title_cell": "Table of Contents", 320 | "title_sidebar": "Contents", 321 | "toc_cell": false, 322 | "toc_position": {}, 323 | "toc_section_display": true, 324 | "toc_window_display": false 325 | } 326 | }, 327 | "nbformat": 4, 328 | "nbformat_minor": 2 329 | } 330 | -------------------------------------------------------------------------------- /3-generator-iterator.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 可迭代对象" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "ExecuteTime": { 15 | "end_time": "2019-10-08T14:13:03.525627Z", 16 | "start_time": "2019-10-08T14:13:03.508675Z" 17 | } 18 | }, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "{1, 2, 3, 4, 5}\n", 25 | "\n", 26 | "True\n", 27 | "False\n", 28 | "1\n", 29 | "2\n", 30 | "3\n", 31 | "4\n", 32 | "5\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "X = set([1,2,3,4,5])\n", 38 | "print(X)\n", 39 | "print(type(X))\n", 40 | "print(1 in X)\n", 41 | "print(2 not in X)\n", 42 | "for x in X:\n", 43 | " print(x)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "metadata": { 50 | "ExecuteTime": { 51 | "end_time": "2019-10-08T14:13:11.568759Z", 52 | "start_time": "2019-10-08T14:13:11.464038Z" 53 | } 54 | }, 55 | "outputs": [ 56 | { 57 | "ename": "TypeError", 58 | "evalue": "'set' object is not an iterator", 59 | "output_type": "error", 60 | "traceback": [ 61 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 62 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 63 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 64 | "\u001b[1;31mTypeError\u001b[0m: 'set' object is not an iterator" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "next(X)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "# 迭代器" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 3, 82 | "metadata": { 83 | "ExecuteTime": { 84 | "end_time": "2019-10-08T14:13:29.961408Z", 85 | "start_time": "2019-10-08T14:13:29.954396Z" 86 | } 87 | }, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "\n", 94 | "\n", 95 | "1\n", 96 | "2\n", 97 | "3\n" 98 | ] 99 | } 100 | ], 101 | "source": [ 102 | "X = [1,2,3,4,5]\n", 103 | "print(type(X))\n", 104 | "Y = iter(X)\n", 105 | "print(type(Y))\n", 106 | "print(next(Y))\n", 107 | "print(next(Y))\n", 108 | "print(next(Y))" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": 4, 114 | "metadata": { 115 | "ExecuteTime": { 116 | "end_time": "2019-10-08T14:13:39.166029Z", 117 | "start_time": "2019-10-08T14:13:39.157053Z" 118 | } 119 | }, 120 | "outputs": [ 121 | { 122 | "name": "stdout", 123 | "output_type": "stream", 124 | "text": [ 125 | "\n", 126 | "1\n", 127 | "2\n", 128 | "3\n", 129 | "4\n", 130 | "5\n" 131 | ] 132 | } 133 | ], 134 | "source": [ 135 | "class Iterator(object):\n", 136 | " def __init__(self, array):\n", 137 | " self.x = array\n", 138 | " self.index = 0\n", 139 | " \n", 140 | " def __iter__(self):\n", 141 | " return self\n", 142 | " \n", 143 | " def __next__(self):\n", 144 | " if self.index < len(self.x):\n", 145 | " value = self.x[self.index]\n", 146 | " self.index += 1\n", 147 | " else:\n", 148 | " raise StopIteration\n", 149 | " return value\n", 150 | " \n", 151 | "it = Iterator([1,2,3,4,5])\n", 152 | "print(type(it))\n", 153 | "for i in it:\n", 154 | " print(i)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "# 生成器" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 5, 167 | "metadata": { 168 | "ExecuteTime": { 169 | "end_time": "2019-10-08T14:13:55.426637Z", 170 | "start_time": "2019-10-08T14:13:55.421651Z" 171 | } 172 | }, 173 | "outputs": [ 174 | { 175 | "name": "stdout", 176 | "output_type": "stream", 177 | "text": [ 178 | "\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "def generator(array):\n", 184 | " for i in array:\n", 185 | " return i\n", 186 | " \n", 187 | "gen = generator([1,2,3,4,5])\n", 188 | "print(type(gen))" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 6, 194 | "metadata": { 195 | "ExecuteTime": { 196 | "end_time": "2019-10-08T14:14:00.251184Z", 197 | "start_time": "2019-10-08T14:14:00.246198Z" 198 | } 199 | }, 200 | "outputs": [ 201 | { 202 | "name": "stdout", 203 | "output_type": "stream", 204 | "text": [ 205 | "\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "def generator(array):\n", 211 | " for i in array:\n", 212 | " yield(i)\n", 213 | " \n", 214 | "gen = generator([1,2,3,4,5])\n", 215 | "print(type(gen))" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 7, 221 | "metadata": { 222 | "ExecuteTime": { 223 | "end_time": "2019-10-08T14:14:06.166281Z", 224 | "start_time": "2019-10-08T14:14:06.161295Z" 225 | } 226 | }, 227 | "outputs": [], 228 | "source": [ 229 | "def generator(array):\n", 230 | " for sub_array in array:\n", 231 | " yield from sub_array\n", 232 | "\n", 233 | "gen = generator([(1,2,3), (4,5,6,7)])" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 8, 239 | "metadata": { 240 | "ExecuteTime": { 241 | "end_time": "2019-10-08T14:14:10.897919Z", 242 | "start_time": "2019-10-08T14:14:10.891936Z" 243 | } 244 | }, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "1\n", 251 | "2\n" 252 | ] 253 | } 254 | ], 255 | "source": [ 256 | "print(next(gen))\n", 257 | "print(next(gen))" 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": {}, 263 | "source": [ 264 | "# 生成器表达式" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 9, 270 | "metadata": { 271 | "ExecuteTime": { 272 | "end_time": "2019-10-08T14:14:29.391632Z", 273 | "start_time": "2019-10-08T14:14:29.384650Z" 274 | } 275 | }, 276 | "outputs": [ 277 | { 278 | "name": "stdout", 279 | "output_type": "stream", 280 | "text": [ 281 | "\n", 282 | "\n", 283 | "\n" 284 | ] 285 | } 286 | ], 287 | "source": [ 288 | "X = [1, 2, 3, 4, 5]\n", 289 | "it = [i for i in X]\n", 290 | "gen = (i for i in X)\n", 291 | "print(type(X))\n", 292 | "print(type(it))\n", 293 | "print(type(gen))" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [] 302 | } 303 | ], 304 | "metadata": { 305 | "kernelspec": { 306 | "display_name": "Python 3", 307 | "language": "python", 308 | "name": "python3" 309 | }, 310 | "language_info": { 311 | "codemirror_mode": { 312 | "name": "ipython", 313 | "version": 3 314 | }, 315 | "file_extension": ".py", 316 | "mimetype": "text/x-python", 317 | "name": "python", 318 | "nbconvert_exporter": "python", 319 | "pygments_lexer": "ipython3", 320 | "version": "3.6.4" 321 | }, 322 | "toc": { 323 | "base_numbering": 1, 324 | "nav_menu": {}, 325 | "number_sections": true, 326 | "sideBar": true, 327 | "skip_h1_title": false, 328 | "title_cell": "Table of Contents", 329 | "title_sidebar": "Contents", 330 | "toc_cell": false, 331 | "toc_position": {}, 332 | "toc_section_display": true, 333 | "toc_window_display": false 334 | } 335 | }, 336 | "nbformat": 4, 337 | "nbformat_minor": 2 338 | } 339 | -------------------------------------------------------------------------------- /4-Singleton.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 普通软件设计模式" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "ExecuteTime": { 15 | "end_time": "2019-10-19T12:42:10.663811Z", 16 | "start_time": "2019-10-19T12:42:10.654832Z" 17 | } 18 | }, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "2538846619576\n", 25 | "2538846620024\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "class Software(object):\n", 31 | " def __init__(self):\n", 32 | " pass\n", 33 | "\n", 34 | "soft1 = Software()\n", 35 | "soft2 = Software()\n", 36 | "print(id(soft1))\n", 37 | "print(id(soft2))" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "# python单例模式" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": { 50 | "ExecuteTime": { 51 | "end_time": "2019-10-19T13:06:49.376959Z", 52 | "start_time": "2019-10-19T13:06:49.373008Z" 53 | } 54 | }, 55 | "source": [ 56 | "## \\_\\_new\\_\\_" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 37, 62 | "metadata": { 63 | "ExecuteTime": { 64 | "end_time": "2019-10-19T14:07:51.823209Z", 65 | "start_time": "2019-10-19T14:07:51.815230Z" 66 | } 67 | }, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "2538877093648\n", 74 | "2538877093648\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "class Singleton(object):\n", 80 | " def __new__(cls, *args, **kw):\n", 81 | " if not hasattr(cls, '_instance'):\n", 82 | " orig = super(Singleton, cls)\n", 83 | " cls._instance = orig.__new__(cls)\n", 84 | " return cls._instance\n", 85 | " \n", 86 | "class Books(Singleton):\n", 87 | " def __init__(self):\n", 88 | " pass\n", 89 | " \n", 90 | "book1 = Books()\n", 91 | "book2 = Books()\n", 92 | "print(id(book1))\n", 93 | "print(id(book2))" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "## 装饰器" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 38, 106 | "metadata": { 107 | "ExecuteTime": { 108 | "end_time": "2019-10-19T14:07:52.516478Z", 109 | "start_time": "2019-10-19T14:07:52.506505Z" 110 | } 111 | }, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "2538874966872\n", 118 | "2538874966872\n" 119 | ] 120 | } 121 | ], 122 | "source": [ 123 | "def singleton(cls, *args, **kw): \n", 124 | " instances = {} \n", 125 | " def wrapper(): \n", 126 | " if cls not in instances: \n", 127 | " instances[cls] = cls(*args, **kw) \n", 128 | " return instances[cls] \n", 129 | " return wrapper \n", 130 | "\n", 131 | "@singleton\n", 132 | "class Animal(object):\n", 133 | " def __init__(self):\n", 134 | " pass\n", 135 | " \n", 136 | "animal1 = Animal()\n", 137 | "animal2 = Animal()\n", 138 | "print(id(animal1))\n", 139 | "print(id(animal2))" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "## metaclass" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 39, 152 | "metadata": { 153 | "ExecuteTime": { 154 | "end_time": "2019-10-19T14:07:52.976151Z", 155 | "start_time": "2019-10-19T14:07:52.967174Z" 156 | } 157 | }, 158 | "outputs": [ 159 | { 160 | "name": "stdout", 161 | "output_type": "stream", 162 | "text": [ 163 | "2538875293880\n", 164 | "2538875297632\n" 165 | ] 166 | } 167 | ], 168 | "source": [ 169 | "class Singleton(type): \n", 170 | " def __init__(cls, name, bases, dict): \n", 171 | " super(Singleton, cls).__init__(name, bases, dict) \n", 172 | " cls.instance = None \n", 173 | " \n", 174 | " def __call__(cls, *args, **kw): \n", 175 | " if cls.instance is None: \n", 176 | " cls.instance = super(Singleton, cls).__call__(*args, **kw) \n", 177 | " return cls.instance \n", 178 | " \n", 179 | "class MyClass(object): \n", 180 | " __metaclass__ = Singleton \n", 181 | " \n", 182 | "print(id(MyClass()))\n", 183 | "print(id(MyClass())) " 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": {}, 189 | "source": [ 190 | "# Usage" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 40, 196 | "metadata": { 197 | "ExecuteTime": { 198 | "end_time": "2019-10-19T14:07:53.717909Z", 199 | "start_time": "2019-10-19T14:07:53.710926Z" 200 | } 201 | }, 202 | "outputs": [], 203 | "source": [ 204 | "class SqlClient(object):\n", 205 | " def __init__(self, host, user, passwd):\n", 206 | " self.host = host\n", 207 | " self.user = user\n", 208 | " self.passwd = passwd\n", 209 | " self.register()\n", 210 | " \n", 211 | " def register(self):\n", 212 | " self.info = \"{}--{}---{}\".format(self.host, self.user, self.passwd)\n", 213 | " \n", 214 | " def select(self):\n", 215 | " print(\"SELECT * FROM {}\".format(self.host))" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "## 反复实例化" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 41, 228 | "metadata": { 229 | "ExecuteTime": { 230 | "end_time": "2019-10-19T14:07:57.968879Z", 231 | "start_time": "2019-10-19T14:07:57.960902Z" 232 | } 233 | }, 234 | "outputs": [ 235 | { 236 | "name": "stdout", 237 | "output_type": "stream", 238 | "text": [ 239 | "SELECT * FROM 10.293.291.19\n", 240 | "SELECT * FROM 10.293.291.19\n", 241 | "SELECT * FROM 10.293.291.19\n" 242 | ] 243 | } 244 | ], 245 | "source": [ 246 | "host = \"10.293.291.19\"\n", 247 | "user = \"admin\"\n", 248 | "passwd = \"666666\"\n", 249 | "def use_data_1():\n", 250 | " sql_client = SqlClient(host, user, passwd)\n", 251 | " sql_client.select()\n", 252 | " \n", 253 | "def use_data_2():\n", 254 | " sql_client = SqlClient(host, user, passwd)\n", 255 | " sql_client.select()\n", 256 | "\n", 257 | "def use_data_3():\n", 258 | " sql_client = SqlClient(host, user, passwd)\n", 259 | " sql_client.select()\n", 260 | " \n", 261 | "use_data_1()\n", 262 | "use_data_2()\n", 263 | "use_data_3()" 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": {}, 269 | "source": [ 270 | "## 把实例化后的对象作为参数传入到每个用到select的函数里" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 42, 276 | "metadata": { 277 | "ExecuteTime": { 278 | "end_time": "2019-10-19T14:07:58.711033Z", 279 | "start_time": "2019-10-19T14:07:58.703054Z" 280 | } 281 | }, 282 | "outputs": [ 283 | { 284 | "name": "stdout", 285 | "output_type": "stream", 286 | "text": [ 287 | "SELECT * FROM 10.293.291.19\n", 288 | "SELECT * FROM 10.293.291.19\n", 289 | "SELECT * FROM 10.293.291.19\n" 290 | ] 291 | } 292 | ], 293 | "source": [ 294 | "host = \"10.293.291.19\"\n", 295 | "user = \"admin\"\n", 296 | "passwd = \"666666\"\n", 297 | "def use_data_1(sql_client):\n", 298 | " sql_client.select()\n", 299 | " \n", 300 | "def use_data_2(sql_client):\n", 301 | " sql_client.select()\n", 302 | "\n", 303 | "def use_data_3(sql_client):\n", 304 | " sql_client.select()\n", 305 | " \n", 306 | "sql_client = SqlClient(host, user, passwd)\n", 307 | "use_data_1(sql_client)\n", 308 | "use_data_2(sql_client)\n", 309 | "use_data_3(sql_client)" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 43, 315 | "metadata": { 316 | "ExecuteTime": { 317 | "end_time": "2019-10-19T14:07:59.638804Z", 318 | "start_time": "2019-10-19T14:07:59.630819Z" 319 | } 320 | }, 321 | "outputs": [ 322 | { 323 | "name": "stdout", 324 | "output_type": "stream", 325 | "text": [ 326 | "SELECT * FROM 10.293.291.19\n", 327 | "SELECT * FROM 10.293.291.19\n" 328 | ] 329 | } 330 | ], 331 | "source": [ 332 | "host = \"10.293.291.19\"\n", 333 | "user = \"admin\"\n", 334 | "passwd = \"666666\"\n", 335 | "def use_data_1(sql_client):\n", 336 | " sql_client.select()\n", 337 | " use_data_2(sql_client)\n", 338 | " \n", 339 | "def use_data_2(sql_client):\n", 340 | " use_data_3(sql_client)\n", 341 | "\n", 342 | "def use_data_3(sql_client):\n", 343 | " sql_client.select()\n", 344 | " \n", 345 | "sql_client = SqlClient(host, user, passwd)\n", 346 | "use_data_1(sql_client)" 347 | ] 348 | }, 349 | { 350 | "cell_type": "markdown", 351 | "metadata": {}, 352 | "source": [ 353 | "## 单例模式" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 50, 359 | "metadata": { 360 | "ExecuteTime": { 361 | "end_time": "2019-10-19T14:10:54.315205Z", 362 | "start_time": "2019-10-19T14:10:54.308271Z" 363 | } 364 | }, 365 | "outputs": [], 366 | "source": [ 367 | "class Singleton(object):\n", 368 | " def __new__(cls, *args, **kw):\n", 369 | " if not hasattr(cls, '_instance'):\n", 370 | " orig = super(Singleton, cls)\n", 371 | " cls._instance = orig.__new__(cls)\n", 372 | " return cls._instance\n", 373 | " \n", 374 | "class SqlClient(Singleton):\n", 375 | " info = None\n", 376 | " \n", 377 | " def register(self, host, user, passwd):\n", 378 | " self.info = \"{}--{}--{}\".format(host, user, passwd)\n", 379 | " \n", 380 | " def select(self):\n", 381 | " print(self.info)" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": 51, 387 | "metadata": { 388 | "ExecuteTime": { 389 | "end_time": "2019-10-19T14:10:56.520947Z", 390 | "start_time": "2019-10-19T14:10:56.515994Z" 391 | } 392 | }, 393 | "outputs": [], 394 | "source": [ 395 | "SqlClient().register(host, user, passwd)" 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "execution_count": 55, 401 | "metadata": { 402 | "ExecuteTime": { 403 | "end_time": "2019-10-19T14:12:25.902187Z", 404 | "start_time": "2019-10-19T14:12:25.895207Z" 405 | } 406 | }, 407 | "outputs": [ 408 | { 409 | "name": "stdout", 410 | "output_type": "stream", 411 | "text": [ 412 | "10.293.291.19--admin--666666\n", 413 | "10.293.291.19--admin--666666\n", 414 | "10.293.291.19--admin--666666\n" 415 | ] 416 | } 417 | ], 418 | "source": [ 419 | "def use_data_1():\n", 420 | " SqlClient().select()\n", 421 | "\n", 422 | "def use_data_2():\n", 423 | " SqlClient().select()\n", 424 | " \n", 425 | "def use_data_3():\n", 426 | " SqlClient().select()\n", 427 | "\n", 428 | "use_data_1()\n", 429 | "use_data_2()\n", 430 | "use_data_3()" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": null, 436 | "metadata": {}, 437 | "outputs": [], 438 | "source": [] 439 | } 440 | ], 441 | "metadata": { 442 | "kernelspec": { 443 | "display_name": "Python 3", 444 | "language": "python", 445 | "name": "python3" 446 | }, 447 | "language_info": { 448 | "codemirror_mode": { 449 | "name": "ipython", 450 | "version": 3 451 | }, 452 | "file_extension": ".py", 453 | "mimetype": "text/x-python", 454 | "name": "python", 455 | "nbconvert_exporter": "python", 456 | "pygments_lexer": "ipython3", 457 | "version": "3.6.4" 458 | }, 459 | "toc": { 460 | "base_numbering": 1, 461 | "nav_menu": {}, 462 | "number_sections": true, 463 | "sideBar": true, 464 | "skip_h1_title": false, 465 | "title_cell": "Table of Contents", 466 | "title_sidebar": "Contents", 467 | "toc_cell": false, 468 | "toc_position": {}, 469 | "toc_section_display": true, 470 | "toc_window_display": false 471 | } 472 | }, 473 | "nbformat": 4, 474 | "nbformat_minor": 2 475 | } 476 | -------------------------------------------------------------------------------- /5-abstract .ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 继承" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 3, 13 | "metadata": { 14 | "ExecuteTime": { 15 | "end_time": "2019-10-27T11:19:36.768353Z", 16 | "start_time": "2019-10-27T11:19:36.759375Z" 17 | } 18 | }, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "dog eat food....\n", 25 | "cat eat food....\n" 26 | ] 27 | } 28 | ], 29 | "source": [ 30 | "class Animal(object):\n", 31 | " def eat(self, kind):\n", 32 | " print(\"{} eat food....\".format(kind))\n", 33 | "\n", 34 | "class Dog(Animal):\n", 35 | " pass\n", 36 | "\n", 37 | "class Cat(Animal):\n", 38 | " pass\n", 39 | " \n", 40 | "dog = Dog()\n", 41 | "cat = Cat()\n", 42 | "dog.eat(\"dog\")\n", 43 | "cat.eat(\"cat\")" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "# 抽象基类" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": { 57 | "ExecuteTime": { 58 | "end_time": "2019-10-27T12:36:42.605836Z", 59 | "start_time": "2019-10-27T12:36:42.573922Z" 60 | } 61 | }, 62 | "outputs": [ 63 | { 64 | "name": "stdout", 65 | "output_type": "stream", 66 | "text": [ 67 | "Host : 00.00.00.00\n", 68 | "User : abc\n", 69 | "Password : 000000\n", 70 | "Register Success!\n", 71 | "Host : 11.11.11.11\n", 72 | "User : ABC\n", 73 | "Password : 111111\n", 74 | "Register Success!\n", 75 | "SELECT ID FROM db_name\n", 76 | "SELECT NAME FROM db_name\n" 77 | ] 78 | } 79 | ], 80 | "source": [ 81 | "from abc import ABC\n", 82 | "from abc import abstractmethod\n", 83 | "\n", 84 | "\n", 85 | "class Database(ABC):\n", 86 | " def register(self, host, user, password):\n", 87 | " print(\"Host : {}\".format(host))\n", 88 | " print(\"User : {}\".format(user))\n", 89 | " print(\"Password : {}\".format(password))\n", 90 | " print(\"Register Success!\")\n", 91 | "\n", 92 | " @abstractmethod\n", 93 | " def query(self, *args):\n", 94 | " \"\"\"\n", 95 | " 传入查询数据的SQL语句并执行\n", 96 | " \"\"\"\n", 97 | "\n", 98 | " @staticmethod\n", 99 | " @abstractmethod\n", 100 | " def execute(sql_string):\n", 101 | " \"\"\"\n", 102 | " 执行SQL语句\n", 103 | " \"\"\"\n", 104 | "\n", 105 | "\n", 106 | "class Component1(Database):\n", 107 | " def __init__(self, host, user, password):\n", 108 | " self.register(host, user, password)\n", 109 | "\n", 110 | " @staticmethod\n", 111 | " def execute(sql_string):\n", 112 | " print(sql_string)\n", 113 | "\n", 114 | " def query(self, *args):\n", 115 | " sql_string = \"SELECT ID FROM db_name\"\n", 116 | " self.execute(sql_string)\n", 117 | "\n", 118 | "\n", 119 | "class Component2(Database):\n", 120 | " def __init__(self, host, user, password):\n", 121 | " self.register(host, user, password)\n", 122 | "\n", 123 | " @staticmethod\n", 124 | " def execute(sql_string):\n", 125 | " print(sql_string)\n", 126 | "\n", 127 | " def query(self, *args):\n", 128 | " sql_string = \"SELECT NAME FROM db_name\"\n", 129 | " self.execute(sql_string)\n", 130 | "\n", 131 | "comp1 = Component1(\"00.00.00.00\", \"abc\", \"000000\")\n", 132 | "comp2 = Component2(\"11.11.11.11\", \"ABC\", \"111111\")\n", 133 | "comp1.query()\n", 134 | "comp2.query()" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "metadata": {}, 141 | "outputs": [], 142 | "source": [] 143 | } 144 | ], 145 | "metadata": { 146 | "kernelspec": { 147 | "display_name": "Python 3", 148 | "language": "python", 149 | "name": "python3" 150 | }, 151 | "language_info": { 152 | "codemirror_mode": { 153 | "name": "ipython", 154 | "version": 3 155 | }, 156 | "file_extension": ".py", 157 | "mimetype": "text/x-python", 158 | "name": "python", 159 | "nbconvert_exporter": "python", 160 | "pygments_lexer": "ipython3", 161 | "version": "3.6.4" 162 | }, 163 | "toc": { 164 | "base_numbering": 1, 165 | "nav_menu": {}, 166 | "number_sections": true, 167 | "sideBar": true, 168 | "skip_h1_title": false, 169 | "title_cell": "Table of Contents", 170 | "title_sidebar": "Contents", 171 | "toc_cell": false, 172 | "toc_position": {}, 173 | "toc_section_display": true, 174 | "toc_window_display": false 175 | } 176 | }, 177 | "nbformat": 4, 178 | "nbformat_minor": 2 179 | } 180 | -------------------------------------------------------------------------------- /6-Proxy.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "ExecuteTime": { 8 | "end_time": "2019-11-15T15:23:45.545203Z", 9 | "start_time": "2019-11-15T15:23:45.539220Z" 10 | } 11 | }, 12 | "outputs": [], 13 | "source": [ 14 | "class NotFindError(Exception):\n", 15 | " def __init__(self, msg):\n", 16 | " self.msg = msg" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": { 23 | "ExecuteTime": { 24 | "end_time": "2019-11-15T15:23:51.734064Z", 25 | "start_time": "2019-11-15T15:23:51.728162Z" 26 | } 27 | }, 28 | "outputs": [], 29 | "source": [ 30 | "class RealSubject(object):\n", 31 | " def __init__(self):\n", 32 | " self.score = {\n", 33 | " \"张三\": 90,\n", 34 | " \"李四\": 59,\n", 35 | " \"王二\": 61\n", 36 | " }\n", 37 | "\n", 38 | " def num_students(self):\n", 39 | " num = len(self.score.keys())\n", 40 | " print(\"The number of students is {num}\".format(num=num))\n", 41 | "\n", 42 | " def get_score(self, user_name):\n", 43 | " _score = self.score.get(user_name)\n", 44 | " print(\"The score of {user} is {score}\".format(user=user_name,\n", 45 | " score=_score))" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 3, 51 | "metadata": { 52 | "ExecuteTime": { 53 | "end_time": "2019-11-15T15:24:00.634526Z", 54 | "start_time": "2019-11-15T15:24:00.626550Z" 55 | } 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "class Proxy(object):\n", 60 | " def __init__(self):\n", 61 | " self.default_passwd = \"9l0skjlsa\"\n", 62 | " self.real_subject = RealSubject()\n", 63 | "\n", 64 | " def num_students(self):\n", 65 | " self.real_subject.num_students()\n", 66 | "\n", 67 | " def get_score(self, user_name):\n", 68 | " print(\"You are visiting {} score ...\".format(user_name))\n", 69 | " passwd = input(\"Please input password : \")\n", 70 | " if passwd == self.default_passwd:\n", 71 | " if user_name in self.real_subject.score.keys():\n", 72 | " return self.real_subject.get_score(user_name)\n", 73 | " else:\n", 74 | " raise NotFindError(\"The student you are visiting not found.\")\n", 75 | " else:\n", 76 | " raise ValueError(\"The password you provided is wrong!\")" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "# 密码错误,用户名正确" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 5, 89 | "metadata": { 90 | "ExecuteTime": { 91 | "end_time": "2019-11-15T15:25:57.105496Z", 92 | "start_time": "2019-11-15T15:24:44.408609Z" 93 | } 94 | }, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "You are visiting 张三 score ...\n", 101 | "Please input password : kdksla\n" 102 | ] 103 | }, 104 | { 105 | "ename": "ValueError", 106 | "evalue": "The password you provided is wrong!", 107 | "output_type": "error", 108 | "traceback": [ 109 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 110 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 111 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mproxy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mProxy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mproxy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_score\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"张三\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mclient\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 112 | "\u001b[1;32m\u001b[0m in \u001b[0;36mclient\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mclient\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mproxy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mProxy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mproxy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_score\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"张三\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mclient\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 113 | "\u001b[1;32m\u001b[0m in \u001b[0;36mget_score\u001b[1;34m(self, user_name)\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mNotFindError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"The student you are visiting not found.\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 18\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"The password you provided is wrong!\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 114 | "\u001b[1;31mValueError\u001b[0m: The password you provided is wrong!" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "def client():\n", 120 | " proxy = Proxy()\n", 121 | " proxy.get_score(\"张三\")\n", 122 | "client()" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "# 密码正确,用户名错误" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 7, 135 | "metadata": { 136 | "ExecuteTime": { 137 | "end_time": "2019-11-15T15:26:42.985098Z", 138 | "start_time": "2019-11-15T15:26:36.436174Z" 139 | }, 140 | "scrolled": true 141 | }, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "You are visiting 李三 score ...\n", 148 | "Please input password : 9l0skjlsa\n" 149 | ] 150 | }, 151 | { 152 | "ename": "NotFindError", 153 | "evalue": "The student you are visiting not found.", 154 | "output_type": "error", 155 | "traceback": [ 156 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 157 | "\u001b[1;31mNotFindError\u001b[0m Traceback (most recent call last)", 158 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mproxy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mProxy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mproxy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_score\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"李三\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mclient\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 159 | "\u001b[1;32m\u001b[0m in \u001b[0;36mclient\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mclient\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mproxy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mProxy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mproxy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_score\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"李三\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mclient\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 160 | "\u001b[1;32m\u001b[0m in \u001b[0;36mget_score\u001b[1;34m(self, user_name)\u001b[0m\n\u001b[0;32m 14\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreal_subject\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_score\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0muser_name\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mNotFindError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"The student you are visiting not found.\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 17\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"The password you provided is wrong!\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", 161 | "\u001b[1;31mNotFindError\u001b[0m: The student you are visiting not found." 162 | ] 163 | } 164 | ], 165 | "source": [ 166 | "def client():\n", 167 | " proxy = Proxy()\n", 168 | " proxy.get_score(\"李三\")\n", 169 | "client()" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "# 密码正确,用户名正确" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 8, 182 | "metadata": { 183 | "ExecuteTime": { 184 | "end_time": "2019-11-15T15:28:27.705145Z", 185 | "start_time": "2019-11-15T15:28:22.455934Z" 186 | } 187 | }, 188 | "outputs": [ 189 | { 190 | "name": "stdout", 191 | "output_type": "stream", 192 | "text": [ 193 | "You are visiting 李四 score ...\n", 194 | "Please input password : 9l0skjlsa\n", 195 | "The score of 李四 is 59\n" 196 | ] 197 | } 198 | ], 199 | "source": [ 200 | "def client():\n", 201 | " proxy = Proxy()\n", 202 | " proxy.get_score(\"李四\")\n", 203 | "client()" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [] 212 | } 213 | ], 214 | "metadata": { 215 | "kernelspec": { 216 | "display_name": "Python 3", 217 | "language": "python", 218 | "name": "python3" 219 | }, 220 | "language_info": { 221 | "codemirror_mode": { 222 | "name": "ipython", 223 | "version": 3 224 | }, 225 | "file_extension": ".py", 226 | "mimetype": "text/x-python", 227 | "name": "python", 228 | "nbconvert_exporter": "python", 229 | "pygments_lexer": "ipython3", 230 | "version": "3.6.4" 231 | }, 232 | "toc": { 233 | "base_numbering": 1, 234 | "nav_menu": {}, 235 | "number_sections": true, 236 | "sideBar": true, 237 | "skip_h1_title": false, 238 | "title_cell": "Table of Contents", 239 | "title_sidebar": "Contents", 240 | "toc_cell": false, 241 | "toc_position": {}, 242 | "toc_section_display": true, 243 | "toc_window_display": false 244 | } 245 | }, 246 | "nbformat": 4, 247 | "nbformat_minor": 2 248 | } 249 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to advance-python 👋 2 | 3 | > 本工程旨在讲解一些进阶Python相关知识,前不过多赘述Python基本语法知识,例如列表、循环条件语句、文本读取等,本工程假定你具备一定python的基础知识讲解一些使用技巧和进阶知识。 4 | 5 | ### 🏠 [Homepage](https://jackpopc.github.io) 6 | 7 | ## Usage 8 | 9 | 项目使用`jupyter notebook`进行开发,它是一款`交互式`,支持`富文本`的在线IDE,比较有利于阐述和讲解。 10 | 11 | **安装jupyter notebook** 12 | 13 | ```sh 14 | pip install jupyter notebook 15 | ``` 16 | 17 | 然后它会在默认浏览器打开,你可以点击右上角的`upload`上传并打开项目中的ipynb格式的文件。 18 | 19 | ## Run tests 20 | 21 | ```sh 22 | python ./code/*.py 23 | ``` 24 | 25 | ## Author 26 | 27 | 👤 **Jackpop** 28 | 29 | * Github: [@Jackpopc](https://github.com/Jackpopc) 30 | 31 | ## Show your support 32 | 33 | Give a ⭐️ if this project helped you! 34 | -------------------------------------------------------------------------------- /code/1-decorators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 示例" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "统计函数运行时间--常用方法" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 11, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "func one run time 1.0003266334533691\n", 27 | "func two run time 1.0006120204925537\n", 28 | "func three run time 1.0003201961517334\n" 29 | ] 30 | } 31 | ], 32 | "source": [ 33 | "from time import time, sleep\n", 34 | "\n", 35 | "\n", 36 | "def fun_one():\n", 37 | " start = time()\n", 38 | " sleep(1)\n", 39 | " end = time()\n", 40 | " cost_time = end - start\n", 41 | " print(\"func one run time {}\".format(cost_time))\n", 42 | " \n", 43 | "def fun_two():\n", 44 | " start = time()\n", 45 | " sleep(1)\n", 46 | " end = time()\n", 47 | " cost_time = end - start\n", 48 | " print(\"func two run time {}\".format(cost_time))\n", 49 | " \n", 50 | "def fun_three():\n", 51 | " start = time()\n", 52 | " sleep(1)\n", 53 | " end = time()\n", 54 | " cost_time = end - start\n", 55 | " print(\"func three run time {}\".format(cost_time))\n", 56 | "\n", 57 | " \n", 58 | "fun_one()\n", 59 | "fun_two()\n", 60 | "fun_three()" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "统计函数运行时间--装饰器" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 14, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "func three run time 1.0003271102905273\n", 80 | "func three run time 1.0006263256072998\n", 81 | "func three run time 1.000312328338623\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "def run_time(func):\n", 87 | " def wrapper():\n", 88 | " start = time()\n", 89 | " func() # 函数在这里运行\n", 90 | " end = time()\n", 91 | " cost_time = end - start\n", 92 | " print(\"func three run time {}\".format(cost_time))\n", 93 | " return wrapper\n", 94 | "\n", 95 | "\n", 96 | "@run_time\n", 97 | "def fun_one():\n", 98 | " sleep(1)\n", 99 | " \n", 100 | "@run_time\n", 101 | "def fun_two():\n", 102 | " sleep(1)\n", 103 | " \n", 104 | "@run_time\n", 105 | "def fun_three():\n", 106 | " sleep(1)\n", 107 | "\n", 108 | " \n", 109 | "fun_one()\n", 110 | "fun_two()\n", 111 | "fun_three()" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "# 带参数的装饰器" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 19, 124 | "metadata": {}, 125 | "outputs": [ 126 | { 127 | "name": "stdout", 128 | "output_type": "stream", 129 | "text": [ 130 | "[One] func three run time 1.0013229846954346\n", 131 | "[Two] func three run time 1.000720500946045\n", 132 | "[Three] func three run time 1.0001459121704102\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "def logger(msg=None):\n", 138 | " def run_time(func):\n", 139 | " def wrapper(*args, **kwargs):\n", 140 | " start = time()\n", 141 | " func() # 函数在这里运行\n", 142 | " end = time()\n", 143 | " cost_time = end - start\n", 144 | " print(\"[{}] func three run time {}\".format(msg, cost_time))\n", 145 | " return wrapper\n", 146 | " return run_time\n", 147 | "\n", 148 | "@logger(msg=\"One\")\n", 149 | "def fun_one():\n", 150 | " sleep(1)\n", 151 | " \n", 152 | "@logger(msg=\"Two\")\n", 153 | "def fun_two():\n", 154 | " sleep(1)\n", 155 | " \n", 156 | "@logger(msg=\"Three\")\n", 157 | "def fun_three():\n", 158 | " sleep(1)\n", 159 | " \n", 160 | "fun_one()\n", 161 | "fun_two()\n", 162 | "fun_three()" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "打印日志" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 47, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [ 178 | "import logging\n", 179 | "\n", 180 | "logging.basicConfig(level=logging.DEBUG)\n", 181 | "log = logging.getLogger(\"Test\")\n", 182 | "\n", 183 | "def logger_info(func):\n", 184 | " logmsg = func.__name__\n", 185 | " def wrapper():\n", 186 | " func() \n", 187 | " log.log(logging.INFO, \"{} if over.\".format(logmsg))\n", 188 | " return wrapper\n", 189 | "\n", 190 | "@logger_info\n", 191 | "def main():\n", 192 | " sleep(1)\n", 193 | " \n", 194 | "main()" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "# 自定义属性 " 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 74, 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [ 210 | "import logging\n", 211 | "from functools import partial\n", 212 | "\n", 213 | "def wrapper_property(obj, func=None):\n", 214 | " if func is None:\n", 215 | " return partial(attach_wrapper, obj)\n", 216 | " setattr(obj, func.__name__, func)\n", 217 | " return func\n", 218 | "\n", 219 | "def logger_info(level, name=None, message=None):\n", 220 | " def decorate(func):\n", 221 | " \n", 222 | " logmsg = message if message else func.__name__\n", 223 | "\n", 224 | " def wrapper(*args, **kwargs):\n", 225 | " log.log(level, logmsg)\n", 226 | " return func(*args, **kwargs)\n", 227 | "\n", 228 | " @wrapper_property(wrapper)\n", 229 | " def set_level(newlevel):\n", 230 | " nonlocal level\n", 231 | " level = newlevel\n", 232 | "\n", 233 | " @wrapper_property(wrapper)\n", 234 | " def set_message(newmsg):\n", 235 | " nonlocal logmsg\n", 236 | " logmsg = newmsg\n", 237 | "\n", 238 | " return wrapper\n", 239 | "\n", 240 | " return decorate\n", 241 | "\n", 242 | "\n", 243 | "@logger_info(logging.WARNING)\n", 244 | "def main(x, y):\n", 245 | " return x + y" 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "execution_count": 76, 251 | "metadata": {}, 252 | "outputs": [ 253 | { 254 | "name": "stderr", 255 | "output_type": "stream", 256 | "text": [ 257 | "ERROR:Test:main\n" 258 | ] 259 | }, 260 | { 261 | "data": { 262 | "text/plain": [ 263 | "10" 264 | ] 265 | }, 266 | "execution_count": 76, 267 | "metadata": {}, 268 | "output_type": "execute_result" 269 | } 270 | ], 271 | "source": [ 272 | "main.set_level(logging.ERROR)\n", 273 | "main(5, 5)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "markdown", 278 | "metadata": {}, 279 | "source": [ 280 | "# 保留元信息的装饰器" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": 105, 286 | "metadata": {}, 287 | "outputs": [ 288 | { 289 | "name": "stdout", 290 | "output_type": "stream", 291 | "text": [ 292 | "func three run time 1.0003266334533691\n" 293 | ] 294 | } 295 | ], 296 | "source": [ 297 | "from functools import wraps\n", 298 | "from time import time\n", 299 | "\n", 300 | "def run_time(func):\n", 301 | " @wraps(func)\n", 302 | " def wrapper(*args, **kwargs):\n", 303 | " start = time()\n", 304 | " func() # 函数在这里运行\n", 305 | " end = time()\n", 306 | " cost_time = end - start\n", 307 | " print(\"func three run time {}\".format(cost_time))\n", 308 | " return wrapper\n", 309 | "\n", 310 | "\n", 311 | "@run_time\n", 312 | "def fun_one():\n", 313 | " '''\n", 314 | " func one doc.\n", 315 | " '''\n", 316 | " sleep(1)\n", 317 | " \n", 318 | "fun_one()" 319 | ] 320 | }, 321 | { 322 | "cell_type": "code", 323 | "execution_count": 106, 324 | "metadata": {}, 325 | "outputs": [ 326 | { 327 | "name": "stdout", 328 | "output_type": "stream", 329 | "text": [ 330 | "fun_one\n", 331 | "\n", 332 | " func one doc.\n", 333 | " \n" 334 | ] 335 | } 336 | ], 337 | "source": [ 338 | "print(fun_one.__name__)\n", 339 | "print(fun_one.__doc__)" 340 | ] 341 | } 342 | ], 343 | "metadata": { 344 | "kernelspec": { 345 | "display_name": "Python 3", 346 | "language": "python", 347 | "name": "python3" 348 | }, 349 | "language_info": { 350 | "codemirror_mode": { 351 | "name": "ipython", 352 | "version": 3 353 | }, 354 | "file_extension": ".py", 355 | "mimetype": "text/x-python", 356 | "name": "python", 357 | "nbconvert_exporter": "python", 358 | "pygments_lexer": "ipython3", 359 | "version": "3.6.4" 360 | } 361 | }, 362 | "nbformat": 4, 363 | "nbformat_minor": 2 364 | } 365 | --------------------------------------------------------------------------------