background preloader

Help and Tips

Facebook Twitter

10 common security gotchas in Python and how to avoid them. Faster Python with Cython and PyPy: Part 2. Last week I started this two-part blog series about increasing the speed of Python through Cython and PyPy.

Faster Python with Cython and PyPy: Part 2

While we already discussed the effects of Cython, I wanted to continue the discussion with a look at PyPy and then a contrast of both PyPy and Cython. How Fast Is PyPy? PyPy is an alternative Python interpreter and just-in-time (JIT) compiler that is highly compatible (subject to a few caveats) with the CPython interpreter. It is designed for speed and efficiency and uses a tracing JIT compiler to optimize frequently executed parts of the program at run time, thus increasing the execution speed. Python Is Not Java (dirtSimple.org) I was recently looking at the source of a wxPython-based GUI application, about 45.5KLOC in size, not counting the libraries used (e.g.

Python Is Not Java (dirtSimple.org)

Twisted). How to use Virtualenv in Python. Decorator Design Pattern. Intent Attach additional responsibilities to an object dynamically.

Decorator Design Pattern

Decorators provide a flexible alternative to subclassing for extending functionality.Client-specified embellishment of a core object by recursively wrapping it.Wrapping a gift, putting it in a box, and wrapping the box. Problem You want to add behavior or state to individual objects at run-time. Inheritance is not feasible because it is static and applies to an entire class.

Discussion. Understanding Python Decorators in 12 Easy Steps! Ok, perhaps I jest.

Understanding Python Decorators in 12 Easy Steps!

As a Python instructor, understanding decorators is a topic I find students consistently struggle with upon first exposure. That’s because decorators are hard to understand! Getting decorators requires understanding several functional programming concepts as well as feeling comfortable with some unique features of Python’s function definition and function calling syntax. *Using* decorators is easy (see Section 10)! 30 Python Language Features and Tricks You May Not Know About.

1 Introduction Since I started learning Python, I decided to maintain an often visited list of "tricks".

30 Python Language Features and Tricks You May Not Know About

Any time I saw a piece of code (in an example, on Stack Overflow, in open source software, etc.) that made me think "Cool! I didn't know you could do that! " I experimented with it until I understood it and then added it to the list. This post is part of that list, after some cleaning up. Program Configuration in Python. Simple & Profound Python Tricks. 7 tips to Time Python scripts and control Memory & CPU usage. When running a complex Python program that takes quite a long time to execute, you might want to improve its execution time.

7 tips to Time Python scripts and control Memory & CPU usage

But how? First of all, you need the tools to detect the bottlenecks of your code, i.e. which parts take longer to execute. This way, you can concentrate in speeding these parts first. And also, you should also control the memory and CPU usage, as it can point you towards new portions of code that could be improved. Therefore, in this post I’ll comment on 7 different Python tools that give you some insight about the execution time of your functions and the Memory and CPU usage. 1.

Python IAQ: Infrequently Answered Questions. A question is infrequently answered either because few people know the answer or because it concerns an obscure, subtle point (but a point that may be crucial to you).

Python IAQ: Infrequently Answered Questions

I thought I had invented the term for my Java IAQ, but it also shows up at the very informative About.com Urban Legends site. There are lots of Python FAQs around, but this is the only Python IAQ, except for the Chinese translation of this page by Weiyang Chen, the Russian translation by Alexander Sviridenko, and the Japanese translation by Akihiro Takizawa. (There are a few Infrequently Asked Questions lists, including a satirical one on C.) What never? Well, hardly ever. Wrong. DevDocs/Python. Closures In Python. You might have lived a long and happy coding life without ever needing to know what closures are.

Closures In Python

For one, many languages like C or C++ don't even support those (edit: C++ does support them via lambdas, of course. Sorry for any misinformation). Many functional languages rely heavily on closures. The definitive guide on how to use static, class or abstract methods in Python. Doing code reviews is a great way to discover things that people might struggle to comprehend.

The definitive guide on how to use static, class or abstract methods in Python

While proof-reading OpenStack patches recently, I spotted that people were not using correctly the various decorators Python provides for methods. So here's my attempt at providing me a link to send them to in my next code reviews. :-) A method is a function that is stored as a class attribute. You can declare and access such a function this way: >>> class Pizza(object):... def __init__(self, size):... self.size = size... def get_size(self):... return self.size... >>> Pizza.get_size<unbound method Pizza.get_size> What Python tells you here, is that the attribute get_size of the class Pizza is a method that is unbound. Python Cheat Sheet.

String String Methods Array Indexes and Slices a=[0,1,2,3,4,5] 6 len(a) 0 a[0] 5 a[5] 5 a[-1] 4 a[-2] [1,2,3,4,5] a[1:] [0,1,2,3,4] a[:5] [0,1,2,3] a[:-2] [1,2] a[1:3] [1,2,3,4] a[1:-1] Shallow copy of a b=a[:] Math Constants math.pi The mathematical constant π = 3.141592..., to available precision. math.e The mathematical constant e = 2.718281..., to available precision.

Python Cheat Sheet

Random Functions. The Python “with” Statement by Example. Python’s with statement was first introduced five years ago, in Python 2.5. It’s handy when you have two related operations which you’d like to execute as a pair, with a block of code in between. The classic example is opening a file, manipulating the file, then closing it: with open('output.txt', 'w') as f: f.write('Hi there! ') The above with statement will automatically close the file after the nested block of code.

(Continue reading to see exactly how the close occurs.) Advanced Python or Understanding Python. Planet Python. Documentation Index. Eventual Consistency. Python comes with a large number of built-in functions, operators and keywords. They make working with data structures and built-in types very easy, but usually when we define our own data types (classes) we also tend to come up with our own ways to manipulate and consume our data. One of the nice things in python, is that we don't have to.

We can use "underscore methods" to make our classes compatible with the built-in functions and operators. This makes our code easier to use and does a better job of hiding our nasty, complex implementation from the user. More importantly, it makes our code more intuitive. Python Iteration. Created 24 April 2012, last updated 19 March 2013 This is a presentation I gave at PyCon 2013. You can read the slides and text on this page, or open the actual presentation in your browser (use right and left arrows to advance the slides), or watch the video: A talk for PyCon 2013. This talk is billed as Beginner, and sounds like a beginner topic, but I prefer to think of it as Fundamental. Plenty of expert Python programmers aren't making enough use of the tools I'm going to talk about. Python has a nice model of abstract iteration which can be used to increase the expressiveness of your programs.

Python Community Sites

Alex Martelli Python Materials. Pypy - hacking it, using it. Speed/PerformanceTips. This page is devoted to various tips and tricks that help improve the performance of your Python programs. Wherever the information comes from someone else, I've tried to identify the source. Python has changed in some significant ways since I first wrote my "fast python" page in about 1996, which means that some of the orderings will have changed. I migrated it to the Python wiki in hopes others will help maintain it. You should always test these tips with your application and the specific version of the Python implementation you intend to use and not just blindly accept that one method is faster than another.

See the profiling section for more details. Also new since this was originally written are packages like Cython, Pyrex, Psyco, Weave, Shed Skin and PyInline, which can dramatically improve your application's performance by making it easier to push performance-critical code into C or machine language. Other Versions. What are the drawbacks of Stackless Python. Python - What stackless programming languages are available. 3. Using Python on Windows — Python v2.7.2 documentation. This document aims to give an overview of Windows-specific behaviour you should know about when using Python on Microsoft Windows. Unlike most Unix systems and services, Windows does not include a system supported installation of Python. To make Python available, the CPython team has compiled Windows installers (MSI packages) with every release for many years.

These installers are primarily intended to add a per-user installation of Python, with the core interpreter and library being used by a single user. The installer is also able to install for all users of a single machine, and a separate ZIP file is available for application-local distributions. A Python for Windows Tutorial. This document discusses some of the common pitfalls in getting python scripts running under Windows, with an emphasis on enabling python cgi scripts through Windows Apache.

On the PYTHONPATH. PyCon 2010:The Mighty Dictionary (#55) Pycon 2010 atlanta presents the mighty dictionary (#55) by brandon craig rhodes video produced by carl karsten & a team in conjunction with the psf and support from: [sauce ___ ijwbitz you too can support the psf: Default Parameter Values in 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. Hidden features of Python.