Pear Code
MemoryError and numpy / matplotlib

If you see MemoryError in your application running within Apache (in my case with wsgi [mod_wsgi]) it is probably because of SELinux.

You have to allow httpd to execute code from the /tmp directory

root # setsebool httpd_tmp_exec on

More info: http://docs.fedoraproject.org/en-US/Fedora/13/html/Managing_Confined_Services/sect-Managing_Confined_Services-The_Apache_HTTP_Server-Booleans.html

Python SMTP Debugging Server

python -m smtpd -n -c DebuggingServer localhost:1025

Python pretty print

Simply “print” show objects in ugly format. If you want to see object nice formatted use instruction below:

from pprint import pprint

data = data = [(1, {'a':'A', 'b':'B', 'c':'C', 'd':'D'}), (2, {'e':'E', 'f':'F', 'g':'G', 'h':'H', 'i':'I', 'j':'J', 'k':'K', 'l':'L'})]
pprint(data, indent=4, depth=3)
[   (1, {   'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
    (   2,
        {   'e': 'E',
            'f': 'F',
            'g': 'G',
            'h': 'H',
            'i': 'I',
            'j': 'J',
            'k': 'K',
            'l': 'L'})]

The amount of indentation added for each recursive level is specified by indent; the default is one.

The number of levels which may be printed is controlled by depth. By default, there is no constraint on the depth of the objects being formatted.

Tab completion in Python shell

1. Create .pythonrc in your HOME directory

2. Add these lines to the previously created file:

import rlcompleter, readline
readline.parse_and_bind("tab: complete")

3. Export PYTHONSTARTUP variable in .bashrc file:

export PYTHONSTARTUP="$HOME/.pythonrc"
How to generate random string in Python

Simple:

import random, string 
''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))

where N is a number of characters used in a string.

Fibonacci in Python

Just to remember ;)

1. 0: n = 0
2. 1: n = 1
3. F(n-1) + F(n-2): n > 1
def fib(n):
    if n  
How to remove all .pyc files from a project

Command:

find . -name '*.pyc' -delete
Quicksort in Python

Just to remember ;)

Step 1: Get first element from a list

Step 2: Split the list into two parts

Step 3: Return result recursively

Step 4: Add conditions: List is not sorted if count of items in args list is greater than 1

def quick(args):
    if len(args) > 1:
        first = args[0]
        del args[0]

        left = [x for x in args if x = first]

        return quick(left) + [first] + quick(right)
    else:
        return args
Python __iter__ performance
$ python -m timeit -r 1000 -s "bla=[1,2,3]" "for item in bla:" "  item"
1000000 loops, best of 1000: 0.306 usec per loop

$ python -m timeit -r 1000 -s "bla=[1,2,3]" "lista=bla.__iter__()" "while True:" "  try:" "    lista.next()" "  except:" "    break"
100000 loops, best of 1000: 3.13 usec per loop

__iter__ + try..except + while is round 11x slower than “for in” loop.

Checking key in dictionary - performance experiment

Little experiment:

python -m timeit -r 100 -s "d={'a':'1', 'b':'2', 'c':'3'}" "d.has_key('a')"
1000000 loops, best of 100: 0.361 usec per loop
python -m timeit -r 100 -s "d={'a':'1', 'b':'2', 'c':'3'}" "'a' in d"
10000000 loops, best of 100: 0.159 usec per loop

Checking if dictionary has key using “in” is faster than using has_key() method.

Tested in Python 2.6