Tuesday, December 16, 2014

Simple LXC status indicator

Here is a simple indicator to monitor the status of the LXC running on the machine, works perfectly on Ubuntu 14.04:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python

"""LXC monitor indicator."""

import appindicator
import commands
import gtk

if __name__ == "__main__":

    def on_timer(args=None):
      lxc_number = str(len(commands.getstatusoutput("lxc-ls --running")[1].split("\n")))
      ind.set_label(lxc_number)
      item.set_label(commands.getstatusoutput("lxc-ls -f")[1])
      return True

    ind = appindicator.Indicator(
        "lxc-monitor", 
        "monitor",
        appindicator.CATEGORY_APPLICATION_STATUS)
    ind.set_status (appindicator.STATUS_ACTIVE)
    ind.set_label("...");    
    menu = gtk.Menu()
    item = gtk.MenuItem("...")    
    item.show()
    menu.append(item)
    ind.set_menu(menu)
    gtk.timeout_add(1000, on_timer)    
    gtk.main() 

Wednesday, July 18, 2012

Test Doubles

Dummy


Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists. In Python, None is the ultimate Dummy.

Fake


Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).

Stub


Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.

Mock


Mocks are objects that are pre-programmed with expectations which form a specification of the calls they are expected to receive.

Lifted from Mocks aren't Stubs and derived from the excellent xUnit patterns book.

Wednesday, February 8, 2012

HTTP connection timeout

You wonder how to set a timeout on your HTTP connection?
Try like this:

import socket
import urllib2

socket.setdefaulttimeout(3) # 3 seconds

request = urllib2.Request('http://www.yourhost.com')
response = urllib2.urlopen(req)
#urllib2.urlopen will now uses the default timeout of 3 secs.

Thursday, March 3, 2011

Execute a shell command a given number of time.

A small utility to execute a shell command a given number of time:
#! /bin/bash

#Execute the command a given number of times
EXPECTED_ARGS=2
E_BADARGS=65

if [ $# < $EXPECTED_ARGS ]
then
    echo "Usage: `basename $0` N command"
   exit $E_BADARGS
fi

for ((n=0; n<$1; n++))
do
   ${@:2}
done

Thursday, December 18, 2008

The Zen of Python

 Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those! (Tim Peters)