{ "cells": [ { "cell_type": "markdown", "source": [ "# Mieux programmer en Python 🇫🇷\n", "\n", "[Xiaoou WANG](https://scholar.google.fr/citations?user=vKAMMpwAAAAJ&hl=en)" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "## Les grands principes" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 1, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Zen of Python, by Tim Peters\n", "\n", "Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n", "Flat is better than nested.\n", "Sparse is better than dense.\n", "Readability counts.\n", "Special cases aren't special enough to break the rules.\n", "Although practicality beats purity.\n", "Errors should never pass silently.\n", "Unless explicitly silenced.\n", "In the face of ambiguity, refuse the temptation to guess.\n", "There should be one-- and preferably only one --obvious way to do it.\n", "Although that way may not be obvious at first unless you're Dutch.\n", "Now is better than never.\n", "Although never is often better than *right* now.\n", "If the implementation is hard to explain, it's a bad idea.\n", "If the implementation is easy to explain, it may be a good idea.\n", "Namespaces are one honking great idea -- let's do more of those!\n" ] } ], "source": [ "import this" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## N'utilisez pas des syntaxes exotiques" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 3, 2, 1]\n", "fedcba\n" ] } ], "source": [ "#! don't\n", "a = [1, 2, 3, 4]\n", "c = 'abcdef'\n", "print(a[::-1])\n", "print(c[::-1])\n", "#* do\n", "print(list(reversed(a)))\n", "print(list(reversed(c)))" ] }, { "cell_type": "markdown", "source": [ "## Évitez les keywords de Python pour des noms de variables." ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 5, "outputs": [], "source": [ "#! don't\n", "def funA(list, num):\n", " for element in list:\n", " if num == element:\n", " return True\n", " else:\n", " pass\n", "\n", "#* do\n", "def find_num(searchList, num):\n", " for listValue in searchList:\n", " if num == listValue:\n", " return True\n", " else:\n", " pass" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Implémenter Switch en Python" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 3, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "???\n" ] } ], "source": [ "#* implement switch case in python\n", "def f(x):\n", " return {\n", " 0: \"0\",\n", " 1: \"1\",\n", " 2: \"2\"\n", " }.get(x, \"???\")\n", "\n", "print(f(1)) # 1\n", "print(f(4)) # ???" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Espaces et lignes" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "#* Use space\n", "#! don't\n", "import random\n", "guess_made = 0\n", "number = guess_made\n", "\n", "#* Do\n", "import random\n", "\n", "guess_made = 0\n", "\n", "number = guess_made\n", "\n", "#! don't\n", "if number==3:\n", " print(4)\n", "\n", "#* do\n", "if number == 3:\n", " print(4)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Expression ternaire" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 5, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "# Ternary expression in Python\n", "print(4) if True else print(4)\n" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Refactoring\n", "\n", "Pour un grand projet, la bonne organisation des codes est primordiale et différencie les débutants des chevronnés.\n", "\n", "Une bonne pratique serait de stocker les constantes dans un même fichier et d'en importer là où il en faut." ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 14, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "50\n" ] } ], "source": [ "#* Put all constants in one file `constant.py` in this example\n", "import constant\n", "print(constant.MY_CONSTANT)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Sucres syntaxiques" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 27, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 2 µs, sys: 0 ns, total: 2 µs\n", "Wall time: 3.81 µs\n" ] } ], "source": [ "%%time\n", "#! don't\n", "x = 3\n", "y = 4\n", "\n", "temp = x\n", "x = y\n", "y = temp" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 26, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 2 µs, sys: 0 ns, total: 2 µs\n", "Wall time: 4.77 µs\n" ] } ], "source": [ "%%time\n", "#* do this, it's cleaner, somewhat slower\n", "x = 3\n", "y = 4\n", "\n", "x,y = y,x" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Les conditions de if\n", "\n", "Du moment qu'une condition est évaluée comme fausse, on passe à la ligne suivante." ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 17, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.0674431324005127\n", "0.06275606155395508\n" ] } ], "source": [ "#! don't\n", "from time import time\n", "t = time()\n", "abbreviations = [\"cf.\", \"e.g.\", \"ex.\", \"etc.\", \"flg.\"]\n", "for i in range(100000):\n", " for w in (\"Mr.\", \"Hat\", \"is\", \"chasing\", \".\"):\n", " if w in abbreviations and w[-1]=='.':\n", " pass\n", "print(time() - t)\n", "#* do\n", "t = time()\n", "abbreviations = [\"cf.\", \"e.g.\", \"ex.\", \"etc.\", \"flg.\"]\n", "for i in range(100000):\n", " for w in (\"Mr.\", \"Hat\", \"is\", \"chasing\", \".\"):\n", " if w[-1] == '.' and w in abbreviations:\n", " pass\n", "print(time() - t)\n", "\n" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## To be continued, 2021-03-20" ], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "name": "myenv", "language": "python", "display_name": "Python (myenv)" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }