Monthly Archives: September 2008

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?

Dhruv walking in YMCA

Dhruv walking in YMCA

From today Dhruv(my son) has started to go to the Day Care/ Play School, again! The first time we put him we was 18 months old, barely speaking he was okay for a month. And then he developed an Urinary Track Infection and had frequent fevers, so we had to stop him from going to Day Care. And for the next six months he spent time at home while the grandparents took turns to take care of him. And just as we thought we have him ready to go to Play School again he got a viral fever which as followed by a bout of diarrhea, for which he was admitted to hospital. Now with crossed fingers i should say he is doing well, and started his day one at the Play School. He went in crying real bad- hmm… i do not know but i feel bad that we have to do this to him. I hope we would do just fine this time around I hope if somebody is reading this post would also tell me more about their kids’ first day experience at school.