Posts Tagged ‘Python’
Joys of Python and APIs
At my present job , Most of the time, I have to do changes in the legacy Java code to improve performance.
But this time i was asked to tell the extent of performance improvement due to my changes . I tried to use some Java Profiler , I tried Eclipse TPTP and HPROF but due to complacency of the legacy application. I was not able to profile my changes.
So i decided to keep it simple . Log the timestamps and analyze the result. But the log generated by this was unable to show any subsequent improvement in performance as I had to log time in Milliseconds ( System.nanoTime () could not be used for Java 1.4) .
For better time precesion i googled and found this timer library by Vladimir Roubstov.I tweaked it for my code and got the comprehesive log output.
Now to analyze the log, I wrote a python script that reads two log files and generates the time improvements in aggregation and for each run. This was the most exciting part.
for i in imap(lambda x,y:(x, y, x-y), [float(b) for b in new_list],[float(a) for a in old_list]):
print '%f - %f = %f' % i
I wrote it in no time , Python Rocks . Then I decided to go one step further. What about plotting it on a graph. Straight away Google Chart API came to my mind and there is python wrapper to this.
So I wrote this small function to produce graph for these logs.
def draw_chart():
from pygooglechart import Axis,SimpleLineChart
chart = SimpleLineChart(600,200,
x_range=(0.000, 0.999), y_range=(0.000, 0.999))
chart.set_colours(['ff0000', '0000ff'])
chart.set_legend(['New','Old'])
chart.add_data([float(a) for a in old_list])
chart.add_data([float(b) for b in new_list])
chart.set_axis_range(Axis.LEFT, 0.000, 0.999)
chart.set_axis_range(Axis.BOTTOM, 1, 100)
chart.download('D:\\my_data.png')
This is the graph generated for my data :
The graph does not show much improvement though , but the post was about joys of python and APIs
P.S. : There are Java backports for using 1.5 > features like System.nanoTime().
Delhi Barcamp 4 Take Aways
After being postponed 2 times , The Delhi Barcamp 4 started at Amity audi quite well as scheduled. All credit to Piyush and volunteers the people behind it.
I reached the venue much early and met this guy Nilesh and IITK and IIML product , He has just started a startup and runs a music school and he was to perform in the evening beer party .
Then the event started in a formal way , But by an amazing personality Mr. Talwant Singh , A Judge by profession who has used IT in the district courts to evolutionarily automating the processes of District Courts, A first by some one in Government Sector. No wonder he blogs for cyber security.
He presented a good talk and said build something that works and is simple to use even for an illiterate person.
Then it was talk on entrepreneurship incubation my Amity ppl, Then we moved to the presentation halls. First interesting talk was by chahiye guys (one of the main sponsors) . The presntation was a bit over crowded by the American names but the emphasis was on building something that adds value.
Nirat Bhatnagar said there are two ways to make a difference :
- start a business and make profit only
- start an NGO with no profit no loss.
But there is middle path also , to put ethics in you business , to do something that adds value to society.
Then a nice talk by two young guys on building facebook apps. They had built a cool Gift sharing app iGift in their summer training.
Another session was by Alabot guys , there product is really cool , it uses NLP and sits as middleware to give results for any query from different sources. The talk was more market oriented more then telling how it was built . There was demo for getting air ticket from chennai to banglore. from IM and it provided awesome results.
In the mean time i met my twitter frnd Mayank Dhingra , He is working some really cool stuff at a startup that works in Mobile and Python . Then the guys frm quite famous slideshare , Arun and Gaurav.
Then it was lunch . After lunch Guneet from chaahiye told about developing web apps with salesforce.
and Demo by RouteGuru folks .
The the last talk was by two firangs Nick and Charlie from UK and Germany . They out sourced themselves to India for yet another startup for yet another Rails app Entrip . This talk was pick of the barcamp in terms of demo and the work they had done . Entrip is mashup of Google Maps , flickr facebook and many other api’s to show travel trips by frequent travllers on a map .
The two presentation i really liked were by prashant on weird topic East Internet company , he elaborated how the History of expeditions lead to colonies and Wat will happen with the internet .
Get the East Internet Company presentation here.
The other presentation was by Akshatt on Folksonomy the term given to social collaboration over Internet . The power of contribution/sharing can do wonders. Get the Folksonomy presentation here .
There was quiz to go and a beer party later , but i had to leave as i had been offred lift to gurgaon by another startupper from Masplantaz. He is working on some product on Rails . Wish i hear another startup success story.:)
And Yes i got the free T-Shirt which says ” I want to change the world but they don’t provide the source code “
This was my first barcamp. I wanted to hear about python/django stuff . But it was Rails and Entrepreneurship all the way .
My Takeways from the Barcamp were:
One should build some thing that is simple and that adds value.
Check out the Barcamp Pics at Flickr.
Lisp,Python and Java
While acting on my new year resolutions, I started reading the SICP book .
The book focuses on the aspects of computer programming fundamentals and making the reader learn this by problem solving through LISP programming language.
So in the first chapter i had to solve some problems , and one of them was :
Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
So my implementation of this problem in LISP is here :
(defun square(x) (* x x) )
(defun sum-of-square(x y) (+ (square x) (square y)))
(defun grt(a b) (if(> a b) a b))
(defun grt-2-sum-of-square (a b c )
(sum-of-square (grt a b) (grt b c) )
)
(grt-2-sum-of-square 1 2 3)
I implemented the same problem with python as following :
#Returns Square
def square(x):
return x*x
#Returns Sum of the Squares
def sum_of_square(x,y):
return square(x)+square(y)
#Returns greater number
def grt(a,b):
if (a>b):
return a
else:
return b
#Returns the sum fo square of greater numbers
def grt_2_sum_of_square(x,y,z):
a=grt(x,y)
b=grt(y,z)
return sum_of_square(a,b)
# If main mein module is called
if __name__=="__main__":
#print result
print grt_2_sum_of_square(1,2,3)
and implementation of same problem in Java goes like this :
public class Test {
// Returns the Square
public int square(int x){
return x*x;
}
// Returns sum of the Squares
public int sum_of_square(int x,int y){
return square(x)+square(y);
}
//Returns greater number
public int grt(int x,int y){
return (x>y)?x:y;
}
//Return the sum fo square of greater numbers
public int grt_2_sum_of_square(int x,int y,int z){
int a=grt(x,y);
int b=grt(y,z);
return sum_of_square(a,b);
}
// Main Method
public static void main(String a[]){
//instantiates class
Test t=new Test();
int res=t.grt_2_sum_of_square(1, 2, 3);
System.out.print(res);
}
}
There are easier ways of implementing it in python and Java though . But i preferred these as i implemented the same LISP construct. I am amused by the simplicity and cleanliness of LISP code here and no. of lines i had to write for doing this.
I wish i should have been taught LISP in school and i would have been a better programmer
Python gives me pleasure of code elegance and simplicity, but Java is the one i am working with . The one that got me a Job.
Learning new things make me feel better, that there is no end to learning.
Powered by ScribeFire.
My New Year Resolutions – 2008
Its dawn of the new year. Every things seems fresh, though its just a psychological feeling.The past is past, everybody make plans for the future, and I have some too.I scribbled some lines in Punjabi on new year eve.Here is English translation :
"have dreams in the eyes,
and passion to live the dreams..
have tides of wishes,
and courage to achieve those.. "
So my new year resolutions will be a progress in living my dreams .
Professional/ Academic :
- Pursue study/research in Natural Language Processing / Linguistics , Full fledged dedication does not seems to be affordable . Will look for alternatives like Open / Free Education thanks to MIT OpenCourseWare. There are many other players which provide open education.
- Start / become member of an open source project in the field of Natural Language Processing and Python as my favorite language.
- Learn a new programming language, LISP is a favorite, Scala or Groovy are also on the Radar.
- Dive more in depth into Java (Excited about closures in Java 7) and Python . In fact become more competent in Python.
-
Will look to work on frameworks/technologies like Struts2, Spring , Guice ,
Hibernate , Axis2 , SOA in Java/J2EE and django , Plone in Python , Will be following happenings in these . - Become a Sun Certified Web Component Developer.
Personal :
- Learn Urdu langauge/script , to be efficient in reading and translating Pakistani Punjabi /Shahmukhi.
- Have personnel web space and host/migrate my blogs there.
- Will make cycling/exercising a habit.
- Will start writing a diary
- Will look for a girl friend…
Books To Read:
- Structure and Interpretation of Computer Programs
- One Hundred Years of Solitude by Gabriel García Márquez.
- The Theory of Everything by Stephen Hawking.
- Ik Nadi Sanvli Jihi(Punjabi) by Nirupuma Dutt
- Many More as they come across .
My Predictions :
- Open Source is going to rule that is for sure . Linux (especially ubuntu) will give the fight to windows in desktop war.
I am excited about KDE4 release. - Google will be drifting from ” Don’t Be Evil ” policy .
- A Multipolar world order as US hegemony gets counter balanced by Russia, Iran and Venezuela trio.
Hmmm. It has become quite a long list . Hope I make all of these . I will appreciate if anybody has similar/anti thoughts on this, please do drop me a comment..
Alvida 2007
2007 has come to an end … Here is my analysis of my surroundings and things i am interested in . Here are the happenings :
Globally
- Open Source has arrived , It was more vibrant this year. Sun Micro systems open sourced its premier programming language Java , its operating system Solaris , even its processor chip ultraspark ( a first).
- Ubuntu captured the buzz in the Linux word and has become the alternative to windows along with opensuse . Its Linux era now as Windows Vista failed to create any impact . Go Linux Go. KDE4 is going to launch in January next year. Which is going to be another feather in the cap as its looks will be rock the Vista look and feel. Go Linux Go
- Google joined hands with various telecom companies and formed open handset alliance which announced its mobile SDK andriod in November this year. Google joined hands with IBM-Sun camp for ODF as document standred to challenge Microsoft’s OpenXML. Although being involved in open source ventures , Google is said to be drifting from its “Don’t be Evil” policy.
- The world is fastly moving to multipolar world as US hegemony is weakened by various elements like weakning of Dollar diplomacy, President Bush’s wrong stance on Iraq war. The countries like Russia,Iran ( in Asia ), Venezuela, Cuba (in Latin America) have risen as poles of new word order. Venezuelan President Hugo Chavez is the greatest challenger of the US hegemony in his region . With his oil diplomacy and pro people policies he has won support of earlier US allies in Latin America.
Personally
The year was a great learning experience in many aspects of life , professionally and personally :
- Became Sun Certified Java Programmer with 90% score in the begining of this year . Learnt hidden aspects of Java Language . Learnt and worked on various new technologies and tools like Apache Struts , Oracle , Hibernate , J-Unit , J – Meter, JBoss, Display Tags and many more.
- Migrated office work environment from windows platform to OpenSuse 10.2 Linux . Migrated IDE use foe Java/J2EE development from Netbeans to Eclipse ..
- Started learning Python and django , and very much in love with it .
- Some happier moments at family front (Dad recovered from braintstroke well and joined job ) , Some harsh moments at emotional front .
- Books : Read numurous books this year . Link to some of them.
But Best i read this year were :
Java –Head First Design Patterns , SCJP Study Guide by kathy Birts and Sierra
Python — Dive in Python , The Django Book
English Literautre — Metamorphosis by Franz Kafka, Art of War by Sun Tzu .
Punjabi Literature — Naag Lok By Lal Singh Dil , Anhoye by Gurdial Singh, Failsoofiaan by Amarjit Chandan
Punjabi Magazines — Hun (Woderful Punjabi Magazine), The Sunday Indian (Punjabi). (mail me for more info on Hun). - Movies : Did not watch many but favourites were :
Hollywood — Apoclypto by Mel Gibson , Oscar Winner The Pianist .
Bollywood– Bheja Fry, Jab We Met , Chak De .
Others — Documentry Pala by Gurwinder Singh. - Celebrated Shaheed Bhagat Singh’s hundred’th Bitrhtday , with a treat to colleagues in the office .
Inquilab Jindabad - Got a chance to meet Punjabi writers Amarjit Chandan, Nirupama Dutt (Editor The Sunday Indian) and Sushil Dosanjh (Editor Hun)
So this was my last year 2007 in brief . I would like to hear about your views on last year and this post . Please post comments .
Alvida 2007
XKCD:Python

XKCD:Python import antigravity …


