Python : hints & cheats

Son propre fichier basique de log

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

Teaching Python

Here

Découpages Python : formation

Checkio

Demystifying Two Factor Auth

Two-factor auth

Python Open Source Projects of the Year

Here!

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:
        print('ok')
    if 1 in (x, y, z):
        print('ok')
    # si l'un d'eux n'est pas vide:
    if x or y or z:
        print('ok')
    if any((x, y, z)):
        print('ok')
  • 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))',
                      number=10000)
    0.3412662749997253
    »»» timeit.timeit('"-".join([str(n)
                                 for n in range(100)])',
                      number=10000)
    0.2996307989997149
    »»» timeit.timeit('"-".join(map(str, range(100)))',
                      number=10000)
    0.24581470699922647
  • 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
    'red'
    »»» my_car.mileage
    3812.4
    # 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
    True
    »»» a == b
    True
    »»» c = list(a)
    »»» a == c
    True
    »»» a is c
    False

    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
https://learnxinyminutes.com/docs/python3/

How to Send an Email With Python
https://dbader.org/blog/python-send-email

The Python range() Function
https://realpython.com/courses/python-range-function/

Python sleep(): How to Add Time Delays to Your Code
https://realpython.com/python-sleep/

Cool New Features in Python 3.8
https://realpython.com/python38-new-features/

Python Decorators From the Ground Up
https://pabloariasal.github.io/python-decorators-from-the-ground-up/

How — and why — you should use Python Generators
https://medium.freecodecamp.org/how-and-why-you-should-use-python-generators-f6fb56650888

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.
https://teklern.blogspot.fr/2017/11/download-all-your-email-information.html

Memoization in Python: How to Cache Function Results
https://dbader.org/blog/python-memoization

Implementing a Neural Network from Scratch in Python – An Introduction
https://www.datasciencecentral.com/profiles/blogs/implementing-a-neural-network-from-scratch-in-python-an

—–

Mailtrap – Sending Emails

—–

Introduction to NumPy and Pandas – A Simple Tutorial

https://cloudxlab.com/blog/numpy-pandas-introduction

Fastest way to uniquify a list in Python >=3.6

https://www.peterbe.com/plog/fastest-way-to-uniquify-a-list-in-python-3.6

8 Python Modules For Files Handling
http://devarea.com/8-python-modules-for-files-handling/

How do async for loops work in Python? Using asynchronous for loops in Python
https://quentin.pradet.me/blog/using-asynchronous-for-loops-in-python.html

How to use Python and Flask to build a web app — an in-depth tutorial
https://medium.freecodecamp.org/how-to-use-python-and-flask-to-build-a-web-app-an-in-depth-tutorial-437dbfe9f1c6

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.
http://falconframework.org

How to break a CAPTCHA system in 15 minutes with Machine Learning
https://medium.com/@ageitgey/how-to-break-a-captcha-system-in-15-minutes-with-machine-learning-dbebb035a710

Python Exceptions: An Introduction
https://realpython.com/python-exceptions/

Python Metaclasses
https://realpython.com/python-metaclasses/

Building a Simple Web App with Bottle, SQLAlchemy, and the Twitter API
https://realpython.com/blog/python/building-a-simple-web-app-with-bottle-sqlalchemy-twitter-api/

Python – Regular Expressions Practical Guide
http://devarea.com/python-regular-expressions-practical-guide/#.Wki2nN_iZhE

A fast high-level screen scraping and web crawling framework.
https://scrapy.org

A fast high-level screen scraping and web crawling framework.
https://pyfiddle.io/

Python Web scraping
http://scrapingauthority.com/python-scrapy-mysql-and-matplotlib-to-gain-web-data-insights/

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