Streamline slides

This commit is contained in:
Alexander Hess 2020-10-28 17:20:55 +01:00
parent ca9df9e440
commit 8dd24414ac
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
3 changed files with 406 additions and 86 deletions

View file

@ -1287,14 +1287,22 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"From a theoretical point of view, the text representation provided by `.__repr__()` contains all the information (i.e., the $0$s and $1$s in memory) that is needed to model something in a computer. In a way, it is a natural extension from the binary (cf., [Chapter 5 <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_nb.png\">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/05_numbers/00_content.ipynb#Binary-Representations)), hexadecimal (cf., [Chapter 5 <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_nb.png\">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/05_numbers/00_content.ipynb#Hexadecimal-Representations)), and `bytes` (cf., [Chapter 6 <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_nb.png\">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/06_text/02_content.ipynb#The-bytes-Type)) representations of information. After all, just like Unicode characters are encoded in `bytes`, the more \"complex\" objects in this chapter are encoded in Unicode characters via their text representations." "From a theoretical point of view, the text representation provided by `.__repr__()` contains all the information (i.e., the $0$s and $1$s in memory) that is needed to model something in a computer. In a way, it is a natural extension from the binary (cf., [Chapter 5 <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_nb.png\">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/05_numbers/00_content.ipynb#Binary-Representations)), hexadecimal (cf., [Chapter 5 <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_nb.png\">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/05_numbers/00_content.ipynb#Hexadecimal-Representations)), and `bytes` (cf., [Chapter 6 <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_nb.png\">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/06_text/02_content.ipynb#The-bytes-Type)) representations of information. After all, just like Unicode characters are encoded in `bytes`, the more \"complex\" objects in this chapter are encoded in Unicode characters via their text representations."
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [ "source": [
"### The `Matrix` Class" "### The `Matrix` Class"
] ]

View file

@ -260,7 +260,7 @@
"execution_count": 8, "execution_count": 8,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "fragment" "slide_type": "slide"
} }
}, },
"outputs": [ "outputs": [
@ -474,7 +474,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 15,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -542,7 +546,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 16,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -562,7 +570,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 17,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -597,7 +609,7 @@
"execution_count": 18, "execution_count": 18,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "slide" "slide_type": "fragment"
} }
}, },
"outputs": [ "outputs": [
@ -1798,7 +1810,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 55, "execution_count": 55,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"v = Vector([1, 2, 3])\n", "v = Vector([1, 2, 3])\n",
@ -1808,7 +1824,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 56, "execution_count": 56,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1828,7 +1848,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 57, "execution_count": 57,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1920,7 +1944,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"... or pass `v` to [abs() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#abs)." "... or pass `v` to [abs() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#abs)."
] ]
@ -1928,7 +1956,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 60, "execution_count": 60,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"ename": "TypeError", "ename": "TypeError",
@ -1960,7 +1992,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 61, "execution_count": 61,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"import math" "import math"
@ -1969,7 +2005,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 62, "execution_count": 62,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"def norm(vec_or_mat):\n", "def norm(vec_or_mat):\n",
@ -2051,7 +2091,7 @@
"execution_count": 65, "execution_count": 65,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "slide" "slide_type": "fragment"
} }
}, },
"outputs": [ "outputs": [
@ -2134,7 +2174,7 @@
"execution_count": 68, "execution_count": 68,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "slide" "slide_type": "fragment"
} }
}, },
"outputs": [ "outputs": [
@ -2156,7 +2196,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 69, "execution_count": 69,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"z = Vector([0, 0])" "z = Vector([0, 0])"
@ -2202,7 +2246,7 @@
"execution_count": 71, "execution_count": 71,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "slide" "slide_type": "fragment"
} }
}, },
"outputs": [ "outputs": [
@ -2259,7 +2303,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 73, "execution_count": 73,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"s = Vector([42])" "s = Vector([42])"
@ -2270,7 +2318,7 @@
"execution_count": 74, "execution_count": 74,
"metadata": { "metadata": {
"slideshow": { "slideshow": {
"slide_type": "slide" "slide_type": "fragment"
} }
}, },
"outputs": [ "outputs": [

View file

@ -35,14 +35,22 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [ "source": [
"## Packages vs. Modules" "## Packages vs. Modules"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"In [Chapter 2 <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_nb.png\">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/02_functions/02_content.ipynb#Local-Modules-and-Packages), we introduce the concept of a Python module that is imported with the `import` statement. Essentially, a **module** is a single plain text \\*.py file on disk that contains Python code (e.g., [*sample_module.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/02_functions/sample_module.py) in [Chapter 2's folder <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/tree/develop/02_functions)).\n", "In [Chapter 2 <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_nb.png\">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/02_functions/02_content.ipynb#Local-Modules-and-Packages), we introduce the concept of a Python module that is imported with the `import` statement. Essentially, a **module** is a single plain text \\*.py file on disk that contains Python code (e.g., [*sample_module.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/02_functions/sample_module.py) in [Chapter 2's folder <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/tree/develop/02_functions)).\n",
"\n", "\n",
@ -56,7 +64,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -72,7 +84,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"`!ls` lists all the files and folders in the current location: These are Chapter 11's Jupyter notebooks (i.e., the \\*.ipynb files) and the [*sample_package* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/tree/develop/11_classes/sample_package) folder. " "`!ls` lists all the files and folders in the current location: These are Chapter 11's Jupyter notebooks (i.e., the \\*.ipynb files) and the [*sample_package* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/tree/develop/11_classes/sample_package) folder. "
] ]
@ -80,7 +96,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -97,7 +117,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"If we run `!ls` with the `sample_package` folder as the argument, we see the folder's contents: Four \\*.py files. Alternatively, you can use [JupyterLab' File Browser](https://jupyterlab.readthedocs.io/en/stable/user/interface.html?highlight=file%20browser#left-sidebar) on the left to navigate into the package." "If we run `!ls` with the `sample_package` folder as the argument, we see the folder's contents: Four \\*.py files. Alternatively, you can use [JupyterLab' File Browser](https://jupyterlab.readthedocs.io/en/stable/user/interface.html?highlight=file%20browser#left-sidebar) on the left to navigate into the package."
] ]
@ -105,7 +129,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 3,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -121,7 +149,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"The package is organized such that the [*matrix.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/matrix.py) and [*vector.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/vector.py) modules each define just one class, `Matrix` and `Vector`. That is intentional as both classes consist of several hundred lines of code and comments.\n", "The package is organized such that the [*matrix.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/matrix.py) and [*vector.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/vector.py) modules each define just one class, `Matrix` and `Vector`. That is intentional as both classes consist of several hundred lines of code and comments.\n",
"\n", "\n",
@ -135,7 +167,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"import sample_package as pkg" "import sample_package as pkg"
@ -143,7 +179,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"The above cell runs the code in the [*\\_\\_init\\_\\_.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/__init__.py) file from top to bottom, which in turn runs the [*matrix.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/matrix.py), [*utils.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/utils.py), and [*vector.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/vector.py) modules (cf., look at the `import` statements in the four \\*.py files to get the idea). As both [*matrix.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/matrix.py) and [*vector.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/vector.py) depend on each other (i.e., the `Matrix` class needs the `Vector` class to work and vice versa), understanding the order in that the modules are executed is not trivial. Without going into detail, we mention that Python guarantees that each \\*.py file is run only once and figures out the order on its own. If Python is unable to do that, for example, due to unresolvable cirular imports, it aborts with an `ImportError`.\n", "The above cell runs the code in the [*\\_\\_init\\_\\_.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/__init__.py) file from top to bottom, which in turn runs the [*matrix.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/matrix.py), [*utils.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/utils.py), and [*vector.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/vector.py) modules (cf., look at the `import` statements in the four \\*.py files to get the idea). As both [*matrix.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/matrix.py) and [*vector.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/vector.py) depend on each other (i.e., the `Matrix` class needs the `Vector` class to work and vice versa), understanding the order in that the modules are executed is not trivial. Without going into detail, we mention that Python guarantees that each \\*.py file is run only once and figures out the order on its own. If Python is unable to do that, for example, due to unresolvable cirular imports, it aborts with an `ImportError`.\n",
"\n", "\n",
@ -153,7 +193,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 5,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -173,7 +217,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 6,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -192,7 +240,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"... and we use the built-in [dir() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#dir) function to check what attributes `pkg` comes with." "... and we use the built-in [dir() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#dir) function to check what attributes `pkg` comes with."
] ]
@ -200,7 +252,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 7,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -235,7 +291,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"The package's meta information and documentation are automatically parsed from the [*\\_\\_init\\_\\_.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/__init__.py) file." "The package's meta information and documentation are automatically parsed from the [*\\_\\_init\\_\\_.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/__init__.py) file."
] ]
@ -243,7 +303,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 8,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -818,7 +882,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"The meta information could also be accessed separately and individually." "The meta information could also be accessed separately and individually."
] ]
@ -826,7 +894,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 9,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -846,7 +918,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 10,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -865,7 +941,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"We create `Vector` and `Matrix` instances in the usual way by calling the `Vector` and `Matrix` classes from the package's top level." "We create `Vector` and `Matrix` instances in the usual way by calling the `Vector` and `Matrix` classes from the package's top level."
] ]
@ -873,7 +953,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 11,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -893,7 +977,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 12,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -912,7 +1000,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"A common practice by package authors is to put all the objects on the package's top level that they want the package users to work with directly. That is achieved via the `import` statements in the [*\\_\\_init\\_\\_.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/__init__.py) file.\n", "A common practice by package authors is to put all the objects on the package's top level that they want the package users to work with directly. That is achieved via the `import` statements in the [*\\_\\_init\\_\\_.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/__init__.py) file.\n",
"\n", "\n",
@ -924,7 +1016,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 13,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -944,7 +1040,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 14,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -963,7 +1063,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"Also, let's import the [*utils.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/utils.py) module with the `norm()` function into the global scope. As this function is integrated into the `Vector.__abs__()` and `Matrix.__abs__()` methods, there is actually no need to work with it explicitly." "Also, let's import the [*utils.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/utils.py) module with the `norm()` function into the global scope. As this function is integrated into the `Vector.__abs__()` and `Matrix.__abs__()` methods, there is actually no need to work with it explicitly."
] ]
@ -971,7 +1075,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 15,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"from sample_package import utils" "from sample_package import utils"
@ -980,7 +1088,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 16,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -1014,7 +1126,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"Many tutorials on the internet begin by importing \"everything\" from a package into the global scope with `from ... import *`.\n", "Many tutorials on the internet begin by importing \"everything\" from a package into the global scope with `from ... import *`.\n",
"\n", "\n",
@ -1028,7 +1144,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 17,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"from sample_package import *" "from sample_package import *"
@ -1037,7 +1157,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 18, "execution_count": 18,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1057,7 +1181,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 19,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1076,7 +1204,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"For further information on modules and packages, we refer to the [official tutorial <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/tutorial/modules.html)." "For further information on modules and packages, we refer to the [official tutorial <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/tutorial/modules.html)."
] ]
@ -1094,7 +1226,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"The final implementations of the `Vector` and `Matrix` classes are in the [*matrix.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/matrix.py) and [*vector.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/vector.py) files: They integrate all of the functionalities introduced in this chapter. In addition, the code is cleaned up and fully documented, including examples of common usages.\n", "The final implementations of the `Vector` and `Matrix` classes are in the [*matrix.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/matrix.py) and [*vector.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/vector.py) files: They integrate all of the functionalities introduced in this chapter. In addition, the code is cleaned up and fully documented, including examples of common usages.\n",
"\n", "\n",
@ -1104,7 +1240,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 20, "execution_count": 20,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"v = Vector([1, 2, 3])" "v = Vector([1, 2, 3])"
@ -1113,7 +1253,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 21, "execution_count": 21,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1133,7 +1277,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 22,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1153,7 +1301,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 23,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"m = Matrix([(1, 2, 3), (4, 5, 6), (7, 8, 9)])" "m = Matrix([(1, 2, 3), (4, 5, 6), (7, 8, 9)])"
@ -1162,7 +1314,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 24,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1182,7 +1338,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 25,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1215,7 +1375,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 26, "execution_count": 26,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1235,7 +1399,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 27, "execution_count": 27,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1255,7 +1423,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 28, "execution_count": 28,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1295,7 +1467,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"Both `Matrix/Vector.storage` and `Matrix/Vector.typing` themselves reference the `DEFAULT_ENTRIES_STORAGE` and `DEFAULT_ENTRY_TYPE` constants in the [*utils.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/utils.py) module. This way, we could, for example, change only the constants and thereby also change how the `._entries` are stored internally in both classes. Also, this single **[single source of truth <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_wiki.png\">](https://en.wikipedia.org/wiki/Single_source_of_truth)** ensures that both classes are consistent with each other at all times." "Both `Matrix/Vector.storage` and `Matrix/Vector.typing` themselves reference the `DEFAULT_ENTRIES_STORAGE` and `DEFAULT_ENTRY_TYPE` constants in the [*utils.py* <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_gh.png\">](https://github.com/webartifex/intro-to-python/blob/develop/11_classes/sample_package/utils.py) module. This way, we could, for example, change only the constants and thereby also change how the `._entries` are stored internally in both classes. Also, this single **[single source of truth <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_wiki.png\">](https://en.wikipedia.org/wiki/Single_source_of_truth)** ensures that both classes are consistent with each other at all times."
] ]
@ -1303,7 +1479,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 29, "execution_count": 29,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1323,7 +1503,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 30, "execution_count": 30,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1342,7 +1526,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"For the same reasons, we also replace the \"hard coded\" references to the `Vector` and `Matrix` classes within the various methods.\n", "For the same reasons, we also replace the \"hard coded\" references to the `Vector` and `Matrix` classes within the various methods.\n",
"\n", "\n",
@ -1352,7 +1540,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 31, "execution_count": 31,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1371,7 +1563,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"Of course, we could also use the [type() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#type) built-in instead." "Of course, we could also use the [type() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#type) built-in instead."
] ]
@ -1379,7 +1575,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 32, "execution_count": 32,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1398,7 +1598,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"So, for example, the `Matrix.transpose()` method makes a `self.__class__(...)` instead of a `Matrix(...)` call." "So, for example, the `Matrix.transpose()` method makes a `self.__class__(...)` instead of a `Matrix(...)` call."
] ]
@ -1406,7 +1610,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 33, "execution_count": 33,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1441,7 +1649,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"Whenever we need a `str` representation of a class's name, we use the `.__name__` attribute on the class, ..." "Whenever we need a `str` representation of a class's name, we use the `.__name__` attribute on the class, ..."
] ]
@ -1449,7 +1661,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 34, "execution_count": 34,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1468,7 +1684,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"... or access it via the `.__class__` attribute on an instance." "... or access it via the `.__class__` attribute on an instance."
] ]
@ -1476,7 +1696,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 35, "execution_count": 35,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1495,7 +1719,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"For example, the `.__repr__()` and `.__str__()` methods make use of that." "For example, the `.__repr__()` and `.__str__()` methods make use of that."
] ]
@ -1503,7 +1731,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 36, "execution_count": 36,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1531,7 +1763,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"In order to not have to \"hard code\" the name of *another* class (e.g., the `Vector.as_matrix()` method references the `Matrix` class), we apply the following \"hack:\" First, we store a reference to the other class as a class attribute (e.g., `Matrix.vector_cls` and `Vector.matrix_cls`), and then reference that attribute within the methods, just like `.storage` and `.typing` above." "In order to not have to \"hard code\" the name of *another* class (e.g., the `Vector.as_matrix()` method references the `Matrix` class), we apply the following \"hack:\" First, we store a reference to the other class as a class attribute (e.g., `Matrix.vector_cls` and `Vector.matrix_cls`), and then reference that attribute within the methods, just like `.storage` and `.typing` above."
] ]
@ -1539,7 +1775,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 37, "execution_count": 37,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1559,7 +1799,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 38, "execution_count": 38,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1578,7 +1822,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"As an example, the `Vector.as_matrix()` method makes a `self.matrix_cls(...)` instead of a `Matrix(...)` call." "As an example, the `Vector.as_matrix()` method makes a `self.matrix_cls(...)` instead of a `Matrix(...)` call."
] ]
@ -1586,7 +1834,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 39, "execution_count": 39,
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [ "outputs": [
{ {
"data": { "data": {
@ -1638,7 +1890,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [ "source": [
"## \"Real-life\" Experiment" "## \"Real-life\" Experiment"
] ]
@ -1762,7 +2018,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"Similarily, `v` is now a `Vector` with $50$ entries." "Similarily, `v` is now a `Vector` with $50$ entries."
] ]
@ -2236,7 +2496,11 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [ "source": [
"... while `Matrix` objects come with `.n_rows` and `.n_cols` properties." "... while `Matrix` objects come with `.n_rows` and `.n_cols` properties."
] ]