advanced-python-homework-2023/lectures/04.DataModel.ipynb

853 lines
37 KiB
Plaintext
Raw Normal View History

2023-10-23 08:32:55 +03:00
{
"metadata": {
"kernelspec": {
"name": "python",
"display_name": "Python (Pyodide)",
"language": "python"
},
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8"
}
},
"nbformat_minor": 5,
"nbformat": 4,
"cells": [
{
"cell_type": "markdown",
"source": "# Всё в Python является объектом",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"id": "917bfefc-582c-41b4-b3b2-df29da3c7200"
},
{
"cell_type": "code",
"source": "print(isinstance(\"add\", object))\nprint(isinstance(1_000, object))\nprint(isinstance(3.14, object))",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 1,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "True\n\nTrue\n\nTrue\n"
}
],
"id": "9aa513ac"
},
{
"cell_type": "code",
"source": "(3.14).as_integer_ratio()",
"metadata": {},
"execution_count": 3,
"outputs": [
{
"execution_count": 3,
"output_type": "execute_result",
"data": {
"text/plain": [
"(7070651414971679, 2251799813685248)"
]
},
"metadata": {}
}
],
"id": "3ecb4677"
},
{
"cell_type": "code",
"source": "class Vector2D:\n x = 0\n y = 0\n \n def norm(self):\n return (self.x**2 + self.y**2)**0.5\n\nvec = Vector2D()",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 13,
"outputs": [],
"id": "b7025d1e-79cf-4ba7-a49d-7c1bf2fe063e"
},
{
"cell_type": "code",
"source": "isinstance(vec, object)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 4,
"outputs": [
{
"execution_count": 4,
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {}
}
],
"id": "3371dfc5-234d-481c-8404-f1fd0fd68bb3"
},
{
"cell_type": "code",
"source": "isinstance(Vector2D, object)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 5,
"outputs": [
{
"execution_count": 5,
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {}
}
],
"id": "88ccb376-5fbb-47ed-84b5-4773f26d3490"
},
{
"cell_type": "code",
"source": "isinstance(object, object)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 4,
"outputs": [
{
"execution_count": 4,
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {}
}
],
"id": "4d77d081"
},
{
"cell_type": "code",
"source": "import math\nisinstance(math, object)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 5,
"outputs": [
{
"execution_count": 5,
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {}
}
],
"id": "d4e0bb29-320c-4206-8fc3-4345eda4f73f"
},
{
"cell_type": "code",
"source": "def add(a,b): return a + b\nisinstance(add, object)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 6,
"outputs": [
{
"execution_count": 6,
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {}
}
],
"id": "374f4bc2-7bb6-441d-96aa-392445856565"
},
{
"cell_type": "code",
"source": "add.x = 1",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 7,
"outputs": [],
"id": "0b0b8d31-917e-4fb0-8d41-3e49cc381494"
},
{
"cell_type": "markdown",
"source": "# Магические методы",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"id": "cce63a23"
},
{
"cell_type": "code",
"source": "dir(vec)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 11,
"outputs": [
{
"execution_count": 11,
"output_type": "execute_result",
"data": {
"text/plain": [
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'norm',\n",
" 'x',\n",
" 'y']"
]
},
"metadata": {}
}
],
"id": "f0cac210-b14c-4940-811f-2f7b982014f5"
},
{
"cell_type": "code",
"source": "dir(add)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 18,
"outputs": [
{
"execution_count": 18,
"output_type": "execute_result",
"data": {
"text/plain": [
"['__annotations__',\n",
" '__call__',\n",
" '__class__',\n",
" '__closure__',\n",
" '__code__',\n",
" '__defaults__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__get__',\n",
" '__getattribute__',\n",
" '__globals__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__kwdefaults__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__name__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__qualname__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__']"
]
},
"metadata": {}
}
],
"id": "1b5597dc-484a-43d3-b37a-cd30908d495d"
},
{
"cell_type": "markdown",
"source": "* Управляют внутренней работой объектов\n* Хранят различную информацию объектов (которую можно получать в runtime)\n* Вызываются при использовании синтаксических конструкций\n* Вызываются встроенными (builtins) функциями\n* Область применения: перегрузка операторов, рефлексия и метапрограммирование",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"id": "bc3cdb2f"
},
{
"cell_type": "code",
"source": "class TenItemList:\n\n def __len__(self):\n return 10\n\n\nten_item_list = TenItemList()\nlen(ten_item_list)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 8,
"outputs": [
{
"execution_count": 8,
"output_type": "execute_result",
"data": {
"text/plain": [
"10"
]
},
"metadata": {}
}
],
"id": "dabd01d8"
},
{
"cell_type": "markdown",
"source": "# Всё в Python является объектом, а все синтаксические конструкции сводятся к вызовам магических методов",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"id": "0effc8ba"
},
{
"cell_type": "markdown",
"source": "# Пример сложение\n",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"id": "3841bda9"
},
{
"cell_type": "code",
"source": "class Vector2D:\n\n def __init__(self, x, y):\n self.x = x\n self.y = y\n\n def __add__(self, other):\n return Vector2D(self.x + other.y, self.x + other.y)\n\n def norm(self):\n return (self.x**2 + self.y**2)**0.5\n\nvec1 = Vector2D(1,2)\nvec2 = Vector2D(3,4)\nvec3 = vec1 + vec2\nvec3.x, vec3.y",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 10,
"outputs": [
{
"execution_count": 10,
"output_type": "execute_result",
"data": {
"text/plain": [
"(5, 5)"
]
},
"metadata": {}
}
],
"id": "e3460fe5"
},
{
"cell_type": "markdown",
"source": "## Пример присваивание",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"id": "538a0794"
},
{
"cell_type": "code",
"source": "class Vector2D:\n x = 0\n y = 0\n \n def norm(self):\n return (self.x**2 + self.y**2)**0.5\n\nvec = Vector2D()",
"metadata": {},
"execution_count": 14,
"outputs": [],
"id": "1acd0880"
},
{
"cell_type": "code",
"source": "vec = Vector2D()\nvec.__getattribute__(\"x\")",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 15,
"outputs": [
{
"execution_count": 15,
"output_type": "execute_result",
"data": {
"text/plain": [
"0"
]
},
"metadata": {}
}
],
"id": "1f2bfb38"
},
{
"cell_type": "code",
"source": "vec.__getattribute__(\"norm\")()",
"metadata": {},
"execution_count": 17,
"outputs": [
{
"execution_count": 17,
"output_type": "execute_result",
"data": {
"text/plain": [
"0.0"
]
},
"metadata": {}
}
],
"id": "640d5fad"
},
{
"cell_type": "code",
"source": "vec.x = 5\nvec.__getattribute__(\"x\")",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 18,
"outputs": [
{
"execution_count": 18,
"output_type": "execute_result",
"data": {
"text/plain": [
"5"
]
},
"metadata": {}
}
],
"id": "21efb174"
},
{
"cell_type": "code",
"source": "vec.__setattr__(\"x\", 10)\ngetattr(vec, \"x\")",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 19,
"outputs": [
{
"execution_count": 19,
"output_type": "execute_result",
"data": {
"text/plain": [
"10"
]
},
"metadata": {}
}
],
"id": "dfbce5a2"
},
{
"cell_type": "code",
"source": "setattr(vec, \"x\", 20)\nvec.x",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 20,
"outputs": [
{
"execution_count": 20,
"output_type": "execute_result",
"data": {
"text/plain": [
"20"
]
},
"metadata": {}
}
],
"id": "1d6de7d3"
},
{
"cell_type": "code",
"source": "class Foo:\n def __setattr__(self, key, value):\n print(key, value)\n\nfoo = Foo()\nfoo.a = \"A\"",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 21,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": "a A\n"
}
],
"id": "40df4888"
},
{
"cell_type": "code",
"source": "foo.a",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 22,
"outputs": [
{
"ename": "AttributeError",
"evalue": "'Foo' object has no attribute 'a'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_35789/2615815247.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'Foo' object has no attribute 'a'"
]
}
],
"id": "871b0b35"
},
{
"cell_type": "markdown",
"source": "",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"id": "13c80f91"
},
{
"cell_type": "markdown",
"source": "# На самом деле все объекты реализованы как словари хранящие атрибуты объекта (однако есть возможности для оптимизаций)",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"id": "266ca832"
},
{
"cell_type": "code",
"source": "class Vector2D:\n x = 0\n y = 0\n\n def norm(self):\n return (self.x**2 + self.y**2)**0.5\n\nvec = Vector2D()",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 23,
"outputs": [],
"id": "af1b83e9"
},
{
"cell_type": "code",
"source": "vec.__dict__",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 24,
"outputs": [
{
"execution_count": 24,
"output_type": "execute_result",
"data": {
"text/plain": [
"{}"
]
},
"metadata": {}
}
],
"id": "acc6da9e"
},
{
"cell_type": "code",
"source": "Vector2D.__dict__",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 27,
"outputs": [
{
"execution_count": 27,
"output_type": "execute_result",
"data": {
"text/plain": [
"mappingproxy({'__module__': '__main__',\n",
" 'x': 0,\n",
" 'y': 0,\n",
" 'norm': <function __main__.Vector2D.norm(self)>,\n",
" '__dict__': <attribute '__dict__' of 'Vector2D' objects>,\n",
" '__weakref__': <attribute '__weakref__' of 'Vector2D' objects>,\n",
" '__doc__': None})"
]
},
"metadata": {}
}
],
"id": "4cfbfaec"
},
{
"cell_type": "code",
"source": "vec.x = 5\nvec.__dict__",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 26,
"outputs": [
{
"execution_count": 26,
"output_type": "execute_result",
"data": {
"text/plain": [
"{'x': 5}"
]
},
"metadata": {}
}
],
"id": "b5eb0e56"
},
{
"cell_type": "markdown",
"source": "# Модуль inspect --- информация об объектах в runtime\n\n* Не вся информация может быть доступна через магические методы\n* Недоступную информацию можно получить через модуль inspect",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"id": "f4af619e"
},
{
"cell_type": "code",
"source": "import inspect\n\ndef add(a,b): return a + b\ninspect.isfunction(add)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 28,
"outputs": [
{
"execution_count": 28,
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {}
}
],
"id": "cb6a08ad"
},
{
"cell_type": "code",
"source": "inspect.getsource(add)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 29,
"outputs": [
{
"execution_count": 29,
"output_type": "execute_result",
"data": {
"text/plain": [
"'def add(a,b): return a + b\\n'"
]
},
"metadata": {}
}
],
"id": "3bd00ea2"
},
{
"cell_type": "code",
"source": "from numpy import random\ninspect.getsource(random)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 30,
"outputs": [
{
"execution_count": 30,
"output_type": "execute_result",
"data": {
"text/plain": [
"'\"\"\"\\n========================\\nRandom Number Generation\\n========================\\n\\nUse ``default_rng()`` to create a `Generator` and call its methods.\\n\\n=============== =========================================================\\nGenerator\\n--------------- ---------------------------------------------------------\\nGenerator Class implementing all of the random number distributions\\ndefault_rng Default constructor for ``Generator``\\n=============== =========================================================\\n\\n============================================= ===\\nBitGenerator Streams that work with Generator\\n--------------------------------------------- ---\\nMT19937\\nPCG64\\nPCG64DXSM\\nPhilox\\nSFC64\\n============================================= ===\\n\\n============================================= ===\\nGetting entropy to initialize a BitGenerator\\n--------------------------------------------- ---\\nSeedSequence\\n============================================= ===\\n\\n\\nLegacy\\n------\\n\\nFor backwards compatibility with previous versions of numpy before 1.17, the\\nvarious aliases to the global `RandomState` methods are left alone and do not\\nuse the new `Generator` API.\\n\\n==================== =========================================================\\nUtility functions\\n-------------------- ---------------------------------------------------------\\nrandom Uniformly distributed floats over ``[0, 1)``\\nbytes Uniformly distributed random bytes.\\npermutation Randomly permute a sequence / generate a random sequence.\\nshuffle Randomly permute a sequence in place.\\nchoice Random sample from 1-D array.\\n==================== =========================================================\\n\\n==================== =========================================================\\nCompatibility\\nfunctions - removed\\nin the new API\\n-------------------- ---------------------------------------------------------\\nrand Uniformly distributed values.\\nrandn Normally distributed values.\\nranf Uniformly distributed floating point numbers.\\nrandom_integers Uniformly distributed integers in a given range.\\n (deprecated, use ``integers(..., closed=True)`` instead)\\nrandom_sample Alias for `random_sample`\\nrandint Uniformly distributed integers in a given range\\nseed Seed the legacy random number generator.\\n==================== =========================================================\\n\\n==================== =========================================================\\nUnivariate\\ndistributions\\n-------------------- ---------------------------------------------------------\\nbeta Beta distribution over ``[0, 1]``.\\nbinomial Binomial distribution.\\nchisquare :math:`\\\\\\\\chi^2` distribution.\\nexponential Exponential distribution.\\nf F (Fisher-Snedecor) distribution.\\ngamma Gamma distribution.\\ngeometric Geometric distribution.\\ngumbel Gumbel distribution.\\nhypergeometric Hypergeometric distribution.\\nlaplace Laplace distribution.\\nlogistic Logistic distribution.\\nlognormal Log-normal distribution.\\nlogseries Logarithmic series distribution.\\nnegative_binomial Negative binomial distribution.\\nnoncentral_chisquare Non-central chi-square distribution.\\nnoncentral_f Non-central F distribution.\\nnormal Normal / Gaussian distribution.\\npareto Pareto distribution.\\npoisson Poisson distribution.\\npower Power distribution.\\nrayleigh Rayleigh distribution.\\ntriangular Triangular distribution.\\nuniform Uniform distribution.\\nvonmises Von Mises circular distribution.\\nwald Wald (inverse Gaussian) distribution.
]
},
"metadata": {}
}
],
"id": "e2e6f3f7"
},
{
"cell_type": "markdown",
"source": "# Модуль inspect --- информация об объектах в runtime\n\n* Не вся информация может быть доступна через магические методы\n* Недоступную информацию можно получить через модуль inspect",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"id": "8c0cfc80"
},
{
"cell_type": "code",
"source": "import inspect\n\ndef add(a,b): return a + b\ninspect.isfunction(add)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 2,
"outputs": [
{
"execution_count": 2,
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {}
}
],
"id": "f49084f4"
},
{
"cell_type": "code",
"source": "inspect.getsource(add)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 12,
"outputs": [
{
"execution_count": 12,
"output_type": "execute_result",
"data": {
"text/plain": [
"'def add(a,b): return a + b\\n'"
]
},
"metadata": {}
}
],
"id": "7de58194"
},
{
"cell_type": "code",
"source": "from numpy import random\ninspect.getsource(random)",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": 3,
"outputs": [
{
"execution_count": 3,
"output_type": "execute_result",
"data": {
"text/plain": [
"'\"\"\"\\n========================\\nRandom Number Generation\\n========================\\n\\nUse ``default_rng()`` to create a `Generator` and call its methods.\\n\\n=============== =========================================================\\nGenerator\\n--------------- ---------------------------------------------------------\\nGenerator Class implementing all of the random number distributions\\ndefault_rng Default constructor for ``Generator``\\n=============== =========================================================\\n\\n============================================= ===\\nBitGenerator Streams that work with Generator\\n--------------------------------------------- ---\\nMT19937\\nPCG64\\nPCG64DXSM\\nPhilox\\nSFC64\\n============================================= ===\\n\\n============================================= ===\\nGetting entropy to initialize a BitGenerator\\n--------------------------------------------- ---\\nSeedSequence\\n============================================= ===\\n\\n\\nLegacy\\n------\\n\\nFor backwards compatibility with previous versions of numpy before 1.17, the\\nvarious aliases to the global `RandomState` methods are left alone and do not\\nuse the new `Generator` API.\\n\\n==================== =========================================================\\nUtility functions\\n-------------------- ---------------------------------------------------------\\nrandom Uniformly distributed floats over ``[0, 1)``\\nbytes Uniformly distributed random bytes.\\npermutation Randomly permute a sequence / generate a random sequence.\\nshuffle Randomly permute a sequence in place.\\nchoice Random sample from 1-D array.\\n==================== =========================================================\\n\\n==================== =========================================================\\nCompatibility\\nfunctions - removed\\nin the new API\\n-------------------- ---------------------------------------------------------\\nrand Uniformly distributed values.\\nrandn Normally distributed values.\\nranf Uniformly distributed floating point numbers.\\nrandom_integers Uniformly distributed integers in a given range.\\n (deprecated, use ``integers(..., closed=True)`` instead)\\nrandom_sample Alias for `random_sample`\\nrandint Uniformly distributed integers in a given range\\nseed Seed the legacy random number generator.\\n==================== =========================================================\\n\\n==================== =========================================================\\nUnivariate\\ndistributions\\n-------------------- ---------------------------------------------------------\\nbeta Beta distribution over ``[0, 1]``.\\nbinomial Binomial distribution.\\nchisquare :math:`\\\\\\\\chi^2` distribution.\\nexponential Exponential distribution.\\nf F (Fisher-Snedecor) distribution.\\ngamma Gamma distribution.\\ngeometric Geometric distribution.\\ngumbel Gumbel distribution.\\nhypergeometric Hypergeometric distribution.\\nlaplace Laplace distribution.\\nlogistic Logistic distribution.\\nlognormal Log-normal distribution.\\nlogseries Logarithmic series distribution.\\nnegative_binomial Negative binomial distribution.\\nnoncentral_chisquare Non-central chi-square distribution.\\nnoncentral_f Non-central F distribution.\\nnormal Normal / Gaussian distribution.\\npareto Pareto distribution.\\npoisson Poisson distribution.\\npower Power distribution.\\nrayleigh Rayleigh distribution.\\ntriangular Triangular distribution.\\nuniform Uniform distribution.\\nvonmises Von Mises circular distribution.\\nwald Wald (inverse Gaussian) distribution.
]
},
"metadata": {}
}
],
"id": "8fd68968"
},
{
"cell_type": "code",
"source": "",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"execution_count": null,
"outputs": [],
"id": "3d84fffb"
}
]
}