{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Les modules\n", "cf. https://python101.pythonlibrary.org/chapter36_creating_modules_and_packages.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Allons consulter le fichier `arithmetic.py` contenu dans le répertoire courant..." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import arithmetic" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "13\n", "5\n", "0.2857142857142857\n", "72\n" ] } ], "source": [ "print(arithmetic.add(5, 8))\n", "print(arithmetic.subtract(10, 5))\n", "print(arithmetic.division(2, 7))\n", "print(arithmetic.multiply(12, 6))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import arithmetic as toto\n", "toto.add(5, 8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Les paquets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Allons consulter le sous-répertoire `mymath` du répertoire courant..." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "13\n", "5\n", "0.2857142857142857\n", "72\n" ] } ], "source": [ "import mymath\n", "\n", "print(mymath.add(5, 8))\n", "print(mymath.subtract(10, 5))\n", "print(mymath.division(2, 7))\n", "print(mymath.multiply(12, 6))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "72" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import mymath as tata\n", "tata.multiply(12, 6)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from mymath import add" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add(2, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Un petit catalogue de paquets (très) utiles \n", "cf. https://docs.python.org/3/library/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [os](https://docs.python.org/3/library/os.html)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import os" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "#dir(os)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/home/acerioni/devel/python101'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.getcwd()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'voici/un/chemin/de/acces'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.path.join('voici', 'un', 'chemin', 'de', 'acces')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [sys](https://docs.python.org/3/library/sys.html)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'utf-8'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.getdefaultencoding()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['/home/acerioni/devel/python101/venv/lib/python3.7/site-packages/ipykernel_launcher.py',\n", " '-f',\n", " '/home/acerioni/.local/share/jupyter/runtime/kernel-63d60470-7896-4dbd-874f-7c6a76e3aed6.json']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.argv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [time](https://docs.python.org/3/library/time.html)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "import time" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1583792633.057946" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "time.time()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.0024161338806152\n" ] } ], "source": [ "start = time.time()\n", "\n", "time.sleep(2)\n", "\n", "end = time.time()\n", "\n", "print(end-start)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [datetime](https://docs.python.org/3/library/datetime.html)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "import datetime" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2020, 3, 9, 23, 23, 55, 97904)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt = datetime.datetime.now()\n", "dt" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2020-03-09 23:23:55.097904\n" ] } ], "source": [ "print(dt)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2020" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.year" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(dt)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt.tzinfo is None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [pytz](https://pythonhosted.org/pytz/)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "import pytz" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "datetime.datetime(2020, 3, 9, 23, 23, 55, 153883, tzinfo=<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt2 = datetime.datetime.now(pytz.timezone('Europe/Paris'))\n", "dt2 " ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dt2.tzinfo" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2020-03-09 23:23:55.153883+01:00\n" ] } ], "source": [ "print(dt2)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2020-03-09 18:23:55.153883-04:00\n" ] } ], "source": [ "print(dt2.astimezone(pytz.timezone('America/New_York')))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2020-03-09T23:23:55+0100\n" ] } ], "source": [ "print(dt2.strftime(\"%Y-%m-%dT%H:%M:%S%z\"))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2020-03-09T22:23:55Z\n" ] } ], "source": [ "print(dt2.astimezone(pytz.utc).strftime(\"%Y-%m-%dT%H:%M:%SZ\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "cf. https://en.wikipedia.org/wiki/ISO_8601" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [logging](https://docs.python.org/3/library/logging.html)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "import logging\n", "\n", "logger = logging.getLogger()\n", "logger.setLevel(logging.DEBUG)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:root:Ceci est un message d'info.\n" ] } ], "source": [ "logging.info(\"Ceci est un message d'info.\")" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "DEBUG:root:Ceci est un message de debug.\n" ] } ], "source": [ "logging.debug(\"Ceci est un message de debug.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [json](https://docs.python.org/3/library/json.html)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "import json" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "my_dict = {\"key1\": \"value1\", \"key2\": 2}" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_dict)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'key1': 'value1', 'key2': 2}\n" ] } ], "source": [ "print(my_dict)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"key1\": \"value1\", \"key2\": 2}\n" ] } ], "source": [ "print( json.dumps( my_dict ) )" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type( json.dumps( my_dict ) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cela fait la même chose que " ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"{'key1': 'value1', 'key2': 2}\"" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(my_dict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...mais le paquet `json` offre des fonctionnalités supplémentaires. Ex. :" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"key1\": \"value1\",\n", " \"key2\": 2\n", "}\n" ] } ], "source": [ "print( json.dumps(my_dict, indent=4) )" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"key1\": \"value1\",\n", " \"key2\": 2\n", "}\n" ] } ], "source": [ "print( json.dumps(my_dict, indent=4, sort_keys=True) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "De plus, c'est très utile pour écrire sur disque des dictionnaires Python, au format JSON :" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "with open('my_dict.json', 'w') as fp:\n", " json.dump(my_dict, fp)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "del my_dict" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "with open('my_dict.json', 'r') as fp:\n", " my_dict = json.load(fp)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'key1': 'value1', 'key2': 2}" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_dict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [PyYAML](https://pyyaml.org/)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "import yaml" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "with open('my_dict.yaml', 'w') as fp:\n", " yaml.dump(my_dict, fp, default_flow_style=False)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "del my_dict" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "with open('my_dict.yaml', 'r') as fp:\n", " my_dict = yaml.full_load(fp)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'key1': 'value1', 'key2': 2}" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [pickle](https://docs.python.org/3/library/pickle.html)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "import pickle" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "with open('my_dict.pkl', 'wb') as fp:\n", " pickle.dump(my_dict, fp)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "del my_dict" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "with open('my_dict.pkl', 'rb') as fp:\n", " my_dict = pickle.load(fp)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'key1': 'value1', 'key2': 2}" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Et encore..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [requests](https://2.python-requests.org//en/master/)\n", "on l'utilisera dans le prochain cahier..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [pandas](https://pandas.pydata.org/)\n", "on l'utilisera dans le prochain cahier..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [BeautifulSoup](https://www.crummy.com/software/BeautifulSoup/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Flask](http://flask.pocoo.org/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Gunicorn](https://gunicorn.org/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Django](https://www.djangoproject.com/)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [SQLAlchemy](https://www.sqlalchemy.org/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [NumPy](https://www.numpy.org/)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [SciPy](https://www.scipy.org/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [SimPy](https://simpy.readthedocs.io/en/latest/)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [matplotlib](https://matplotlib.org/)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [seaborn](http://seaborn.pydata.org/)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Plotly](https://plot.ly/python/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [Dash](https://plot.ly/dash/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [scikit-learn](https://scikit-learn.org/stable/index.html)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [TensorFlow](https://www.tensorflow.org/)\n", "cf. https://en.wikipedia.org/wiki/Comparison_of_deep-learning_software" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }