diff --git a/slides.qmd b/slides.qmd index cac1171322e12809a450f03434cffd6912f3e12c..8b709a5f4662041625592e7674ca2efda77c7601 100644 --- a/slides.qmd +++ b/slides.qmd @@ -13,57 +13,79 @@ author: # Python -* Invented by Guido van Rossum in XXXX +::::{.columns} + + +:::{.column width=75%} +* Invented by Guido van Rossum in 1991 * Usually considered to have a simple syntax * Interpreted scripting lanuage * De-facto standard in many data science fields +::: -## Interpreter +:::{.column width=15%} + +::: + +:::: + +# Interpreter * Python is not compiled^[this is true but also not true] but interpreted on runtime * The interpreter can be called directly ```{raw} - python3 my_scripy.py - ``` -* Scripts can define an interpreter in the shebang + python3 my_script.py ``` +* Scripts can define an interpreter in ther [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) + ```{.raw .python} #!/usr/bin/env python3 + print("Hello world!") ``` # Standard library -* Python organizes code in individual packages -* [Standard library](https://docs.python.org/3/library/index.html) provide extensive built-in facilities (`os`, `sys`, `datetime`, `itertools`, ...) +* Python code is organized in [modules](https://docs.python.org/3/tutorial/modules.html) and [packages](https://docs.python.org/3/tutorial/modules.html#packages) +* The [standard library](https://docs.python.org/3/library/index.html) provides extensive built-in facilities</br> + (`sys`, `re`, `datetime`, `pathlib`, ...) + + ```{python} + #| echo: true + from random import randint + + print(randint(0, 42)) + ``` # External packages {auto-animate=true} * The python package index ([PyPI](https://pypi.org)) is a centralized repository for third-party packages -* Everyone can register packages under a not yet taken name (first come first serve) -* Packages can be installed using the built-in package manager pip: +* Everyone can register packages under a not yet taken name</br> + (first come first serve) +* Packages can be installed using the built-in package manager [pip](https://docs.python.org/3/installing/index.html): ```{raw} python3 -m pip install numpy ``` # Package manager -* A package manager is a software that helps you to manage different version of packages and their dependencies +* A package manager helps you to install different versions of packages and their dependencies * General purpose package manager (`brew`, `apt-get`, `conda`, ...) * Python package manager (`pip`, `conda`, ...) -# _Virtual_ Environments +# Virtual environments -* Manage different sets of python packages -* Allows you to have a stable combination of package versions -* Pure python tools (`virtualenv`, ...) -* Tools to handle general software environments (`conda`, ...) +* Allows you to have a specific combination of libraries/binaries/packages for a project +* You can manage different environments side-by-side +* [`venv`](https://docs.python.org/3/library/venv.html) (formerly known as `virutalenv`) is shipped with the standard library +* Tools to handle general software environments (e.g. `conda`) # (Ana)conda -* `conda` is a general purpose package manager provided by Anacond Inc. -* `conda` allows you to install - 1. handle virtual environments - 2. with different python interpreters - 3. and install python packages +* `conda` is a general purpose package manager +* `conda` allows you to _i)_ create virtual environments + _ii)_ with different python interpreters, + _iii)_ install python packages, + _iv)_ and other software/libraries (e.g. ecCodes, NetCDF, ...) +* Due to restricted system permissions, `conda` became quite popular among HPC users # The amazing world of snakes {.national} @@ -97,38 +119,34 @@ author: # (micro)mamba -* `mamba` is a drop-in replacement for `conda` written in C++ -* Conflict resolution is **much** faster -* Can be installed without any pre-occupied environment (`micromamba`) +::::{.columns} -# Setting up an environment +:::{.column width=75%} +* [`mamba`](https://mamba.readthedocs.io/en/latest/index.html) is a drop-in replacement for `conda` written in C++ +* Conflict resolution is **much** faster +* Can be installed as self-contained single-file executable (`micromamba`, well-fitted for CI) +::: -```{raw} -mamba create -n my_env python=3.12 -mamba activate my_env -mamba install matplotlib numpy scipy -``` +:::{.column width=15%} + +::: -# Recreating an environment {auto-animate=true visibility="uncounted"} +:::: -Specify environment name, python version and dependencies: -```{.yaml filename=environment.yaml} -name: my_env -channels: - - conda-forge -dependencies: - - matplotlib - - numpy - - scipy -``` +# Setting up an environment -Create a new environment based on your configuration: -```{raw} -mamba env create -f environment.yaml -mamba activate my_env -``` +* Interactively create and populate a virtual environment + ```{raw} + mamba create -n my_env python=3.12 + mamba activate my_env + mamba install matplotlib numpy scipy eccodes + ``` +* Some packages might only be installable via `pip`: ^[Always un `pip` last] ^[Use the correct `pip`] + ```{raw} + python -m pip install gribscan + ``` -# Recreating an environment {auto-animate=true} +# Recreating an environment Specify environment name, python version and dependencies: ```{.yaml filename=environment.yaml} @@ -139,6 +157,7 @@ dependencies: - matplotlib - numpy - scipy + - eccodes - pip: - gribscan ``` @@ -161,10 +180,14 @@ mamba activate my_env ```{raw} python3 -m ipykernel install --name my_env ``` -* The current environment should be available as kernel in Jupyter +* The environment is now available as kernel in jupyter # Conclusions -* Use virtual environments to organize your work -* Keep track of installed packages in an `environment.yaml` file -* If in doubt, use `micromamba` to do so +* Use virtual environments to organize dependencies for your projects +* Keep track of installed packages using an `environment.yaml` file +* If in doubt, use `micromamba` + +:::{.fragment} +Python has nothing to do with snakes---it is named after "Monty Python" +::: diff --git a/static/Python-logo-notext.svg b/static/Python-logo-notext.svg new file mode 100644 index 0000000000000000000000000000000000000000..269bbea5882dc7567b4d8b928abbba365ef46df9 --- /dev/null +++ b/static/Python-logo-notext.svg @@ -0,0 +1,265 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- Created with Inkscape (http://www.inkscape.org/) --> + +<svg + version="1.0" + id="svg2" + sodipodi:version="0.32" + inkscape:version="1.2.1 (9c6d41e410, 2022-07-14)" + sodipodi:docname="python-logo-only.svg" + width="92.070236pt" + height="101.00108pt" + inkscape:export-filename="python-logo-only.png" + inkscape:export-xdpi="232.44" + inkscape:export-ydpi="232.44" + xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" + xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" + xmlns:xlink="http://www.w3.org/1999/xlink" + xmlns="http://www.w3.org/2000/svg" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:dc="http://purl.org/dc/elements/1.1/"> + <metadata + id="metadata371"> + <rdf:RDF> + <cc:Work + rdf:about=""> + <dc:format>image/svg+xml</dc:format> + <dc:type + rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> + </cc:Work> + </rdf:RDF> + </metadata> + <sodipodi:namedview + inkscape:window-height="2080" + inkscape:window-width="1976" + inkscape:pageshadow="2" + inkscape:pageopacity="0.0" + guidetolerance="10.0" + gridtolerance="10.0" + objecttolerance="10.0" + borderopacity="1.0" + bordercolor="#666666" + pagecolor="#ffffff" + id="base" + inkscape:zoom="2.1461642" + inkscape:cx="91.558698" + inkscape:cy="47.9926" + inkscape:window-x="1092" + inkscape:window-y="72" + inkscape:current-layer="svg2" + width="210mm" + height="40mm" + units="mm" + inkscape:showpageshadow="2" + inkscape:pagecheckerboard="0" + inkscape:deskcolor="#d1d1d1" + inkscape:document-units="pt" + showgrid="false" + inkscape:window-maximized="0" /> + <defs + id="defs4"> + <linearGradient + id="linearGradient2795"> + <stop + style="stop-color:#b8b8b8;stop-opacity:0.49803922;" + offset="0" + id="stop2797" /> + <stop + style="stop-color:#7f7f7f;stop-opacity:0;" + offset="1" + id="stop2799" /> + </linearGradient> + <linearGradient + id="linearGradient2787"> + <stop + style="stop-color:#7f7f7f;stop-opacity:0.5;" + offset="0" + id="stop2789" /> + <stop + style="stop-color:#7f7f7f;stop-opacity:0;" + offset="1" + id="stop2791" /> + </linearGradient> + <linearGradient + id="linearGradient3676"> + <stop + style="stop-color:#b2b2b2;stop-opacity:0.5;" + offset="0" + id="stop3678" /> + <stop + style="stop-color:#b3b3b3;stop-opacity:0;" + offset="1" + id="stop3680" /> + </linearGradient> + <linearGradient + id="linearGradient3236"> + <stop + style="stop-color:#f4f4f4;stop-opacity:1" + offset="0" + id="stop3244" /> + <stop + style="stop-color:white;stop-opacity:1" + offset="1" + id="stop3240" /> + </linearGradient> + <linearGradient + id="linearGradient4671"> + <stop + style="stop-color:#ffd43b;stop-opacity:1;" + offset="0" + id="stop4673" /> + <stop + style="stop-color:#ffe873;stop-opacity:1" + offset="1" + id="stop4675" /> + </linearGradient> + <linearGradient + id="linearGradient4689"> + <stop + style="stop-color:#5a9fd4;stop-opacity:1;" + offset="0" + id="stop4691" /> + <stop + style="stop-color:#306998;stop-opacity:1;" + offset="1" + id="stop4693" /> + </linearGradient> + <linearGradient + x1="224.23996" + y1="144.75717" + x2="-65.308502" + y2="144.75717" + id="linearGradient2987" + xlink:href="#linearGradient4671" + gradientUnits="userSpaceOnUse" + gradientTransform="translate(100.2702,99.61116)" /> + <linearGradient + x1="172.94208" + y1="77.475983" + x2="26.670298" + y2="76.313133" + id="linearGradient2990" + xlink:href="#linearGradient4689" + gradientUnits="userSpaceOnUse" + gradientTransform="translate(100.2702,99.61116)" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient4689" + id="linearGradient2587" + gradientUnits="userSpaceOnUse" + gradientTransform="translate(100.2702,99.61116)" + x1="172.94208" + y1="77.475983" + x2="26.670298" + y2="76.313133" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient4671" + id="linearGradient2589" + gradientUnits="userSpaceOnUse" + gradientTransform="translate(100.2702,99.61116)" + x1="224.23996" + y1="144.75717" + x2="-65.308502" + y2="144.75717" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient4689" + id="linearGradient2248" + gradientUnits="userSpaceOnUse" + gradientTransform="translate(100.2702,99.61116)" + x1="172.94208" + y1="77.475983" + x2="26.670298" + y2="76.313133" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient4671" + id="linearGradient2250" + gradientUnits="userSpaceOnUse" + gradientTransform="translate(100.2702,99.61116)" + x1="224.23996" + y1="144.75717" + x2="-65.308502" + y2="144.75717" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient4671" + id="linearGradient2255" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(0.562541,0,0,0.567972,-11.5974,-7.60954)" + x1="224.23996" + y1="144.75717" + x2="-65.308502" + y2="144.75717" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient4689" + id="linearGradient2258" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(0.562541,0,0,0.567972,-11.5974,-7.60954)" + x1="172.94208" + y1="76.176224" + x2="26.670298" + y2="76.313133" /> + <radialGradient + inkscape:collect="always" + xlink:href="#linearGradient2795" + id="radialGradient2801" + cx="61.518883" + cy="132.28575" + fx="61.518883" + fy="132.28575" + r="29.036913" + gradientTransform="matrix(1,0,0,0.177966,0,108.7434)" + gradientUnits="userSpaceOnUse" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient4671" + id="linearGradient1475" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(0.562541,0,0,0.567972,-14.99112,-11.702371)" + x1="150.96111" + y1="192.35176" + x2="112.03144" + y2="137.27299" /> + <linearGradient + inkscape:collect="always" + xlink:href="#linearGradient4689" + id="linearGradient1478" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(0.562541,0,0,0.567972,-14.99112,-11.702371)" + x1="26.648937" + y1="20.603781" + x2="135.66525" + y2="114.39767" /> + <radialGradient + inkscape:collect="always" + xlink:href="#linearGradient2795" + id="radialGradient1480" + gradientUnits="userSpaceOnUse" + gradientTransform="matrix(1.7490565e-8,-0.23994696,1.054668,3.7915457e-7,-83.7008,142.46201)" + cx="61.518883" + cy="132.28575" + fx="61.518883" + fy="132.28575" + r="29.036913" /> + </defs> + <path + style="fill:url(#linearGradient1478);fill-opacity:1" + d="M 54.918785,9.1927389e-4 C 50.335132,0.02221727 45.957846,0.41313697 42.106285,1.0946693 30.760069,3.0991731 28.700036,7.2947714 28.700035,15.032169 v 10.21875 h 26.8125 v 3.40625 h -26.8125 -10.0625 c -7.792459,0 -14.6157588,4.683717 -16.7499998,13.59375 -2.46181998,10.212966 -2.57101508,16.586023 0,27.25 1.9059283,7.937852 6.4575432,13.593748 14.2499998,13.59375 h 9.21875 v -12.25 c 0,-8.849902 7.657144,-16.656248 16.75,-16.65625 h 26.78125 c 7.454951,0 13.406253,-6.138164 13.40625,-13.625 v -25.53125 c 0,-7.2663386 -6.12998,-12.7247771 -13.40625,-13.9374997 C 64.281548,0.32794397 59.502438,-0.02037903 54.918785,9.1927389e-4 Z m -14.5,8.21875012611 c 2.769547,0 5.03125,2.2986456 5.03125,5.1249996 -2e-6,2.816336 -2.261703,5.09375 -5.03125,5.09375 -2.779476,-1e-6 -5.03125,-2.277415 -5.03125,-5.09375 -10e-7,-2.826353 2.251774,-5.1249996 5.03125,-5.1249996 z" + id="path1948" /> + <path + style="fill:url(#linearGradient1475);fill-opacity:1" + d="m 85.637535,28.657169 v 11.90625 c 0,9.230755 -7.825895,16.999999 -16.75,17 h -26.78125 c -7.335833,0 -13.406249,6.278483 -13.40625,13.625 v 25.531247 c 0,7.266344 6.318588,11.540324 13.40625,13.625004 8.487331,2.49561 16.626237,2.94663 26.78125,0 6.750155,-1.95439 13.406253,-5.88761 13.40625,-13.625004 V 86.500919 h -26.78125 v -3.40625 h 26.78125 13.406254 c 7.792461,0 10.696251,-5.435408 13.406241,-13.59375 2.79933,-8.398886 2.68022,-16.475776 0,-27.25 -1.92578,-7.757441 -5.60387,-13.59375 -13.406241,-13.59375 z m -15.0625,64.65625 c 2.779478,3e-6 5.03125,2.277417 5.03125,5.093747 -2e-6,2.826354 -2.251775,5.125004 -5.03125,5.125004 -2.76955,0 -5.03125,-2.29865 -5.03125,-5.125004 2e-6,-2.81633 2.261697,-5.093747 5.03125,-5.093747 z" + id="path1950" /> + <ellipse + style="opacity:0.44382;fill:url(#radialGradient1480);fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:15.4174;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" + id="path1894" + cx="55.816761" + cy="127.70079" + rx="35.930977" + ry="6.9673119" /> +</svg> diff --git a/static/mamba-logo.png b/static/mamba-logo.png new file mode 100644 index 0000000000000000000000000000000000000000..a2c04148c00e48cb159b64d32a8e9aad9478c5f9 Binary files /dev/null and b/static/mamba-logo.png differ