Entries Tagged 'Python' ↓

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.

Python – 5th place in TIOBE Programming Community Index

Python has jumped from 6th to 5th place in TIOBE Programming Community Index (May 2009). Congratulations! :)

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

How to write and read array from cookie in Pylons

How to write and read array from cookies ?

The easiest way to write an array in cookie is serialize it using pickle module and quote returned string with urllib module. For example:

import pickle, urllib
cookie = []
response.set_cookie('some_cookie', urllib.quote(pickle.dumps(cookie)))

And getting an array from cookie:

cookie = pickle.loads(str(urllib.unquote(request.cookies['some_cookie'])))

The easiest way to modify response headers in Pylons

Remember the trick!

The easiest way to modify response headers in Pylons is add:

response.headerlist = [('Content-type', 'image/png')]

just before render() function.

Simple solution – how to use own decorators in Pylons actions

This is a simple solution to create own decorator function for any action in Pylons.

First, you have to import some functions:

from decorator import decorator
from pylons.decorators.util import get_pylons # to get request environment

Now create decorator function, for example myowndecorator:

def myowndecorator():
    def wrapper(func, *args, **kwargs):
        request = get_pylons(args).request # use it for any operation with request
        # do something
        return func(*args, **kwargs)
    return decorator(wrapper)

Save it in your project lib directory as mylib.py.

In last step you have to import function from you library, and use decorator in your controller:

from myproject.lib.mylib import myowndecorator

class PanelController(BaseController):
    @myowndecorator()
    def index(self):
        return render('/panel/index.mako')

Pylons 0.9.7 has been released

New version(0.9.7) of Pylons framework has been released. It’s a big step to standardize code and documentation :)

Major changes:

  • Switched to using WebOb for the request/response object
  • Various performance improvements to object initialization
  • Beaker and Routes updates
  • Middleware improvements, and optimizations

New Pylons Site

I have noticed today pylonshq.com has new website :) It’s prettier than last one. and I hope it’s announce, that new stable version(0.9.7) of Pylons is comming soon :D