Archive for June, 2009

Filed Under (Python) by Marcin Kuźmiński on June-29-2009

Today a new version of Python came out Python 3.1. After having 2 RC ’s final version is available for download here
Here’s what they say about this release at http://python.org site.
Python 3.1 is a continuation of the work started by Python 3.0, the new backwards-incompatible series of Python. Improvements in this release include:

* An ordered dictionary type
* Various optimizations to the int type
* New unittest features including test skipping and new assert methods.
* A much faster io module
* Tile support for Tkinter
* A pure Python reference implementation of the import statement
* New syntax for nested with statements

And the what’s new link http://docs.python.org/dev/py3k/whatsnew/3.1.html

I’ll for sure will give it a try soon.



Filed Under (Python) by Marcin Kuźmiński on June-27-2009

UPDATE: i added two small fixes. When a functions is returning let’s pass the return value further, and exception when there’s no parameters.

I began studying more about decorators. I made a simple one for now it can be used to calculate execution time in miliseconds,seconds,minutes or hours of function decorated. It uses closures with wrapper function for handling parameters. Next time i’ll post decorator for running a function in a background using threads.

Here’s the code:

''' @author: marcink '''

import time
from types import FunctionType

class GetExecutionTime:
    '''
    This generic class is meant to be used as decorator for messuring
    simple method execution time it works on class methods as well
    as regular non class functions ;
    Availible parameters:
        'ms' - miliseconds
        's' - seconds
        'm' - minutes
        'h' - hours
    usage: @GetExecutionTime(param)

    samples:
    @GetExecutionTime('s')
    def samplemethod(self,params):
        function body
    will print executions time at end in seconds

    @GetExecutionTime('m')
    def samplemethod(params):
        function body
    will print executions time at end in minutes

     '''

    def __init__(self, *args):
        #in case of calling without parametrs
        #first argument will be the function decorated raise exception than

        if len(args) is 0 or type(args[0]) is FunctionType:
            raise Exception("Calling decorator without parameters is not possible")
        else:
            self.fn = None
            self.d_as = args[0]

        def miliseconds():return 1
        def seconds(): return 10000000000
        def minutes():return seconds() * 60
        def hours(): return minutes() * 60
        def default_():return seconds()

        #this is a cool way to replace switch case with default parameter
        commands = {'ms':miliseconds,
                    's':seconds,
                    'm':minutes,
                    'h':hours}
        self.display_as = commands.get(self.d_as, default_)()

    def __call__(self,fn):

        display_as = self.display_as
        d_as = self.d_as

        def wrapper_function(*args, **kwargs):
            start = time.time()

            ret = fn(*args,**kwargs)

            end = time.time()
            microsec = (end - start) * 10000000000

            print "function %s execution time: %s %s" % (fn.__name__, (microsec) / display_as, d_as)
            if ret:
                return ret
        return wrapper_function

#EXAMPLES:

from time import sleep

@GetExecutionTime('s')
def short_func(max, min):
    for x in range(3):
        sleep(1)
    print max,min
    return (max, min)

short_func(100, 10)

''' prints: function short_func execution time: 3.00342392921 s '''


Filed Under (Python) by Marcin Kuźmiński on June-26-2009

I found interesting short article about integer comparison in python: http://distilledb.com/blog/archives/date/2009/06/18/python-gotcha-integer-equality.page



Filed Under (Editors, Python) by Marcin Kuźmiński on June-25-2009

Today is Eclipse Galileo release day. So i made my new PyDev + eclipse compilation with some customizations.

I remove all the unnecessary stuff for java IDE since we program in python we don’t need java stuff in there( and it loads faster to :D )
replaced splash screen with the one from pyDev site, and throw some nice (Python) window icons.

Here’s the main window:
Screenshot-Pydev - Eclipse

Here’s what you have to do to have my setup:

  • Get the newest version of eclipse for java IDE (not the EE) use this direct link if you want download eclipse
  • extract folder eclipse to you hard drive and rename it to let’s say PyDev ;]
  • DON’T RUN ECLIPSE !! please
  • Ok here’s what you have to remove to get rid of all the java stuff
    (i have nothing against java, but we’re talking python here)
  • from folder features remove org.eclipse.jdt_3.5.0.v20090527-2000-7r88FEeFJePyvYeA33DjZ_c1
  • from folder plugins remove
    org.eclipse.jdt.debug_3.5.0.v20090526
    org.eclipse.jdt.apt.core_3.3.200.v20090528-1135.jar
    org.eclipse.jdt.apt.pluggable.core_1.0.200.v20090526-2130.jar
    org.eclipse.jdt.apt.ui_3.3.200.v20090528-1135.jar
    org.eclipse.jdt.compiler.apt_1.0.200.v20090528-1135.jar
    org.eclipse.jdt.compiler.tool_1.0.100.v_963.jar
    org.eclipse.jdt.core.manipulation_1.3.0.v20090603.jar
    org.eclipse.jdt.core_3.5.0.v_963.jar
    org.eclipse.jdt.debug.ui_3.4.0.v20090527.jar
    org.eclipse.jdt.doc.user_3.5.0.v20090603-1530.jar
    org.eclipse.jdt.junit.runtime_3.4.100.v20090513-2000.jar
    org.eclipse.jdt.junit4.runtime_1.1.0.v20090513-2000.jar
    org.eclipse.jdt.junit_3.5.0.v20090526-2000.jar
    org.eclipse.jdt.launching_3.5.0.v20090527.jar
    org.eclipse.jdt.ui_3.5.0.v20090604.jar
    org.eclipse.jdt_3.5.0.v200906111540.jar
  • Firstly i decided to remove epp.package.java but this really screws up eclipse and the icons so don’t do it
  • Now download this pydev icons  package pydev_icons and extract it somewhere
    if you don’t need the eclipse icon replacement you can skip this.
  • Copy and replace splash.bmp to folder plugins/org.eclipse.platform_3.3.200.v200906111540
    and paste rest of images to folder in plugins/org.eclipse.epp.package.java_1.2.0.20090619-0620
  • In case of newer eclipse builds (3.5.2) just copy everything to plugins/org.eclipse.platform_*.*(version number) and remove the gifs, so eclipse will load png
  • Your done !

Now run eclipse save you workspace and setup pydev as in this post:

http://www.python-blog.com/index.php/2009/06/20/python-ide-pydev-best-choice/

Enjoy lighter version of newest eclipse galileo with Pydev and some nice icons :)

UPDATE: I changed the splash screen in pydev icons to be the same size as original one.



Filed Under (Nginx, Python) by Marcin Kuźmiński on June-23-2009

Recently i made a simple random password generator with ability to make n-length passwords with appropriate hash of this password. Especially i was interested in CRYPT to create a password for nginx passwd files. You need to make a password hashed with crypt in order to put it to any *NIX passwd file. Which has a format of username:password using the to_crypt function you can create password for these type of access files. You can use any salt for the passwords.

Heres the code fell free to use it:

'''
@author: marcink
This is a simple class for generating password from
different sets of characters

functions sha1,md5 and crypt returns a tuple of (generated_password,hashed_password,type)

'''

from hashlib import sha1, md5
import random
import crypt

class PasswordGenerator(object):

    def __init__(self, passwd = ''):

        self.ALPHABETS_NUM = r'''1234567890'''#[0]
        self.ALPHABETS_SMALL = r'''qwertyuiopasdfghjklzxcvbnm'''#[1]
        self.ALPHABETS_BIG = r'''QWERTYUIOPASDFGHJKLZXCVBNM'''#[2]
        self.ALPHABETS_SPECIAL = r'''`-=[]\;',./~!@#$%^&*()_+{}|:"<>?'''    #[3]
        self.ALPHABETS_FULL = self.ALPHABETS_BIG + self.ALPHABETS_SMALL + self.ALPHABETS_NUM + self.ALPHABETS_SPECIAL#[4]
        self.ALPHABETS_ALPHANUM = self.ALPHABETS_BIG + self.ALPHABETS_SMALL + self.ALPHABETS_NUM#[5]
        self.ALPHABETS_BIG_SMALL = self.ALPHABETS_BIG + self.ALPHABETS_SMALL
        self.ALPHABETS_ALPHANUM_BIG = self.ALPHABETS_BIG + self.ALPHABETS_NUM#[6]
        self.ALPHABETS_ALPHANUM_SMALL = self.ALPHABETS_SMALL + self.ALPHABETS_NUM#[7]
        self.passwd = passwd

    def gen_password(self, len, type):
        #''.join(seq) is 30x faster that str_password+=random.choice(type)
        self.passwd = ''.join([random.choice(type) for _ in xrange(len) ])
        return self

    def to_md5(self):
        return (self.passwd, md5(self.passwd).hexdigest(),'md5')

    def to_sha1(self):
        return (self.passwd, sha1(self.passwd).hexdigest(),'sha1')

    def to_crypt(self, salt):
        return (self.passwd, crypt.crypt(self.passwd, salt),'crypt[salt=%s]' % salt)

    def to_plain(self):
        return self.passwd

if __name__ == "__main__":
    passwd_gen = PasswordGenerator()

    #print 8-letter password containing only big and small letters of alphabet
    print passwd_gen.gen_password(8, passwd_gen.ALPHABETS_BIG_SMALL).to_plain()

    #print 8 letter password containing numbers only and return crypt hash of it
    print passwd_gen.gen_password(8, passwd_gen.ALPHABETS_NUM).to_crypt(salt = 'ab')

    # create an instance of passwordGenerator with you own password
    my_custom_password = PasswordGenerator('my_secret_password')
    print my_custom_password.to_plain()

    #print md5 of that password
    print my_custom_password.to_md5()

    '''
    sample output:
    raeBvpdd
    ('10047981', 'abOaHGCbrkA7U', 'crypt[salt=ab]')
    my_secret_password
    ('my_secret_password', '7ee416d527a2d047751c026d82dbbeef', 'md5')
    '''


Filed Under (Editors, Python) by Marcin Kuźmiński on June-20-2009

I went through many of python editors, and always came back to PyDev. Pydev in an plug-in to eclipse it comes with load of features, among them:

  • Package Explorer showing outline for python modules
  • Mylyn integration (separate feature: org.python.pydev.mylyn.feature)
  • Jython and Python support!
  • Support for scripting Pydev with Jython
  • Nice PYTHONPATH configuration!
  • Project and module creation wizards
  • Ctrl+Shift+O: Organizes imports or sorts selection alphabetically
  • Ctrl+Shift+F: Autoformat your code (preferences can be set)
  • Python 2.4, 2.5, 2.6, 3.0 syntax supported
  • Pydev builders can be disabled
  • Syntax highlighting
  • Spell checking (depends on JDT for Eclipse 3.3)
  • Hovering in errors shows descriptions
  • Matching brackets highlighted.
  • Parser errors marked in the task list
  • Outline view with imports/functions/classes
  • Tabs or spaces preference
  • Smart indentation (indent and dedent)
  • Navigation: keyboard shortcuts to previous or next function. Default: (Ctrl+Shift+Up and Ctrl+Shift+Down)
  • Comment and uncomment commands (on the popup menu) and keybindings. Default: (Ctrl+3 and Ctrl+Shift+3)
  • hyperlinks over functions//import statements
  • Code folding
  • Refactoring with bicycle repair man.
  • Code Completion (Ctrl+Space)
  • Templates Completion (Ctrl+Space Too)
  • Go to definition with F3 (powered by bicycle repair man)
  • Content Assistant (Ctrl+1)
  • Code Coverage
  • Pylint
  • TODO tasks
  • Background and current line color chooser.

I highly recommend using Pylint in pydev it really helps to keep the code good quality. Pylint analyzes your code and shows unnecessary variables and imports, provides helpers to clean up the code and many more.

To install pydev just add http://pydev.sourceforge.net/updates/ http://pydev.org/updates (see update below) link to eclipse by using software updates and addons option.
I’m using the nightly builds for eclipse (http://nightly.aptana.com/pydev/) because they always come with new features earlier than the main release but remember use it on your own responsibility. You can find how to setup pydev here. When your up and running with pydev download pylint from here don’t forget the necessary dependencies i.e: logilab-astng, logilab-common, links are on the pylint download site. The turn on pylint you have to add a path to lint.py file in pylint tab on pydev options in eclipse. After restart of eclipse you should see output from pylint on the eclipse console anytime when you save a python file.

Now enjoy the most powerful python IDE available :-)

UPDATE: since PyDev release 1.5 now the package have integrated a pyDev extensions… the new repository for eclipse containing the version 1.5 is :http://pydev.org/updates
I also added a post about the PyDev extensions see it here http://www.python-blog.com/2009/09/06/pydev-extension-now-open-source/



Filed Under (Python) by Marcin Kuźmiński on June-20-2009

import antigravity

import antigravity“  funny thing ;] try to import it and see what happens.

If you don’t have Python 3000 i can tell you that it open the classic XKCD comic, in your webrowser.

That is along with import this and from __future__ import braces another Easter Egg in Python 3.0

Here is the commic for antigravity

python-comic-antigravity



Filed Under (Editors, Python) by Marcin Kuźmiński on June-20-2009

I was looking something to replace Gedit / Scite text editor, since  i work with large files (1+ mln lines) opening those files in gedit was a nightmare i took 5-8 minutes to open a file with almost 100% cpu load, and searching through the files was almost as slow as opening.

Before gedit i used scite the scintilla base text editor i was extremely fast it almost perfectly suited my needs except two things. Lack of other types of encoding than UTF-8 and in my opinion stupid options file that had to be edited manually since it doesn’t save options when you change it in scite. I looked for something to replace scite as well.

So i started looking for a scintilla based text editors, and i found one. Geany 0.17.

“Geany is a small and lightweight Integrated Development Environment. It was developed to provide a small and fast IDE, which has only a few dependencies from other packages. Another goal was to be as independent as possible from a special Desktop Environment like KDE or GNOME – Geany only requires the GTK2 runtime libraries.”

Some basic features of Geany:

  • Syntax highlighting
  • Code folding
  • Symbol name auto-completion
  • Construct completion/snippets
  • Auto-closing of XML and HTML tags
  • Call tips
  • Many supported filetypes including C, Java, PHP, HTML, Python, Perl, Pascal (full list)
  • Symbol lists
  • Code navigation
  • Build system to compile and execute your code
  • Simple project management
  • Plugin interface (see Plugins)
  • Built in console
  • Opens file in choosen encoding
  • Replaces EOF for WIN UNIX MAC formats

So now opening a file is taking 20 seconds :) It’s extremely fast, and have powerful search options. This is THE text editor I’ve been looking for…
A good replacement for gedit which is slow, and scite whitch is fast but week in configuration.
Highly recommended you can download a source /  win exe from this location download geany



Filed Under (Nginx, Pylons, Python) by Marcin Kuźmiński on June-19-2009

Hi welcome to python blog !

We are up and running. Some technical information about our site:

  • Nginx 0.7.59
  • SliceHost 256MB slice
  • PHP 5.2.9 patched with php-fpm
  • Debian 5.0 + grsec & selinux
  • Python 2.6.2

Soon I’ll post the setups i made for this site as well as some useful links for custom building php with php-fm .

Regards Marcin Kuzminski site Administrator