Python : hints & cheats

Son propre fichier basique de log

import datetime
msg = "Test de log"
dt =
with open("monlogfile.log", 'a+') as f:
    f.write('{:02}:{:02} - {}\n'.format(
        dt.hour, dt.minute, msg))

Teaching Python


Découpages Python : formation


Demystifying Two Factor Auth

Two-factor auth

Python Open Source Projects of the Year


Thanks to Dan Bader

  • Python Parallel Computing (in 60 Seconds or less): here
  • Python Decorators: A Step-By-Step Introduction: here
  • Interfacing Python and C: Advanced “ctypes” Features: here
  • Working with Random Numbers in Python » ici
  • Face detection
  • Différentes manières de testers plusieurs flags en même temps en Python :
    x, y, z = 0, 1, 0
    if x == 1 or y == 1 or z == 1:
    if 1 in (x, y, z):
    # si l'un d'eux n'est pas vide:
    if x or y or z:
    if any((x, y, z)):
  • Comment trier un dictionnaire Python par ses valeurs :
    »»» xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}
    »»» sorted(xs.items(), key=lambda x: x[1])

    ou bien :
    »»» import operator
    »»» sorted(xs.items(), key=operator.itemgetter(1))
  • Mesurer le temps d’exécution de petits morceaux de code Python :
    »»» import timeit
    »»» timeit.timeit('"-".join(str(n) for n in range(100))',
    »»» timeit.timeit('"-".join([str(n)
                                 for n in range(100)])',
    »»» timeit.timeit('"-".join(map(str, range(100)))',
  • Comment utiliser la classe namedtuples :
    # namedtup1e est une classe :
    »»» from collections import namedtuple
    »»» Car = namedtup1e('Car' , 'color mileage')
    # Our new "Car" class works as expected:
    »»» my_car = Car('red', 3812.4)
    »»» my_car.color
    »»» my_car.mileage
    # Une belle représentation repr avec :
    »»» my_car
    Car(color='red' , mileage=3812.4)
    # Comme les tuples, les namedtuples sont immuables :
    »»» my_car.color = 'blue'
    AttributeError: "can't set attribute"
  • « is » vs « == » :
    »»» a = [1, 2, 3]
    »»» b = a
    »»» a is b
    »»» a == b
    »»» c = list(a)
    »»» a == c
    »»» a is c

    is est True si deux variables pointent vers le même objet ;
    == est True si les variables contenues dans les objets sont identiques.

  • Titre Titre  :
    »»» Code code code

Learning Python in minutes

How to Send an Email With Python

The Python range() Function

Python sleep(): How to Add Time Delays to Your Code

Cool New Features in Python 3.8

Python Decorators From the Ground Up

How — and why — you should use Python Generators

Download information on all your gmail emails and the body text to either csv or json. I developed this to download my 100K + emails stored over several years on gmail.

Memoization in Python: How to Cache Function Results

Implementing a Neural Network from Scratch in Python – An Introduction


Mailtrap – Sending Emails


Introduction to NumPy and Pandas – A Simple Tutorial

Fastest way to uniquify a list in Python >=3.6

8 Python Modules For Files Handling

How do async for loops work in Python? Using asynchronous for loops in Python

How to use Python and Flask to build a web app — an in-depth tutorial

Framework ultra simple pour faire des micro-services en Json
Falcon is a bare-metal Python web API framework for building very fast app backends and microservices.

How to break a CAPTCHA system in 15 minutes with Machine Learning

Python Exceptions: An Introduction

Python Metaclasses

Building a Simple Web App with Bottle, SQLAlchemy, and the Twitter API

Python – Regular Expressions Practical Guide

A fast high-level screen scraping and web crawling framework.

A fast high-level screen scraping and web crawling framework.

Python Web scraping

Tips for writing extremely short Python programs
Extremely short Python programs (aka « golfing »)

Instagramming with Python for Data Analysis
The guide

Julien Danjou blog

Easy Python logging with daiquiri

The three things you need to know about packaging are:

  • – Use pip to install your packages from PyPI
  • – Use pbr to package your modules
  • – Use PyPI to publish your package

Read more here.

A safe GitHub workflow with Pastamaker
The definitive guide to Python exceptions
How do you write your Python tests?
The unittest module in Python is the natural entry point to start writing test, and it’s really easy to use. It’s not really harder than using assert, and it will provide much nicer output when run. Once you get on that road, there’s a lot of other nice modules you can start using, such pytest, coverage, nose or mock. And if your project is hosted on places such as GitHub, it’s really easy to use services such as Travis to automate tests runs.

A simple filtering syntax tree in Python

Stop merging your pull requests manually

How I stopped merging broken code

How to Log Properly in Python

More GitHub workflow automation

Code Style Checks in Python