Wednesday, July 22, 2009

Twitter (python api) on Nokia S60

I wanted a system whereby people can sms to a phone that will log it to twitter.
With this system an organization can keep track of what its support/sales staff are at any given point of time.
After tinkering around with the twitter python api and Nokia s60 python (1.9.6), i finally
got it working...(duh...that took me more than an hour...)

There is however ONE big catch (read end of the section)

Here is the code :


Gotchas :-

1 - Ah yes you also need to change that getusername routine in twitter, else it wont work

2- Pay attention to that pesky pyS60 Application Packager (READ the README file) , this is not py2exe. Basically, you will need to rename your main file to some fix default.py.

3- It will keep asking you for connection (access point) on each msg , unless your using some newer phones like nokia 5800 XM. If you are using N82, ALL the popular examples shown in the internet like these :

http://discussion.forum.nokia.com/forum/showthread.php?t=163939

or

http://snippets.dzone.com/tag/pys60

or

http://croozeus.com/blogs/?p=836

DOESN'T Work. The reason is due to some "compatibility problem" between socket and btsocket.

"Twitter" uses urllib2 not urllib, and these people probably have never tested it on that api. Someone from Silicon Valley once said that Nokia know nuts about promoting development, and thus he rejected their offer to develop for symbian and instead move the whole team to iphone.

In some ways, i agree....but i still love my nokias ;-) I reckon the same thing would have worked in Iphone much earlier and with less blood on my desk.

** Update :

Marcelo Barros from Croozeus :

Yes, that is the problem. I reported it at maemo some time ago.
I suggest you to use my twitter api. Extend it if necessary.
http://code.google.com/p/wordmobi/source/browse/trunk/wordmobi/src/s60twitter.py
It uses urllib and simplejson (I ported it to S60, code in the same dir).

Note : If you still want to use the twitterapi as it is and not the ported version, you will

need to hardcode the "getusername" and stick to "socket" only calls, avoid using btsocket.

Sunday, July 12, 2009

C# calling Python scripts and processing output

Occasionally there would be a situation where the C# needs to call a python script and work with its output. While this might sounds like a piece of cake, it does have its gotcha, that is when
you want to process each line one by one and you happened to have somekind of timing (time.sleep) or some other python codes that works correctly under console but unable to be processed by C#.

Below is how you do it : (read until the end, there is a gotcha)

private static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (outLine.Data != null)
Console.Out.WriteLine(outLine.Data.ToString());
}


static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = "python.exe";
p.StartInfo.Arguments = "c:\\test\\test.py";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
p.Close();
}



Assuming ur python script is the following (test.py) :

import time
import sys

for x in range(100) :
print x
time.sleep(1)


You will be surprised that you dont get anything. For some reason, time.sleep(1) and possibly
some other python library calls would result in the output not being flushed. The solution
would be to "flush" it :

for x in range(100) :
print x
sys.stdout.flush()



Vmware and the Virtualization Gotcha's

There is this lab that has 4 different Win2k servers, each running a different application.
Server A - Mailserver
Server B - Intranet application with MS SQL db
Server C - Vpn server and backup
Server D - Test server

Along the way, the admin decides to put all these dated servers into a VM and slot it in to a high end Server from Dell. The whole porting process took around 2 weeks and when it was over,
everyone was happy with the new setup. No more additional switching cables and multiple monitors lyring around and the perceived energy savings cost was a bonus.

However one day, the mailserver began to feel very slow, Procexp (sysinternals) itself was myteriously taking up 45% cpu and more and even with the mailserver service stopped the pc still feels awkwardly slow. Rebooting didnt that mail server VM doesn't help either.

When i came in to help out in this scenario, the first thing i went thru was the list of VMs running in the server. One particular VM is taking up 26% of the CPU of the main server, however that should not be the reason why it would affect the mail server VM. Upon closer inspection however, i notice some native apps running on the test machine VM (26%) that is using up the VM's tcp/ip port very quickly.
It then became logically clear that this was the problem, pausing that VM immediately restored the other server's performance and that was like a 100% improvement.

I suspect the problem is because the main server is still just an OS with the normal limitation of the 65535 ports on a single ip and single network card. Since all the VMs runs on this machine , that test application was just blasting away the network resource, this turns the allocation of the real main server NDIS packets resources into an ugly situation where each of the VMs are queueing up to get its allocation.

Another case solve. Another Hamster Huey and the GooiKablooi award for virtualization.