Category Archives: Fun

This is an old trick i had lost track of since i last used it. It resurfaced with the need in another program that i was writing and thought it apt to share it, and record it for my reference.  It is fairly simple. But i would summarize it before we go to the script.

  • Create a class to pass the instance of the stdout/stderr. Here i have chose to pass stderr.
  • Open the log file to which the stderr messages need to be recorded to- should be done in the constructor.
  • And then override the write method so that it writes both to the stderr and to the file.

Now you are basically done. See the follow code.

import sys

class logstderr:
    """
    logstderr class is used to log any output that
    is directed to the screen
    """
    def __init__(self, wrfile):
        """
        logstderr constructor
        takes the stream that needs to be logged
        """
        self.wrfile = wrfile
        self.log = file("log.log","w+")

    def write(self, str):
        """
        To override the write of
        sys.stderr.write
        """
        self.wrfile.write(str)
        self.log.write(str)

    def __del__(self):
        self.log.close()

if __name__=="__main__":
    lg =  logstderr(sys.stderr)
    sys.stderr = lg

    print >>sys.stderr,"Hello"
    print >>sys.stderr,"World!"

Many will be surprised that such a user friendly language as python does not provide something a like a switch-case. Wow! Isn’t that something that should come with the language by default. Why did python not opt it in? You could read this chain. But all the more reason to read this article to get around this inability and like all thing in python this too is fairly simple. You would kick yourself for not figuring out. I would put the code in first… here!

def madrid():
  print "Welcome to Madrid"

def geneva():
  print "Welcome to Geneva"

def hawaii():
  print "Welcome to Hawaii"

def unknown():
  print "I cannot take you there. Sorry!"

#destinations = {"madrid":madrid,"geneva":geneva,"hawaii":hawaii}
#incorporating drj11's suggestions for he feels awkward and its true.
#Thanks drj11
destinations = dict(madrid=madrid,geneva=geneva,hawaii=hawaii)

destination =  raw_input("Where do you want to go today? ")
destinations.get(destination,unknown)()

Two things that you need to take care while using the above given method

  1. Always create your dictionary after the function definition. You check out what happens if you dont do  that :)
  2. Make sure sure you pass the second argument to the dict.get method as this works like the “default” case of your standard switch statement.

The code is pretty straightforward and needs no explanation.

I had a hell of time doing creating Swig wrappers to access my C++ code/data/method from Python :(

After all those trail and errors of an evening i have tumbled upon a solution, which i should presume could be helpful and simple to many.

Pls note: I tried this on Archlinux, 32bit machine. The compilation steps may differ for various platforms

First lets see my example.h code…

//example.h
#include <iostream>
using namespace std;

class example{
public:
void foo();
void bar();
};

Here is my example.cpp file …

//example.cpp
#include "example.h"
void example::foo(){
cout<<"FOO";
}
void example::bar(){
cout<<"BAR";
}

And finally my Swig interface file example.i

%module example
%{
#include "example.h"
%}
%include "example.h"

Then its a few simple steps for compilation…

$ swig -c++ -python example.i
$ g++ -c example.cpp
$ g++ -c example_wrap.cxx -I/usr/local/include/python2.4
$ g++ -shared example.o example_wrap.o -o _example.so

Now ‘python’ and ….

>>> from example import example
>>> e=example()
>>> print e.foo()
FOO
>>> print e.bar()
BAR

Thats it! Now, u r up and running with Swig/C++.

Why nload? Because its a simple application with minimum UI capability, but a wholistic application. Now what did i intend to do? Learn how gprof works ;) . Downloaded the nload source and extracted the souces into the nload directory.

And then i did,

$ ./configure

from the nload directory. And got three Makefile’s in the following directories.

nload/

nload/src

nload/docs

And when edited the all the three Makefile’s by adding the ‘-pg’ option to following flags…

CFLAGS

CXXFLAGS

And then i did…

$ make

Which created the nload executable in the nload/src directory. When ran

$ ./nload

and exited, from the nload/src directory, it did generate the gmon.out file in there. And all i needed to do now was(from nload/src)…

$ gprof nload gmon.out>g.out

I ended up getting “No symbols found”. Now what did i do wrong. I scratched my head and looked at various documentations and finally posted a question on google groups. And a generous mind answered,’ Try “objdump -W” or “nm” on your executable.’ And I did exactly with no symbols being returned. And queried the wise mind again when he said, ‘The build system is stripping your symbols’ and found exactly that ‘-s’ option was set in the Makefile’s for the LDFLAG.

And having readup quite a bit by now saw tha setting the -O options on the compiler would result in undesired output of gprof so i removed the -O2 option set for the. CFLAGS and CXXFLAGS. And then i did

$ make clean

$ make

$ cd src

$ ./nload

$ gprof nload gmon.out> g.out

$ vi g.out

And found all that i needed in there. I hope this would help some one if it did pls comment. If it did not pls comment ;)

I have been waiting write something about this for a while now. And here i am. I have just kinda of made myself familiar with this. There is a whole lot of documentation on Win32::GUITest package distributed with Perl, or i suppose it is. If not you can find by using Google(should i say that).

Why should you read Win32::GUITest?
> enthought.guitest modulde is a rewrite of the Win32::GUITest.
> There isn’t any good documentation/tutorial/reference on enthought.guitest anyway.

To twist the blog around a bit i will talk about the downsides of enthought.GUITest…

  • with enthought.guitest you need different script for different platforms.
  • You need to use too many sleep statements, as the WaitForReady is not available in the python version, to wait for a window operation to complete before you carry out the next operation. This is purely on your gut feel.
  • If you have not developed the GUI application yourself, it difficult to identify the Control ID of the sub-windows to the application, you could use Spy++ or any other similar tool, to get it. But still it did not work for me.
  • Though it is used for a pure vanilla testing of your GUI but if think you could spend a lot time, you could sit down and explore immaculately what you could do.

Now this is where you could find the enthought.guitest for your application.

  1. Linux
  2. Windows

In the same tree somewhere you could find the Mac as well. If you figure out how you tested the gui apps on Mac please leave a comment.

What do you do once you have downloaded the egg?

Extract the files. The directory will be called something like enthoughtXXXXXXX… I renamed it to my convinience to enthought.quitest.linux. Now i placed all my scripts inside for ease of running. One the simplest script is given below and its fail intuitive. This link give you a fair idea about how to do more than this on linux.

from enthought import guitest
from time import sleep

guitest.StartApp("gedit")
win = guitest.WaitWindowViewable("gedit")
guitest.SendKeys("Hello sam")

guitest.SendKeys("%(f)q")
sleep(1)
guitest.Sendkeys("{TAB}~")

What does the script do?

  1. Starts an application.
  2. Waits for the application to render itself.
  3. Sends the keyboard input “Hello sam” to the application
  4. Send Alt-F + Q to the application to quit.
  5. Sends the TAB and ENTER keys to the confirmation window.

Now as i said the same script cannot be run on windows, by just changing “gedit” to “notepad”.  It would look something like this…

from enthought.guitest import *
import os
from time import sleep

p = Popen("notepad.exe")

note_win = WaitWindow("Notepad",wait=10)
sleep(1)
SetForegroundWindow(note_win[0])
sleep(1)
SendKeys("Hello")
sleep(1)

SendKeys("%(f)x")
sleep(1)

SendKeys("{TAB}")
SendKeys("~")

These are the some operations as above. But you could notice that there has been some function call differences. If you have not developed the application that you are testing and the devloper is reluctant to give off the Control ID’s. You could do take the screen shot of the application and determine the relative pixel locations of the button or other controls and then make a gut feel decision of the operation times and apply sleeps appropriately and then emulate the mouse clicks.

Google Chrome

Google Chrome

Google is about to announce its new product the Google Chrome. A new web browser that does not crash all the tabs when you kill one application. As each tab is a process. This called harnessing the computing power of “today”. Thats the key word for Google. They always address the “NOW”.

And i was chatting with a likeminded friend about the how they have branded Google. To the extent that even if they move their hair its news. And smallest application from them is also is trusted to the hilt.

I through the comic and this is what Google does…
* Address the need of the hour
* Do not bother about legacies

What do you think?

Intel now in its bid to build a mobile space around the its new breed of Atom processors has made a notable acquisition of OpenedHand, as its website reads… “The OpenedHand team will join the Intel Open Source Technology Center and will focus on the development of the Moblin Software Platform, the optimized software stack for Intel Atom processors”.

Moblin has not managed to catch many developers’ eye like the Android platform, though Moblin is mainly targeted at the Mobile Internet Device market unlike Android whose main focus the mobile phones. This acquisition might turn things around as as Poky could be defining platform..

“Poky is an open source platform build tool. It is a complete software development environment for the creation of Linux devices. It aids the design, development, building, debugging, simulation and testing of complete modern software stacks using Linux, the X Window System and GNOME Mobile based application frameworks.

Hardware Support

With a wide range of supported ARM and x86 boards and devices, Poky provides a comprehensive selection of power, functionality, and form factor options for devices.

Key supported ARM platforms include:

  • Compulab PXA270
  • Freescale MX31
  • ST Nomadik
  • Marvell Zylonite

In addition virtually any x86 platform can be supported and Poky can be easily ported to other boards as they become available.

We are continually widening support.

Building on OpenEmbedded technology, Poky consists of large number of customisable software package ‘recipes’ which are processed to build bootable tuned filesystems for a range of ARM based boards and x86 hardware. Through highly flexible build configuration and integrated device virtualisation with QEMU, Poky is a powerful tool for all areas of device development. Poky is what you wish your existing BSP would be.

Poky also optionally includes ‘Sato’, our reference GNOME Mobile smartphone and PDA user interface. Sato is built on GTK+ 2.10 and includes the full Matchbox suite, a port of our Pimlico PIM suite, a GStreamer based media player, and a web browser.

Poky has been used by manufacturers such as Vernier as a reliable base system for their device, allowing them to build complete filesystems rapidly and consistently. Building their device on Poky gave Vernier a fast time to market and allowed them to focus on their customer specific applications. Find out more in the Vernier case study.

OpenedHand provides a range of professional support packages for Poky. Whether you need the confidence of an annual support and maintenance subscription, require development expertise to create a new application, device platform or board support package, we can tailor a package to suit your needs.”

Just last night i sat up past midnight to install the Zenwalk[Gnome] 5.2. I have a partition is the use to evaluate new linux distros, distro hopping is hobby ;)

I install Zenwalk in the partitions sda7(/boot) sda8(/) sda9(/home) and sda10(swap). All went really smooth. And i did not choose to install lilo on sda.

Sadly Zenwalk has not switched grub.(On need to look up why!). Now i have Ubuntu HardyHeron also installed which i hardly switch, is almost a constant on my HP500 laptop.  This article is more of a personal reference, but i could also be helpful for the needy :)

This was was my ordeal to get Zenwalk up and running from grub’s menu.lst on my Ubuntu. I went ahead and added the following lines to the end of menu.lst file to represent my Zenwalk partition…

# Zenwalk Linux
title Zenwalk Linux
root (hd0,6)
kernel /vmlinuz root=/dev/sda8 vga=0x31A video=vesafb:mtrr,ywrap splash=silent
initrd /initrd.splash

 
Note for those who have little idea about entries… 

(hd0,6) means look for the files on the first(index 0) physical hard drive in the 7th partition(indexing starting from 0). What files? The kernel image file and the init file.

In the kernel line make sure the the “root=” points to the where your “/” resides in, in my case the /dev/sda8 partition.

NOTE: make sure you make backup of the existing menu.lst file before you make these changes

Mark Shuttleworth is dreaming as well! I started my blog with the “I have a dream” and someone who had already realised that dream is dreaming something BIGGER!

Dream?!? But i hope it would be real and true one day, some day. Some months, possibly 14 months, back i registered this site called www.openink.org. But I never got beyond the point of creating the first(or home-) page if you would like to call that one :(

So it does still remain a dream, to make Linux like an outreach tool for many NGO’s which are struggling to find a way to showase their work. It just does not end there.

The idea is to start of with offering the NGO’s a Linux network, depending on the NGO’s budget constraint on hardware procurement. And them to train staff on using the Linux tools like evolution, openoffice etc. And according to their needs design a decent website that would help to project themselves on the cyberworld. All the software needs would be done for *free*.

Now, what happens when the setup is done? Who would administor the network? And who would design or update websites for them? The plausible solution to this would be have a final year engineering graduates be placed with these NGO’s giving them such support. In turn these getting valuable experience as well.

What do i gain? Seriously, nothing! My drive for money is kinda almost nil :)

Mind you! This is just a Dream but it a possible one!