diff --git a/notebooks/1c--Python_Refresher.ipynb b/notebooks/1c--Python_Refresher.ipynb index 9bc804acbc9fbd13cf4eb4963757279a8163fbb1..f4c6ebf838d616b6f7790ace7520b93617ee4ff5 100644 --- a/notebooks/1c--Python_Refresher.ipynb +++ b/notebooks/1c--Python_Refresher.ipynb @@ -103,7 +103,7 @@ }, "source": [ "### Basic data types and data structures\n", - "* numbers: integer (*arbitrary* precision), float (double precision), complex `2 + 3j`\n", + "* numbers: integer (*arbitrary* precision), float (double precision), complex `2 + 3j` (double precision)\n", "* boolean: `True`, `False`\n", "* strings: `\"foo\"`\n", "* collections (selection)\n", @@ -124,10 +124,12 @@ }, "source": [ "### Variables, mutable vs. immutable objects\n", + "\n", "* a variable is a named reference to an object, e.g.\n", "```python\n", "a = 5\n", "```\n", + "* in Python everything is an object, i.e. contains data along with attributes and/or methods\n", "* immutable objects may not change once created: integer, float, boolean, string, tuple\n", "* mutable objects may change in place: list, set, dictionary, most user-defined classes\n", "* if unsure, check via the object ID (memory location)" @@ -135,7 +137,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 13, "metadata": { "slideshow": { "slide_type": "fragment" @@ -146,8 +148,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "140644013146224\n", - "140644013148208\n" + "140514850383472\n", + "140514850382384\n" ] } ], @@ -176,7 +178,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 14, "metadata": { "slideshow": { "slide_type": "subslide" @@ -229,10 +231,10 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 15, "metadata": { "slideshow": { - "slide_type": "subslide" + "slide_type": "fragment" } }, "outputs": [ @@ -270,7 +272,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 16, "metadata": { "slideshow": { "slide_type": "-" @@ -315,7 +317,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 17, "metadata": { "slideshow": { "slide_type": "-" @@ -332,7 +334,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m/tmp/ipykernel_2358014/3076520060.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;31m# tuple object cannot be modified once instanciated\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mtup1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/tmp/ipykernel_366051/3076520060.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;31m# tuple object cannot be modified once instanciated\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mtup1\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } @@ -363,7 +365,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 18, "metadata": { "slideshow": { "slide_type": "fragment" @@ -377,7 +379,7 @@ "4\n", "True\n", "False\n", - "{'banana', 11}\n" + "{11, 'banana'}\n" ] } ], @@ -402,15 +404,16 @@ }, "source": [ "### Dictionaries\n", - "* a dictionary is a collection which is unordered, mutable and indexed\n", + "\n", + "* a dictionary is a collection which is ~~un~~ordered, mutable and indexed\n", "* dictionaries contain pairs of keys and values\n", "* no duplicate keys are allowed, keys must be immutable\n", - "* since Python 3.7 the standard defines `dict` to [retain the insertion order](https://docs.python.org/3.7/library/stdtypes.html#typesmapping)" + "* since Python 3.7 the language standard defines `dict` to [retain the insertion order](https://docs.python.org/3.7/library/stdtypes.html#typesmapping)" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 19, "metadata": { "slideshow": { "slide_type": "fragment" @@ -487,7 +490,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 20, "metadata": { "slideshow": { "slide_type": "-" @@ -522,7 +525,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 21, "metadata": { "slideshow": { "slide_type": "-" @@ -567,7 +570,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 22, "metadata": { "slideshow": { "slide_type": "skip" @@ -621,7 +624,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 23, "metadata": { "slideshow": { "slide_type": "subslide" @@ -675,7 +678,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 24, "metadata": { "slideshow": { "slide_type": "skip" @@ -731,7 +734,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 25, "metadata": { "scrolled": true, "slideshow": { @@ -764,6 +767,43 @@ "say_hello(\"Peter\")" ] }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "scrolled": true, + "slideshow": { + "slide_type": "skip" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello Peter!\n", + "Hello Peter!\n", + "Hello Peter!\n" + ] + } + ], + "source": [ + "# a decorator with an argument requires one more level of indirection\n", + "def do_n_times(n):\n", + " def decorator(func):\n", + " def wrapper(name):\n", + " for i in range(n):\n", + " func(name)\n", + " return wrapper\n", + " return decorator\n", + "\n", + "@do_n_times(3)\n", + "def say_hello(name):\n", + " print(\"Hello {}!\".format(name))\n", + "\n", + "say_hello(\"Peter\")" + ] + }, { "cell_type": "markdown", "metadata": { @@ -782,7 +822,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 27, "metadata": { "slideshow": { "slide_type": "subslide" @@ -823,7 +863,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 28, "metadata": { "scrolled": false, "slideshow": { @@ -857,12 +897,12 @@ "source": [ "#### Generators\n", "* custom iterators can be written easily using generator functions and generator expressions\n", - "* useful to process large amounts of data in a memory-efficient way, element per element" + "* e.g. useful to process large amounts of data in a memory-efficient way, element per element" ] }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 29, "metadata": { "slideshow": { "slide_type": "subslide" @@ -875,7 +915,7 @@ "9455" ] }, - "execution_count": 43, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } @@ -895,7 +935,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 30, "metadata": { "slideshow": { "slide_type": "subslide" @@ -908,7 +948,7 @@ "9455" ] }, - "execution_count": 44, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -926,7 +966,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 31, "metadata": { "slideshow": { "slide_type": "subslide" @@ -939,7 +979,7 @@ "9455" ] }, - "execution_count": 45, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } @@ -963,7 +1003,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 32, "metadata": { "slideshow": { "slide_type": "fragment" @@ -973,14 +1013,14 @@ "source": [ "# naive way: write to a text file\n", "file_name = \"/tmp/dummy.txt\"\n", - "fp = open(file_name, 'w')\n", - "fp.write(\"Hello\\n\")\n", - "fp.close()" + "fh = open(file_name, 'w')\n", + "fh.write(\"Hello\\n\")\n", + "fh.close()" ] }, { "cell_type": "code", - "execution_count": 47, + "execution_count": 33, "metadata": { "slideshow": { "slide_type": "fragment" @@ -989,8 +1029,8 @@ "outputs": [], "source": [ "# better: write to a text file, close file handle implicitly (ContextManager)\n", - "with open(file_name, 'a') as fp:\n", - " fp.write(\"World\\n\")" + "with open(file_name, 'a') as fh:\n", + " fh.write(\"World\\n\")" ] }, { @@ -1010,7 +1050,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 34, "metadata": { "slideshow": { "slide_type": "subslide" @@ -1027,14 +1067,14 @@ ], "source": [ "# read the complete file into a list in memory\n", - "with open(file_name, 'r') as fp:\n", - " lines = fp.readlines()\n", + "with open(file_name, 'r') as fh:\n", + " lines = fh.readlines()\n", "print(lines)" ] }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 35, "metadata": { "slideshow": { "slide_type": "fragment" @@ -1051,14 +1091,10 @@ } ], "source": [ - "# read line-by-line, more memory efficient generator implementation\n", - "def line_generator():\n", - " with open(file_name, 'r') as fp:\n", - " for line in fp:\n", - " yield line.strip() # get rid of the newline and of leading or trailing whitespace\n", - "\n", - "for line in line_generator():\n", - " print(line)" + "# when reading, the file handle `fh` is actually an iterator on the lines of the file\n", + "with open(file_name, 'r') as fh:\n", + " for line in fh:\n", + " print(line, end='')" ] }, { @@ -1081,7 +1117,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 36, "metadata": { "slideshow": { "slide_type": "subslide" @@ -1094,7 +1130,7 @@ "2.718281828459045" ] }, - "execution_count": 50, + "execution_count": 36, "metadata": {}, "output_type": "execute_result" } @@ -1106,7 +1142,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 37, "metadata": { "slideshow": { "slide_type": "fragment" @@ -1119,7 +1155,7 @@ "2.718281828459045" ] }, - "execution_count": 51, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } @@ -1131,7 +1167,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 38, "metadata": { "slideshow": { "slide_type": "fragment" @@ -1144,7 +1180,7 @@ "1.0" ] }, - "execution_count": 52, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } @@ -1156,7 +1192,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 39, "metadata": { "scrolled": true, "slideshow": { @@ -1191,7 +1227,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 40, "metadata": { "slideshow": { "slide_type": "fragment" @@ -1240,7 +1276,7 @@ "source": [ "### Parameter files\n", "\n", - "* scientific codes are often controled using parameter files\n", + "* scientific codes are often initialized using parameter files\n", " * parameter files can be edited by hand\n", " * code does not need to be changed, reads in parameter file\n", "* recommended\n", @@ -1252,7 +1288,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 41, "metadata": { "slideshow": { "slide_type": "subslide" @@ -1286,7 +1322,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 42, "metadata": { "slideshow": { "slide_type": "fragment" @@ -1303,7 +1339,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 43, "metadata": { "slideshow": { "slide_type": "fragment" diff --git a/notebooks/2b--Cython.ipynb b/notebooks/2b--Cython.ipynb index bc83c310ed56cdf428b85f64d9ff89695b7f1085..fb13423f5837f97bc5071b040a6124478cbd0d87 100644 --- a/notebooks/2b--Cython.ipynb +++ b/notebooks/2b--Cython.ipynb @@ -46,7 +46,8 @@ "source": [ "### Cython?\n", "\n", - "* Cython is Python extended with C data types $\\to$ Cython is a superset of the Python language\n", + "* Cython is Python extended with C data types \n", + " $\\to$ Cython is a superset of the Python language\n", "* Cython is a source-to-source compiler\n", "\n", "### Workflow\n", @@ -385,13 +386,6 @@ " * http://cython.org/ for in-depth information, in particular\n", " * http://cython.readthedocs.io/en/latest/src/tutorial/numpy.html" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/notebooks/4b--Parallel_Frameworks.ipynb b/notebooks/4b--Parallel_Frameworks.ipynb index 419c0f80cce60a709212c63d012a21246495310e..80c3e8ee9c17ec341ff7db3dff5d6d26293d9051 100644 --- a/notebooks/4b--Parallel_Frameworks.ipynb +++ b/notebooks/4b--Parallel_Frameworks.ipynb @@ -594,7 +594,9 @@ } }, "source": [ - "### Dask futures" + "### Dask delayed\n", + "\n", + "* lazy evaluation of custom functions" ] }, { @@ -605,7 +607,23 @@ } }, "source": [ - "### Dask-MPI" + "### Dask futures\n", + "\n", + "* eager evaluation of custom functions" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### Dask-MPI\n", + "\n", + "* enables launch and use of Dask clusters based on MPI\n", + "* allows to run Dask on typical HPC systems, without the need to bother with Cloud-specific complexity" ] }, { @@ -616,7 +634,7 @@ } }, "source": [ - "### Case Study" + "### Example: Monte-Carlo Computation on the HPC system using Dask-MPI" ] }, {