Wednesday, July 13, 2005

Pyrex Rocks!

In the project I am writing now I need to interface to F-Secure daemon, to scan files for viruses. They provide a very nice SDK to be used with C/C++, but ofcourse nothing for Python.

Here's where I come in :) When you hear words 'SDK', 'library' and 'Python' in the same sentance, the next thing you should immidiately think of - Pyrex
It's a nifty tool that provides you a very Pythonic way to write glue code to different libraries. You define what you want to import from the library, define the API of the exported functions, then create a wrapper class to access those, and it compiles it into a nifty Python extension! All you are left to do afterwards, is 'import my_ext' and use the class from it!
Since in the past I was messing with Perl and XS, I can truly appriciate the beauty of this approch!

Tomorrow I think I'll document the code, make a package and post it to PyPi. Stay Tuned.

Tuesday, July 12, 2005

Python Locks

In the project I am developing right now there is a lot of threading done, and therefor some of the code needs to be protected by Mutexes. So, to save my self from finger-breaking, repetative task of lock aquiring and releasing, I wrote a small decorator (python 2.4) to help me out:
def lock_on(lock):
    def decorator(f):
        def wrapper(*args, **kwargs):
            lock.acquire()
            try:
                result = f(*args, **kwargs)
            finally:
                lock.release()
            return result
        return wrapper
    return decorator

Somewhere globally I have a hash of different mutexes, and when I need to create a critical section around some function, now all i have to do is this:
@locked_on(locks["some_mutex"])
def my_func(self):
    pass
Nifty!

Saturday, July 09, 2005

Unfreezing the blog

Hi there. I think I will be slowly unfreezing the blog now.
The pressure of the exams is dying down, I have some interesting project ideas, and basicly the mood is back :)
Due to the last changes in Blogger API, PyQLogger is broken and will not work (authentication problems). One day I will get to it.


Qt 4 is out now, and it's open source even on Windows. Many great, and appealing additions were made to it, so as soon as the nice folks at RiverBank will get to make PyQt4, i'll rewrite PyQLogger for it.
i've stumbled upon this gem today, and just had to share it. Man I love Python!

# Mandelbrot set
print (lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y
>=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(
64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24)
#    \___ ___  \___ ___  |   |   |__ lines on screen
#        V          V      |   |______ columns on screen
#        |          |      |__________ maximum of "iterations"
#        |          |_________________ range on y axis
#        |____________________________ range on x axis

Don't try this at home, kids!


Well, stay tuned, more posts are coming up!