background preloader

Python

Facebook Twitter

PythonBooks - Learn Python the easy way ! Quantwiki. Svn.python.org/projects/python/trunk/Misc/cheatsheet. Full Stack Python. Graph-tool: Efficent network analysis with python. Talks and Tutorials. Here are links to selected talks and tutorials given at user-group meetings and conferences. Most of these talks can also be found on SlideShare. I also have a YouTube Channel where you can find videos. Most of these talks can also be found on pyvideo.org. Builtin Superheroes. Fear and Awaiting in Async: A Savage Journey to the Heart of the Coroutine Dream.

Topics of Interest (Python Asyncio). Python Concurrency From the Ground Up: LIVE!. Modules and Packages: Live and Let Die! Generators: The Final Frontier [ video. ] Presented at PyCon 2014 in Montreal, April 10, 2014. Discovering Python. Learn Python Through Public Data Hacking [ video ]. Python 3 Metaprogramming [ video ]. Python: A Toy Language. Let's Talk About PyPy. Low Level RPython. Understanding RPython. Embracing the Global Interpreter Lock (GIL). In Search of the Perfect Global Interpreter Lock. Using Python 3 to Build a Cloud Computing Service for my Superboard II [PDF]. Mastering Python 3 I/O (rev 2). Google Python Style Guide. No whitespace inside parentheses, brackets or braces. No whitespace before a comma, semicolon, or colon.

Do use whitespace after a comma, semicolon, or colon except at the end of the line. Yes: if x == 4: print x, y x, y = y, x No: if x == 4 : print x , y x , y = y , x No whitespace before the open paren/bracket that starts an argument list, indexing or slicing. Yes: dict['key'] = list[index] No: dict ['key'] = list [index] Surround binary operators with a single space on either side for assignment (=), comparisons (==, <, >, ! Don't use spaces around the '=' sign when used to indicate a keyword argument or a default parameter value. Yes: def complex(real, imag=0.0): return magic(r=real, i=imag) No: def complex(real, imag = 0.0): return magic(r = real, i = imag) Don't use spaces to vertically align tokens on consecutive lines, since it becomes a maintenance burden (applies to :, #, =, etc.

Python Cost Model - 6.006 Wiki. From 6.006 Wiki Python is a high-level programming language, with many powerful primitives. Analyzing the running time of a Python program requires an understanding of the cost of the various Python primitives. For example, in Python, you can write: where L, L1, and L2 are lists; the given statement computes L as the concatenation of the two input lists L1 and L2. Our goal in this section is to review various Python primitive operations, and to determine bounds and/or estimates on their running times. The Python implementation code base is here. Python Running Time Experiments and Discussion The running times for various-sized inputs were measured, and then a least-squares fit was used to find the coefficient for the high-order term in the running time. The least-squares fit was designed to minimize the sum of squares of relative error, using scipy.optimize.leastsq. The machine used was an IBM Thinkpad T43p with a 1.86GHz Pentium M processor and 1.5GB RAM.

Cost of Python Integer Operations. Runestone Interactive. Welcome to Problem Solving with Algorithms and Data Structures — Problem Solving with Algorithms and Data Structures. Python2web.com. Introspection (python) Code Like a Pythonista: Idiomatic Python. In this interactive tutorial, we'll cover many essential Python idioms and techniques in depth, adding immediately useful tools to your belt.

There are 3 versions of this presentation: ©2006-2008, licensed under a Creative Commons Attribution/Share-Alike (BY-SA) license. My credentials: I am a resident of Montreal,father of two great kids, husband of one special woman,a full-time Python programmer,author of the Docutils project and reStructuredText,an editor of the Python Enhancement Proposals (or PEPs),an organizer of PyCon 2007, and chair of PyCon 2008,a member of the Python Software Foundation,a Director of the Foundation for the past year, and its Secretary. In the tutorial I presented at PyCon 2006 (called Text & Data Processing), I was surprised at the reaction to some techniques I used that I had thought were common knowledge. Many of you will have seen some of these techniques and idioms before. These are the guiding principles of Python, but are open to interpretation.

Import this. PyCon 2014, full conference on YouTube. How to Write a Spelling Corrector. In the past week, two friends (Dean and Bill) independently told me they were amazed at how Google does spelling correction so well and quickly. Type in a search like [speling] and Google comes back in 0.1 seconds or so with Did you mean: spelling. (Yahoo and Microsoft are similar.) What surprised me is that I thought Dean and Bill, being highly accomplished engineers and mathematicians, would have good intuitions about statistical language processing problems such as spelling correction. But they didn't, and come to think of it, there's no reason they should: it was my expectations that were faulty, not their knowledge.

I figured they and many others could benefit from an explanation. So here, in 21 lines of Python 2.5 code, is the complete spelling corrector: import re, collections def words(text): return re.findall('[a-z]+', text.lower()) def train(features): model = collections.defaultdict(lambda: 1) for f in features: model[f] += 1 return model alphabet = 'abcdefghijklmnopqrstuvwxyz' Crossplatform Framework for NUI. Welcome - Learn Python - Free Interactive Python Tutorial. How to use Fabric in Python. Python Tutorials. Code Like a Pythonista: Idiomatic Python. The Python Challenge. Python Online Test. How to Install MySQL with Fabric — muhuk. I just want to share a small fabfile snipplet that installs mysql-server package on a Debian machine if it’s not already installed.

Actually it should have been quite straightforward; just issue an apt-get command, right? But during configuration it displays a curses dialog for MySQL root password. This of course blows your automated configuration plans. We will seed this value to debconf database to avoid this dialog. First let’s have a look at the supporting code: from __future__ import with_statementfrom fabric.api import *from fabric.utils import warn def apt_get(*packages): sudo('apt-get -y --no-upgrade install %s' % ' '.join(packages), shell=False) Our apt_get fabric command issues apt_get install that answers yes to all questions and does not upgrade if the requested package is already installed.

First we want to make sure mysql-server is not already installed. The rest of the code is pretty straightforward. Finally, having finished our little dance, we install mysql-server. Improve Your Python: 'yield' and Generators Explained. Prior to beginning tutoring sessions, I ask new students to fill out a brief self-assessment where they rate their understanding of various Python concepts. Some topics ("control flow with if/else" or "defining and using functions") are understood by a majority of students before ever beginning tutoring.

There are a handful of topics, however, that almost all students report having no knowledge or very limited understanding of. Of these, "generators and the yield keyword" is one of the biggest culprits. I'm guessing this is the case for most novice Python programmers. Many report having difficulty understanding generators and the yield keyword even after making a concerted effort to teach themselves the topic. I want to change that.

What is a Python Generator (Textbook Definition) If this doesn't make any sense to you, don't worry. Note: In recent years, generators have grown more powerful as features have been added through PEPs. Coroutines and Subroutines Example: Fun With Prime Numbers. Fabric tutorial 1 – Take command of your network | awaseroot. This is a guide for installing and using Fabric on Ubuntu. Tested versions: Ubuntu 11.10 Fabric 1.4.1 Fabric is a Python tool and a library for combining Python with SSH. The tool can be used to execute Python functions from the command line. It’s also possible to execute shell commands over SSH with Fabric and by combining the Python functions with the SSH, it’s possible to automate system administration tasks. (fabfile.org) Why Fabric? You can use Fabric as a tool for centralized configuration management. Fabric doesn’t require anything else on the remote systems but a working SSH server.

In these tutorials I will go through the installation and all the basics you need to start using Fabric efficietly. The installation The recommended way to install Fabric is by using pip. If you encounter any errors make sure you have at least Python 2.6 and pip 0.8.1 or above. Making your first fabfile Fabric uses a Python file called fabfile.py to execute tasks. Def hello(): print("Hello World! ") Done.

Django

Speeding Up Your Python Code - Max Burstein's Blog. In my opinion the Python community is split into 3 groups. There's the Python 2.x group, the 3.x group, and the PyPy group. The schism basically boils down to library compatibility issues and speed. This post is going to focus on some general code optimization tricks as well as breaking out into C to for significant performance improvements. I'll also show the run times of the 3 major python groups. Using Generators One commonly overlooked memory optimization is the use of generators. Not only is it slightly faster but you also avoid storing the entire list in memory! Introducing Ctypes For performance critical code Python natively provides us with an API to call C functions. Just by switching to the C random function we cut our run time in half!

Introducing Cython Cython is a superset of Python that allows for the calling of C functions as well as declaring types on variables to increase performance. Sudo pip install cython Compile using: python setup.py build_ext --inplace Introducing PyPy. Porting to Python 3: An in-depth guide — Porting to Python 3 - The Book Site. How To Install Python, pip, and virtualenv on Windows with PowerShell : Tyler Butler. Important note: As of February 2014 this guide is a little out of date. I intend to update it soon; I simply haven’t had the time yet. Working on it! Updated May 12, 2013 If you do any Python development, you’ll probably run into an awful lot of package installation instructions that read: To install, use pip:pip install engineer Now, that’s all fine and dandy, but what is pip? If you’re new to Python, getting up and running with pip and virtualenv can be a challenge, especially on Windows. Before We Start A brief note before we start… To make sure we’re all on the same page, pip is a Python package installer.

The more Python development you do, though, the more packages you’re going to need. Finally, there’s a wrapper utility for virtualenv aptly called virtualenvwrapper. So with the definitions out of the way, let’s get started… Ensure You Can Execute PowerShell Scripts For the most part, this guide assumes you’ve actually used PowerShell a few times and know how to run scripts. Get Python.