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.
XKCD:Python

XKCD:Python import antigravity …


