Solution to enable the apache module rewrite under Ubuntu

If mod rewrite is not working in your apache server under Ubuntu:

Invalid command 'RewriteEngine', perhaps mis-spelled or defined by a module not included in the server configuration

You have to run the following command in a terminal:

sudo a2enmod rewrite

:)

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.decorator.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')

Basic Authentication in Pylons

I had to create basic authentication in my project(based on Pylons), and this is simple solution to do it.

First, you have to open your config/middleware.py. It’s necessary to import AuthBasicHandler: from paste.auth.basic import AuthBasicHandler.

Next step is find ‘app = PylonsApp()‘. Change this line to: ap = PylonsApp() and add after: app = AuthBasicHandler(ap, “My realm”, authfunc). Of course you need to create auth function, for example:

def authfunc(environ, username, password):
    if username == 'myusername' and password == 'mypassword':
        return True
    else:
        return False

Restart your application environment.

That’s it :)

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

The Conference about “Microsoft SQL Server 2008″

sem2

On the 19 of February this year my University is going to organise conference about SQL Server 2008.
The topics:

- T-SQL Tips&Tricks
- SQL Server 2008 - what will give to developers and administrators
- Integration Services - something else aside than sending data

Conference’s place:

Wy ższa Szkoła Bankowa w Poznaniu Wydział Zamiejscowy w Chorzowie
Street: Sportowa 29
City: Chorzów
Start: 8:00

More information on the conference website: http://ms-groups.pl/plssug/Strony/SeminariumSQLServer2008.aspx

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