Archive for the ‘python’ Category
Python, E51 and Sid
I installed python almost immediately after I picked my phone, about six months back. But I forgot all about it! Yesterday, I decided to fix it up for good and start getting my phone do some of my work?
As a first step, I’ve had tremendous success getting python over a bluetooth serial console to work. Of course, sincere thanks to google and other hackers who have tried and tested this method before me. First thing I ever did is to repeat what I did to get my older 6111 to talk to sarge. It is available here. That worked like a piece of cake. I installed a newer python and the python shell using this method.
I tried connecting to python over bluetooth serial console. But I don’t think I ever got it right. So google and I got these links.
http://ramblingfoo.blogspot.com/2007/05/bluetooth-fun-with-my-nokia-6600.html
https://bugs.launchpad.net/ubuntu/+source/bluez-utils/+bug/227429
Both had the most vital information to get started. Here is what I did.
$ sdptool add --channel=4 SP
$ rfcomm listen 1 4
I used 1 because I tried setting my debian to use 0 at startup. Another “fail” attempt that one.
In another terminal,
$ screen /dev/rfcomm1
It had some wierd characters to start, just press enter and a new interactive python prompt appears. I checked to ensure that it is the one on the phone and not on my machine!
Now to try something useful
…
From python to C(pp)
Image via Wikipedia
Yesterday was big disappointment. My python code was taking just too long to process a graph of about 30000 nodes and 80000 edges. Actually, it was taking more than an hour to just construct it. After that I have a bunch of algorithms to run on it. And this graph is generated from a sample graph of about 1024 nodes and edges. But in reality I need to run the whole thing against an initial graph of 20000 nodes. The whole thing just doesn’t sound to work. There are two possibilities: First is my code could be dirty; Or python does not do my kind of work very well. I can’t say anything about python until I have cleared my code to be the best. But I don’t have the time to invest here, since all this is just paths to my actual work, and my work is elsewhere!
So, the other choice is to code the whole thing in C/C++. Actually it should be C, because I am not a particular fan on C++. But given the time constraints, I just may have to go with C++. The reason is simple. I want something like STL to hasten my job and not worry about performance. I simply don’t see any such library for C. There are some ways to use STL with C, I guess. So, C++ it is. I should probably spend some time and write STL like library for C or at least spend some time to see if somebody is already doing the job. Anybody knows some good library for C, please leave a comment. Yeah, and don’t bother asking why I am not a fan of C++. I wouldn’t bother to reply.
Count occurrences of a word in a file?
Tricky question. My friend came up with a request to count the number of occurrences of a word in a text file, without too much of a script. A single line solution? One that pops at the top of the head is grep -c. But that will fail if the word repeated within the line! With so many text processing commands in linux, I was left wondering what works easiest.
I haven’t used awk/sed/perl in a while. I could write him a python script in just about the typing time. But how should I do it without any of these? With the constraint that the words are separated by spaces, I ended up with this,
$ tr [:blank:] ['\n'] < file.txt | grep -c "pattern"
Seems to work. Not sure if there is an obvious mistake laughing at me. Maybe python can be harmful to your old linux habits!!
My Second Django App
Yesterday, I completed my second django application. I still don’t want to say I know django, but this time around I learnt quite a bit. This app is for a friend of mine working in one of those big outsourcing firms. I guess I ll post the code for download, once he okays the application. He has already agreed to that. After all, I want to keep that opensource.
Some very interesting learnings from the code are the newforms library from django 0.96. I had about 15 fields in the model and had to generate select boxes with validations. If I did it with the django I knew so far, I’d never have completed. newforms just worked magic. The other was using the calendar from the admin interface, for DateField, in our code. I took the code from this snippet.
The code has some ugly hacks. You’ll guess it at the first look. I ll fix them over the weekend, if I find some time. Already, last week had been least productive here. The only thing that even worked was this django app. So, I won’t get back to this until some of the other more important ones are completed.
There is one more useless detail that needs to go into this – the license. I haven’t decided on one yet, but since I want it to be on my profile, I ll keep it something like BSD or GPL2.
Blogged with Flock
SWF to FLV
We had a small tutorial session on preliminary ASIC design flow using synopsys and cadence tools. I was handling the tool demo while my guide introduced the concepts. We used VNC for demoing the tools, since I had set the entire thing on my work station.
One of the professors attending the session wanted to record the setup. I suggested vnc2swf to record the session, since we were not so keen on the voices. We only want to record “what menu, what choice and after what”! Anyways, as usual after a few technical glitches we managed to record the entire session in four SWF files. Interestingly, the combined file size was about 40-45MB. I guess lack of audio did the trick.
There is an internal wiki site where some of these recordings get hosted. We decided to host the files on that wiki, so that others can get to use it also. The problem started exactly here. How do you convert SWF to a flash movie like what they have on youtube? There are a bunch of proprietary solutions on google. I am the only guy who uses VNC for these things and I certainly don’t want to afford a converter!
There is a pyvnc2swf project that is hosted along side vnc2swf. Little did I realize that this project did exactly what I wanted. Power of using python? I was woken up by this forum via google. There is a simple edit.py that does the conversion. The command itself is
$ edit.py -o file.flv file.swf
And mplayer played the flash video fine, so I guess the web flash players would do the same too.
Blogged with Flock
PyParsing with Verilog
I came across this python parsing library called pyparsing through my adviser. The first thing I wanted to try implementing on this is a verilog parser. Actually, the site’ pages allows us to request a verilog parser from the author for non-commercial purposes. Anyways, thats not the point. I know and understand some verilog. So it makes more sense for me to try a verilog parser first. I have my parser for structural verilog using this module in my pbwiki page. It cannot handle many cases yet. But I took about an hour to get that code done. I spent about a day or two trying to figure out pyparsing itself. The documentation is fairly good. There are some interesting code snippets and howto in the pyparsing wiki page.
Now I am writing a full fledged verilog parser using this module. I’ve not requested for the author’s script yet. Maybe I’ll do that once I complete to figure out how good I am in using this library. And the full semantic/syntactic specification is available in IEEE explore and in Samir Palnitkar’s book on verilog HDL. I am not sure how much time I can devote to this, so it could be some time before I’ve followed this up. Meanwhile you might want to check the various samples in the pyparsing wiki.
Demo-ing python XML abilities
I have been using python for a while now, but I never have to use the XML processing capabilities of this language! Yesterday I tried writing a quick and dirty way to convert an open office worksheet into a simpler XML. Actually not much reason to do this myself. Rama was doing something with worksheets and I decided to give this a shot. And within 15-20 minutes I have a simple XML output parsed out of the huge XML that open office format already is. That is precisely why I love writing python code.
Here is the script. Don’t bother commenting if the python language constructs are ill formed. I learn very slow if my work gets done before I learnt it all!
My First Experiments With Django
Funny way to call this post! I started learning about django about a month back. My first exposure to this genre of tools was about a year and a half back, when I tried to do some ruby on rails work. The idea didn’t kick off and I kinda didn’t have the patience to understand how RoR worked. At that time, I was not comfortable with ruby. I still am not, but thats a different story.
I started practicing python roughly about two years back. I call it practicing, because if you are some programmer, then python is probably a week of learning. At least it was so with me. But I never managed the web end of python and I did not need it either. Last week, I needed to quickly build a small application to hold all my research work : thought blog, article repo, and an interface to link the two. It was also about the same time that my guide came up with some turbogears project. I got to see the code and found it interesting. In fact the kid templating language in turbogears sort of reminded me of a templating engine called velocity that sakai used to use. I am not following sakai too closely to say if it still is!
Enough of that history. As I started trying turbogears, I remembered reading of django. So, I just stopped turbogears and started django. And my first app is already out in exacly two working days. Whoa, the last time I ever rolled something like that was when I had to meet a deadline and was working on php. This time, there was no deadline, and the actual code that I wrote is far less compared to what I wrote when I did the php work. Ok, that was unfair because the php application was not running a django-like framework. Of course, my main application is still out there untouched. Wondering if I should give turbogears the try! Lemme see which one gets the better.
Thoughts?
Matrices in Python
Well, not anything very tough to do. In order to quicky refresh my python skill set, I decided to rewrite the C code that I had written a couple of days back in python. Works like charm.
I ve always liked python just for this ease of writing code. I spent quite some time with C to do this. I am happy about the C version, because I wrote my own pointers to do some things. But I doubt how well my C code would scale?!?
The first version of the python code is posted at http://abishekgoda.pbwiki.com/My%20Code .
