Friday, May 22, 2009

Tickling, (Did you hear Tickling) Ouch, Its Pickling :-)

Before using pickling, I had a tough time to have the entire data in a single file. Infact i had more than 1Mib in a file. Gedit used to get stuck and infact it lost syntax highlighting too. Python pickling helped me rule out the problem.

Note: For those who are wondering what is pickling, go through this
PICKLE

I had a file full of dictionary elements in it. It was more than 1Mib and I faced lots of problems. It used to take hours together to do a simple program. Then, python pickling helped me to dump the data in to a file in pickle format, more precisely, in a binary format. And, whenever we need, it can be loaded back to the file. If its in the form of dictionary, it will be loaded in the same manner and we can perform get or update functions on the same datastructure. Here is an example of how to convert the dictionary data in to corresponding pickle format.

Consider the following code:

#!/usr/bin/python
import string
import pickle
data={'1':'chandan','2':'nandan'}
file = open("test.py","w")
pickle.dump(data,file)
file.close()
file = open("test.py", "r")
data = pickle.load(file)
file.close()
print data['1']

After dumping the dictionary set into a file test.py, it will be in pickle format in that file. It looks like this
(dp0
S'1'
p1
S'chandan'
)

/* The above pickle format will be in binary. Here i have used a small dictionary set. If we have a large dictionary set, then it is feasible. You can get ur hands dirt using this on your own*/

    * Coming back to the 1Mib file i was mentioning, I converted the entire dictionary set which I had in 1Mib file into pickle format by using the above stated method, dumping it into a file. Then I deleted the entire dictionary set in the 1Mib file. So, that file size reduced to less than 200Kib.
    * Then, I loaded the pickled file to the 200Kib file, which still remains 200Kib itself. The loaded pickled format will be in dictionary format.
    * Then, I used the get, update operations and thus the overloading of gedit is ruled out
    * I have attached few files which might give you an idea of what of I discussed above

How to install NS2 in intrepid ibex or fedora 8

Download the ns2-allinone package from here This is version 2.31. You can download 2.30-33 versions. The steps are same for everything. It will be in compressed form. Extract them to a specific directory. Say, /home/chandan

How to install NS2 in intrepid ibex or fedora 8

Download the ns2-allinone package from here This is ns-allinone2.31 version. You can download 2.30-33 versions. The steps are same for everything. It will be in compressed form. Extract them to a specific directory. Say, /home/chandan

Follow the below mentioned steps.
sudo apt-get install autoconf automake libxmu-dev xorg
cd ns-2.30
./install

It will configure all the files needed to execute the tcl scripts. It will take ten minutes. Dont interrupt the terminal in between. It will be linked with otcl and c++ libraries. By default ns2 supports, aodv, dsdv, dsr and tora protocols. zrp protocol is still under research.

Once its installed, you will get series of instructions at the terminal itself. Follow those instructions carefully.

If you cant follow that, do this.

cd /etc
sudo gedit ~/.bashrc (Ubuntu 8.10)
or
gedit ~/.bashrc (Fedora 8)

Copy the following things and paste there. Replace /home/chandan with your installed path. For example, if its /usr/local, replace /home/chandan with /usr/local.

# LD_LIBRARY_PATH

OTCL_LIB=/home/chandan/ns-allinone-2.30/otcl-1.12

NS2_LIB=/home/chandan/ns-allinone-2.30/lib

X11_LIB=/usr/X11R6/lib

USR_LOCAL_LIB=/home/chandan/lib

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OTCL_LIB:$NS2_LIB:$X11_LIB:$USR_LOCAL_LIB

# TCL_LIBRARY

TCL_LIB=/home/chandan/ns-allinone-2.30/tcl8.4.14/library

USR_LIB=/usr/lib

export TCL_LIBRARY=$TCL_LIB:$USR_LIB

# PATH

XGRAPH=/home/chandan/ns-allinone-2.30/bin:/home/chandan/ns-allinone-2.30/tcl8.4.13/unix:/home/chandan/ns-allinone-2.30/tk8.4.13/unix

NS=/home/chandan/ns-allinone-2.30/ns-2.30/

NAM=/home/chandan/ns-allinone-2.30/nam-1.12/

PATH=$PATH:$XGRAPH:$NS:$NAM

Save and close the file.
Now, type this at the terminal

source ~/.bashrc

Then, type this command at the terminal.

$ns
%
You will get a "%" prompt. If not follow the above steps carefully and you can have simulation working on your intrepid ibex or Fedora 8.

NOTE: In case if u get error message in ubuntu, make sure you have not installed "host" package. If its installed, remove it from synaptic package manager.

Sunday, April 26, 2009

FAREWELL TO Intrepid Ibex!!

Its been quite a long time I have been using Intrepid. Installed it the night it was released. Unfortunately, I could not get JJ on the releasing day, which was released 2 days ago, due to power cut in our area. Finally, I could install it today. This became the last day of II on my comp. II helped me learn Python, GAE, shell scripts (though we had it in our 5th sem) and how to use remote desktop access using SSH(madhu helped me), tcl, Googles Native client. Literally I could apply thoughts which I lacked during my earlier semesters. I can say this is the luckiest OS of all times for me, I did get placed - dont mind crediting II for it :-). For all the efforts I put, I can say, its solely because of II. I have learnt lots and I am technically much better than what I was a semester ago.

In the beginning boots of II, I was not comfortable with the terminal. I started getting used to terminal only after attending the BMSLUG - hackathon session. Especially I got inspired by some of our juniors, who are and were much better than me. I asked many of them quite a lot of things while the session was going on. The session gave an insight of how to build GNUsim8085, and how to fix a bug, and how to report a bug. The session was given by Madhu, Vattam, Krishna, Puneeth and others. Nearly 30 to 40 people had come for that session. And it was very very useful. Came home and got my hands dirt with the things they taught in the session. Who can forget the DOCTOR - PATIENT CONVERSATION while the session was on. :P ( Ishesh, Vattam and Me). It was a fantastic journey of these 6 months. Today, I'm giving A happy farewell to II.





This is how JJ looks on my comp.I hope JJ will help me learn even more things and I am already on it :) It has lots of additional features. Adding to What Madhu has posted in his blog JJ has built in Compiz fusion in it. The editors show the tabwidth and the text format by default. Since it has Openoffice 3.0, It solves a great problem of building graphs for our project at IISc. Everything is so neat and clear. Unfortunately I am not able to install ns2 on JJ. Hope it will be fine. Will list its pros and cons once I get through them. As of now, JJ looks so cool and lightning fast.

Wednesday, April 22, 2009

Playing with Dictionary

This post is to develop a ranklist. Using python it is very simple. Its a mere ten to fifteen lines of code.


At the terminal. Give this command.
$python test4.py > test4

The code is given below.

#! /usr/bin/python
f = file('test2')
ranklist = []
while(True):
c = f.readline()
if(c):
d = c.split('')

marks = d[-1].split('')[-1].split()[0]
try:
marks = int(d[-1].split('')[-1].split()[0])
name = d[0].split('')[1].split('<')[0]
percentage=round((float(marks)/9),2)
usn=name.split()[-1][1:-1]
cusn=usn[-3:]
f.readline()
ranklist.append([marks,name,percentage,cusn])
except:
pass
else :
break

ranklist.sort(reverse=True)
print "RankName(USN)MarksPercentage"

for i in range(len(ranklist)):

print ""+str(i+1)+""+str(ranklist[i][0])+""+str(ranklist[i][2])+""

print ""

NOTE: The formatting may not be proper. Indentation is very important in python. I have attached those files. You can download them .
P.S: Thanks to subbu who assisted me.

Wednesday, April 1, 2009

Converting to and from dictionary!!

In this post I will be showing you how to convert a set of data in to dictionary format in python. This was very handy for me while developiing few applications.

While creating ISE RANKLIST I was giving the 5 line code which I mentioned in my vtu post in a for loop. After fetching the result into a local file, to display the same for a particular usn was kind of tough for me. So, I thought of dictionary datastructure in pyhon.

Here is the code to fetch the results in to a file from the terminal.
$ python test.py > test
And here is the code in test.py file

import urllib
for i in range(1,9):
res = urllib.urlencode({'rid' : '1bm06is40'+str(i),'submit':'submit'})
req = urllib.urlopen('http://results.vtu.ac.in/default.php',res)
data = req.readlines()
print data[245]

/* The above code fetches 9 students result. And it will be put into a file test. */

The test file looks like this .

CHANNABASAVA (1bm06is401) Semester:6      Result:  FAIL SubjectExternal InternalTotalResultSoftware Engineering (CS62)452166PManagement Information Systems (IS63)661985PSystem Programming Lab (ISL68)02323ATotal Marks: 174  

HARSHAVARDHANA G (1bm06is403) Semester:7      Result:  FIRST CLASS SubjectExternal InternalTotalResultEngineering and Technology Management (CS71)532376PObject Oriented Analysis and Design (CS72)411960PDistributed Operating System (CS753)472370PJava and CGI Programming (CS73)672491PComputer Networks - II (CS74)542478PClient Server Computing (CS764)552176PNetworks Laboratory (CSL77)452570PCGI Programming Laboratory (CSL78)461965PTotal Marks: 586  

Now to convert it into a dictionary format. Type this at terminal
$python dict.py > test2
This is the code of dict.py

f = file('test')
z={}
while(True):
c=f.readline()
if(c):
d = c.split('')
c = c + f.readline()
try:

name = d[0].split('')[1].split('<')[0]

usn=name.split()[-1][1:-1]

z.update({usn : c })
except:
pass
else :
break

print z

test3 file looks like this below.

{'1rv05is048': 'SHARMA R S (1rv05is048) Semester:7      Result:  FIRST CLASS WITH DISTINCTION SubjectExternal InternalTotalResultEngineering and Technology Management (CS71)731891PPrinciples of User Interface Design (IS761)662187PObject Oriented Analysis and Design (CS72)692493PJava and CGI Programming (CS73)701585PDistributed Operating System (CS753)572279PComputer Networks - II (CS74)542276PNetworks Laboratory (CSL77)442569PCGI Programming Laboratory (CSL78)482472PTotal Marks: 652     \r\n\n'}

/* here the key is USN. Value is Result */

VTU-RESULTS and ISE-RANKLIST

This is an application to check VTU results and also ise ranklist. I did this using GAE, python, CSS, HTML. I wanted to do this during my 3rd sem. I could do it within a month after learning python and GAE. These are those applications.
VTU RESULTS and
ISE RANKLIST .

It was my longtime dream to develop an application to fetch and display vtu results. Finally I could do it using Python and GAE. Thanks to google for providing such a platform, and giving free hosting :-)


For Linux users :


$python
>>>import urllib
>>>res=urlllib.urlencode({'rid':'type your usn','submit':'submit'})
>>>req=urllib.urlopen({'http://results.vtu.ac.in/default.php',res)
>>>data=req.readlines()
>>>data[245]




For windows users :



Download IDLE IDE for python from www.python.org. Its free and its of 10 Mib. Then after installing open it. You will get a prompt like this
>>>
Then type the following.
>>>import urllib
>>>res=urlllib.urlencode({'rid':'type your usn','submit':'submit'})
>>>req=urllib.urlopen({'http://results.vtu.ac.in/default.php',res)
>>>data=req.readlines()
>>>data[245]

/* Here is the complete description of the above five lines. First we have to import the library for extracting internet resources. Python provides a library for the same. Since the usn what we type will be stored in the text box of VTU site, it will be in the form of key value pair and it needs to be encoded. So, we are using the second line. Then, the result corresponding to the entered usn will be returned in the form of a file object by urlopen function. Then, that needs to be read in the form line by line (using readlines() ) or in the form of a string (using read() ) . Finally if readlines() function is used, we can display the fetched content using data[245]. It will be in the form of a list. So, it will display the result of the corresponding student */