Le « monde » Python

Les versions de Python

Les versions de Python

  • Python 1.5 - avril 1999
  • Python 1.6 - septembre 2000
  • Python 2.0 - octobre 2000
  • Python 2.1 - avril 2001
  • Python 2.2 - décembre 2001
  • Python 2.3 - juillet 2003
  • Python 2.4 - novembre 2004
  • Python 2.5 - septemble 2006
  • Python 2.6 - novembre 2008
  • Python 3.0 - décembre 2010
  • Python 3.1 - juin 2009
  • Python 2.7 - juillet 2010

Les versions de Python

  • Python 2.6 (octobre 2008)
    • amélioration + aide à la migration vers Python 3
    • améliorations de Python 3 => Python 2.6 (transition plus facile)
  • Python 3.0 (décembre 2008)

    • Python 3.0 a volontairement brisé la rétro-compatibilité
    • nettoyage du langage
    • tout est en unicode
    • plus de class foobar(object)

2to3 transforme du code Python 2.x en Python 3.x

Les versions de Python

  • Python 2.7 (juillet 2010)
    • dernière version de la série 2.x
    • sera maintenue plus longtemps que les précédentes versions 2.x
    • amélioration de python 3.1 => python 2.7
  • Python 3.1 (juillet 2010)

Les implémentations de Python

Les implémentations de Python

  • CPython

    • implémentation de référence du langage Python.
    • interpréteur de bytecode écrit en langage C.
  • Jython (java)

  • IronPython (.NET)
  • PyPy (architecture flexible, JIT, LLVM…)
  • Unladen Swallow (Projet Google, LLVM)
  • Stackless Ptyhon (Microthreads…)

Module Standard Python Important

The Python Standard Library

library_reference

http://docs.python.org/library/index.html

Module Standard Python

os

1 import os
2 os.mkdir("/tmp/foobar")
3 os.makedirs("/tmp/1/2/3")
4 os.rename("/tmp/foobar", "/tmp/test")
5 
6 for root, dirs, files in os.walk('/home/'):
7     print(root)
8     print(dirs)
9     print(files)

os.path

1 >>> import os.path
2 >>> os.path.basename('/tmp/foobar.txt')
3 'foobar.txt'
4 >>> os.path.dirname('/tmp/foobar.txt')
5 '/tmp'
6 >>> os.path.exists('/tmp/foobar.txt')
7 False
8 >>> os.path.join('tmp', 'foo', 'bar', 'test.txt')
9 'tmp/foo/bar/test.txt'

Module Standard Python

shutil

1 import shutil
2 shutil.copy('/tmp/source.txt', '/tmp/destination.txt')
3 shutil.copytree('/tmp/', '/tmp2')
4 shutil.rmtree('/tmp/foobar')

sys

1 import sys
2 sys.path
3 sys.exit()

datetime

1 import datetime
2 >>> datetime.date.today()
3 datetime.date(2010, 7, 25)
4 >>> datetime.date.today() - datetime.timedelta(20)
5 >>> datetime.date(2010, 7, 5)

Module Standard Python

re

1 >>> import re
2 >>> re.search('foo(.*)', 'foobar').group(1)
3 'bar'

logging

1 >>> logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
2 >>> logging.debug('This message should go to the log file')

urllib2

1 >>> import urllib2
2 >>> f = urllib2.urlopen('http://www.python.org/')
3 >>> print f.read(100)
4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 <?xml-stylesheet href="./css/ht2html

Module Standard Python

pprint

1 >>> import pprint
2 >>> a = { 'prenom': 'Guido', 'nom': 'van Rossum' }
3 >>> print([a, a, a])
4 [{'nom': 'van Rossum', 'prenom': 'Guido'}, {'nom': 'van Rossum', 'prenom': 'Guido'}, {'nom': 'van Rossum', 'prenom': 'Guido'}]
5 >>> pprint.pprint([a, a, a])
6 [{'nom': 'van Rossum', 'prenom': 'Guido'},
7  {'nom': 'van Rossum', 'prenom': 'Guido'},
8  {'nom': 'van Rossum', 'prenom': 'Guido'}]

csv

1 >>> import csv
2 >>> spamReader = csv.reader(open('eggs.csv'), delimiter=' ', quotechar='|')
3 >>> for row in spamReader:
4 ...     print ', '.join(row)
5 Spam, Spam, Spam, Spam, Spam, Baked Beans
6 Spam, Lovely Spam, Wonderful Spam

Module Standard Python

subprocess

1 import subprocess
2 retcode = subprocess.call(["ls", "-l"])
3 
4 process = subprocess.Popen("cmd", 'w', shell=True, stdin=PIPE)
5 process.stdin.close()
6 if process.wait() != 0:
7     print "There were some errors"

Concepts

Concepts - « namespace »

1 import google.gmail
2 google.gmail.inbox()
3 
4 from google.gmail import inbox
5 inbox()
6 
7 from google.gmail import inbox as ma_boite_mail
8 ma_boite_mail()

Dans google/___init___.py

1 from gmail import inbox

Ensuite

1 import google
2 google.inbox()

Concepts - « Decorateurs »

Concepts - « Metaclass »

Concepts - « WSGI »

Couche modèle

ORM (object-relational mapping)

  • SQLAlchemy (http://www.sqlalchemy.org/)
  • Storm (https://storm.canonical.com/)
  • SQLObject (http://www.sqlobject.org/)
  • Django SQL (http://www.djangoproject.com/)

NoSQL

  • ZODB - a native object database for Python (http://www.zodb.org/)
  • couchdbkit - CouchDB framework in Python (http://couchdbkit.org/)
  • mongokit (http://bitbucket.org/namlook/mongokit/wiki/Home)
  • redis-py (http://github.com/andymccurdy/redis-py/)
  • python-cassandra (http://cassandra.apache.org/)
  • ...

Moteurs de templates

  • Mako (http://www.makotemplates.org/)
  • Jinja2 (http://jinja.pocoo.org/2/)
  • Chameleon (http://chameleon.repoze.org/)
  • Genshi (http://genshi.edgewall.org/)
  • Django Template
  • ...

Framework web

  • Pylons (http://pylonshq.com/)
  • Turbogears (http://turbogears.org/2.0/)
  • Django (http://www.djangoproject.com/)
  • Flask (http://flask.pocoo.org/)
  • Bootle (http://bottle.paws.de/)
  • BFG (repoze - http://bfg.repoze.org/)
  • Grok (http://grok.zope.org/)
  • Zope2 (http://www.zope.org/)
  • Zope3 (http://wiki.zope.org/zope3/Zope3Wiki)
  • ...

Formulaires

  • formencode (http://formencode.org/)
  • formalchemy (http://code.google.com/p/formalchemy/)
  • wtforms (http://wtforms.simplecodes.com/)
  • ...

URL Dispatcher

  • Routes (http://routes.groovie.org/)
  • Werkzeug (http://werkzeug.pocoo.org/)
  • CherryPy (http://www.cherrypy.org/)
  • ...

Authentication / Authorization tools

  • Authkit (http://authkit.org/)
  • repoze.who (http://static.repoze.org/whodocs/)
  • repoze.what (http://what.repoze.org/docs/1.0/)
  • python-openid (http://pypi.python.org/pypi/python-openid/)
  • OAuth (http://oauth.net/code/)

WSGI / Serveurs

  • Paste (Tools for using a Web Server Gateway Interface stack) http://pythonpaste.org/
  • WebOb (WebOb provides objects for HTTP requests and responses) http://pythonpaste.org/webob/
  • gunicorn (WSGI HTTP Server for UNIX) http://gunicorn.org/
  • tornado (Tornado is an open source version of the scalable, non-blocking web server and and tools that power FriendFeed) http://www.tornadoweb.org/
  • modwsgi (Python WSGI adapter module for Apache) http://code.google.com/p/modwsgi/
  • deliverance (Deliverance transforms HTML to theme pages) http://packages.python.org/Deliverance/

Serveur divers

  • twisted (event-driven networking engine written in Python) http://twistedmatrix.com/trac/
  • Lamson (The Python SMTP Server)
    http://lamsonproject.org/

PDF, image, graph

  • reportlabs - The ReportLab Toolkit is the time-proven, ultra-robust open-source engine for programatically creating PDF documents
    http://www.reportlab.com/software/opensource/
  • pypdf - A Pure-Python library built as a PDF toolkit
    http://pybrary.net/pyPdf/
  • pycairo
    http://cairographics.org/pycairo/
  • PIL (Python Imaging Library)
    http://www.pythonware.com/products/pil/
  • python-graph (A library for working with graphs in Python)
    http://code.google.com/p/python-graph/
  • Python Google Chart
    http://pygooglechart.slowchop.com/

Divers

  • webhelpers
    http://webhelpers.groovie.org/
  • Celery - Distributed Task Queue
    http://celeryproject.org/
  • restkit - It allows you to easily access to HTTP resource and build objects around it
    http://pypi.python.org/pypi/restkit/
  • Paramiko - SSH2 protocol for python
    http://www.lag.net/paramiko/
  • Beaker
    http://beaker.groovie.org/
  • babel (A collection of tools for internationalizing Python applications)
    http://pypi.python.org/pypi/Babel/

Divers

  • processing - Package for using processes which mimics the threading module
    http://pypi.python.org/pypi/processing
  • multiprocessing - Process-based “threading” interface
    http://docs.python.org/library/multiprocessing.html
  • Blinker - Blinker provides fast & simple object-to-object and broadcast signaling for Python objects.
    http://discorporate.us/projects/Blinker/
  • zope.interface - Interfaces for Python
    http://pypi.python.org/pypi/zope.interface
  • zope.component - Zope Component Architecture
    http://pypi.python.org/pypi/zope.component
  • ABC - Abstract Base Classes
    http://docs.python.org/library/abc.html

Divers

  • lazy loader http://packages.python.org/importers/ (futur) http://pypi.python.org/pypi/apipkg/
  • deprecated http://wiki.python.org/moin/PythonDecoratorLibrary#GeneratingDeprecationWarnings

    1 @deprecated
    2 def some_old_function(x,y):
    3     return x + y
    
  • plex - Plex is a library building lexical analysers http://pypi.python.org/pypi/plex/2.0.0dev

  • simplejson - Simple, fast, extensible JSON encoder/decoder for Python http://pypi.python.org/pypi/simplejson/
  • pyyaml - the home of various YAML implementations for Python http://pyyaml.org/

Divers

  • prettytable - A simple Python library for easily displaying tabular data in a visually appealing ASCII table format.
    http://pypi.python.org/pypi/PrettyTable
  • argparse / optparse - Python command-line parsing library
    http://pypi.python.org/pypi/argparse/
  • ConfigParser / ConfigObject / ZConfig
  • cmd2 (Extra features for standard library's cmd module)
    http://pypi.python.org/pypi/cmd2/
  • ctype - ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
    http://docs.python.org/library/ctypes.html
  • Redland RDF Language Bindings
    http://librdf.org/docs/python.html

XML

  • ElementTree (dans python)
  • lxml (lxml is the most feature-rich and easy-to-use library for working with XML and HTML in the Python language)
    http://codespeak.net/lxml/
  • pyquery - a jquery-like library for python
    http://packages.python.org/pyquery/

  • beautiful soup - You didn't write that awful page. You're just trying to get some data out of it. Right now, you don't really care what HTML is supposed to look like.
    http://www.crummy.com/software/BeautifulSoup/

Parser

  • docutils (Python Documentation Utilities - reStructuredText) http://docutils.sourceforge.net/

  • markdown (Python implementation of Markdown)
    http://pypi.python.org/pypi/Markdown

  • creole (Parser for WikiCreole text markup)
    http://code.google.com/p/python-creole/

Proling, Checker, Debug

  • pep8 (pep8 is a tool to check your Python code against some of the style conventions in PEP 8.)
    http://pypi.python.org/pypi/pep8

  • meliae / Guppy (Python Memory Usage Analyzer)
    http://pypi.python.org/pypi/meliae/

  • pdb - The Python Debugger
    http://docs.python.org/library/pdb.html

    1 import pdb
    2 pdb.set_trace
    

Proling, Checker, Debug

  • pylint - python code static checker
    http://pypi.python.org/pypi/pylint

  • ipdb (IPython-enabled pdb)
    http://pypi.python.org/pypi/ipdb/

  • cprofile
    http://docs.python.org/library/profile.html

Outils de tests

  • nose - is nicer testing for python
    http://somethingaboutorange.com/mrl/projects/nose/

    1 def test_foobar():
    2     assert 1 == 1
    
  • doctests - Test interactive Python examples
    http://docs.python.org/library/doctest.html

    1 Ceci est un test
    2 >>> a = 1
    3 >>> a
    4 1
    
  • mechanize - Stateful programmatic web browsing in Python
    http://wwwsearch.sourceforge.net/mechanize/

Outils de tests

  • selenium - Python bindings for Selenium
    http://pypi.python.org/pypi/selenium

  • doctest2 - The new features in unittest for Python 2.7 backported to Python 2.3+.
    http://pypi.python.org/pypi/unittest2

  • coverage (Code coverage measurement for Python)
    http://pypi.python.org/pypi/coverage

Outils de tests

  • buildbot - The BuildBot is a system to automate the compile/test cycle required by most software projects to validate code changes.
    http://buildbot.net/

  • mocker - Graceful platform for test doubles in Python (mocks, stubs, fakes, and dummies).
    http://pypi.python.org/pypi/mocker/

Documentation

  • Sphinx - Sphinx is a tool that makes it easy to create intelligent and beautiful documentation http://sphinx.pocoo.org/

  • Docutils - reStructuredText is an easy-to-read, what-you-see-is-what-you-get plaintext markup syntax and parser system.
    http://docutils.sourceforge.net/rst.html

  • Epydoc - Automatic API Documentation Generation for Python
    http://epydoc.sourceforge.net/

Gestion de version décentralisée

  • Mercurial - Mercurial is a free, distributed source control management tool.
    http://mercurial.selenic.com/

  • Bazaar - Bazaar is a version control system that helps you track project history over time and to collaborate easily with others.
    http://bazaar.canonical.com/en/

  • Git - Git is a free & open source, distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
    http://git-scm.com/

Gestionnaire de bug

  • Trac - Trac is an enhanced wiki and issue tracking system for software development projects.
    http://trac.edgewall.org/

  • Roundup - A simple-to-use and -install issue-tracking system with command-line, web and e-mail interfaces. Highly customisable.
    http://pypi.python.org/pypi/roundup

Accélérateur

  • Psycho - Psyco is a Python extension module which can greatly speed up the execution of any Python code.
    http://psyco.sourceforge.net/
  • Pyrex - Pyrex lets you write code that mixes Python and C data types any way you want, and compiles it into a C extension for Python.
    http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/

Accélérateur

  • Cython - Cython is a language that makes writing C extensions for the Python language as easy as Python itself.
    http://cython.org/

Exemple :

 1 def f(double x):
 2     return x**2-x
 3 
 4 def integrate_f(double a, double b, int N):
 5     cdef int i
 6     cdef double s, dx
 7     s = 0
 8     dx = (b-a)/N
 9     for i in range(N):
10         s += f(a+i*dx)
11     return s * dx

4 times speedup over the pure Python version

1 cdef double f(double) except? -2:
2     return x**2-x

Speedup: 150 times over pure Python.

Package / Déploiement

  • distutils / distribute - Easily download, build, install, upgrade, and uninstall Python packages http://pypi.python.org/pypi/distribute

  • PyPI - The Python Package Index is a repository of software for the Python programming language. There are currently 10742 packages here.
    http://pypi.python.org/pypi

  • sliverlining - Cloud Server
    http://cloudsilverlining.org/

  • fabric - Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
    http://docs.fabfile.org/

Package / Déploiement

  • py2app - Create standalone Mac OS X applications with Python
    http://pypi.python.org/pypi/py2app/

  • py2exe - is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.
    http://www.py2exe.org/

  • PIP - pip installs packages. Python packages. An easy_install replacement
    http://pypi.python.org/pypi/pip

Package / Déploiement

  • Virtualenv - Virtual Python Environment builder http://pypi.python.org/pypi/virtualenv

  • distutils2 - Futur distutils http://hg.python.org/distutils2

  • zc.buildout - Buildout is a Python-based build system for creating, assembling and deploying applications from multiple parts, some of which may be non-Python-based. It lets you create a buildout configuration and reproduce the same software later.
    http://www.buildout.org/