Saturday, 23 April 2011

Python 'with' statement

'with' statement is quite useful nice feature added to python 2.5. Good description is presented at Understanding Python's "with" statement. Following is an useful excerpt:
 
class controlled_execution:
        def __enter__(self):
            set things up
            return thing
        def __exit__(self, type, value, traceback):
            tear things down

    with controlled_execution() as thing:
         some code

or some more tangible example:

>>> with open("my.txt",'w') as f:
...    f.write('hello')
... 
>>> f
<closed file 'my.txt', mode 'w' at 0xb774b2e0>
root@kibo-kubuntu:~# ls
my.txt
root@kibo-kubuntu:~# cat my.txt ; echo
hello 

Nice and easy! huh?!

Antispam for the masses

I recently bumped into a nice project SAUCE. It's daemon sits in front of your good old SMTP daemon (postfix, sendmail,..) and does some aggressive spam assassinations! ;) a good companion for SpamAssassin at the heart of Zimbra!

Python console tab tab autocomplete!

This peace of simple code saves a lot of time! For more info check http://docs.python.org/tutorial/interactive.html


>>> import readline, rlcompleter
>>> readline.parse_and_bind("tab: complete")
>>> import daemon
>>> daemon.
daemon.__class__(         daemon.__name__           daemon.__subclasshook__(
daemon.__delattr__(       daemon.__new__(           daemon.basic_daemonize(
daemon.__dict__
daemon.__doc__            daemon.__reduce__(        daemon.daemonize(
daemon.__file__           daemon.__reduce_ex__(     daemon.errno
daemon.__format__(        daemon.__repr__(
daemon.__getattribute__(  daemon.__setattr__(       daemon.sys
daemon.__hash__(          daemon.__sizeof__(        daemon.writePID(
daemon.__init__(          daemon.__str__(  

If you want to go fancy then try IPython or bPython.
root@kibo-kubuntu:~# apt-get install ipython
root@kibo-kubuntu:~# ipython Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) Type "copyright", "credits" or "license" for more information. IPython 0.10 -- An enhanced Interactive Python. ?         -> Introduction and overview of IPython's features. %quickref -> Quick reference. help      -> Python's own help system. object?   -> Details about 'object'. ?object also works, ?? prints more. In [1]: import daemon In [2]: daemon. daemon.__builtins__      daemon.__doc__           daemon.__hash__           daemon.__class__         daemon.__file__          daemon.__init__           daemon.__reduce__        daemon.__sizeof__        daemon.checkPID           daemon.sys daemon.__delattr__       daemon.__format__        daemon.__name__           daemon.__reduce_ex__     daemon.__str__           daemon.daemonize          daemon.writePID daemon.__dict__          daemon.__getattribute__  daemon.__new__            daemon.__repr__          daemon.__subclasshook__  daemon.errno             In [2]: daemon.

While you are there, have a look at IPipe which has got some fantastic features.
Even more intresting would be some graphical IDE, isn't it?! then you might want to try wxPython

root@kibo-kubuntu:~# cat /etc/debian_version squeeze/sid root@kibo-kubuntu:~# apt-get install python-wxgtk2.6 python-wxtools wx2.6-i18n
kibo@kibo-kubuntu:~$ pycrust 
PyCrust 0.9.8 - The Flakiest Python Shell Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. from wxPython.wx import * /usr/bin/pycrust:1: DeprecationWarning: The wxPython compatibility <snip>   #!/usr/bin/python2.6 f = wxFrame(None, -1, "Hello world!") p = wxPanel(f, -1) b = wxButton(p, -1, "Click me!",(10,10)) f.Show <bound method Frame.Show of <wx._windows.Frame; <snip> f.Show() True

But fun doesn't end here, the most sophisticated one is PyDev which is an Eclipse plugin.If you don't already know these, then you have plenty to learn. But for an easy ride installation try the latest Aptana 3.0 which comes with PyDev pre-installed plus loads of other goodies. You will need JRE to run it. No installation is necessary just download the package, unzip then run AptanaStudio3. Then you are on your own after that! ;)