Thursday, May 14, 2009

Awesome Python Twitter Library: See Which Twitter users don’t Follow You Back In Less than 10 lines of Python


Mike Verdon’s Python Twitter Tools is an awesome twitter library. It is less than 100 lines of code, and is very well commented. It uses the dynamic language features and creates the methods on the fly and returns the python dictionary.

The best part: It is dynamic and creates methods on the fly. So there is no need for any documentation beyond the standard twitter API documentation pbwiki. Use the url patterns after `/` as the object parameters by separating them by a `.`.

There is a global constants file that has a list of all methods that must be a POST and it makes post requests for those and get requests for the rest. Infact the author has even written a documentation scrapper that parses the twitter documentation to find out which methods are POST.*

Using it is very simple and pretty straightforward.

#Instantiation: from twitter import Twitter
t = Twitter()
#Public Timeline?
t.statuses.public_timeline()


Jerod Santo showed how using a ruby twitter gem library, he was able to find the people not following you back in twitter in less than 15 lines of code. A perl guy demonstrated to do the same in the Zen Perl library.

So how many lines of code would it take using the library that I think is the best across all languages. Lets try out.
Finding Friends ids? From the twitter API wiki:

URL = http://twitter.com/friends/ids.format

Thus, our method for finding the friends of a given user will be

t.friends.ids(screen_name='becomingguru')

and thus the entire code to print all those the user follows but who is inturn not followed back is as follows:

import wrapper
ts = wrapper.Twitter()
user = 'scorpion032'
diff= set(ts.friends.ids(screen_name=user)) - set(ts.followers.ids(screen_name=user))
for i in diff:
 try: non_fol1 = ts.users.show(id=i)
 except: continue
 print "%s with %d followers and %d following" %(non_fol1['name'],non_fol1['followers_count'],non_fol1['friends_count'])


*However this auto updating of POST functions is yet to be updated as the documentation format got changed, recently after @dougw joined.

Thursday, April 2, 2009

Python Hidden features

Image representing stackoverflow as depicted i...Image via CrunchBase

What are the Hidden features of Python, a stackoverflow question asks. Thats a very nice thread in Stackoverflow, as of this writing has 94 answers, many very nice ones. With the kind of community on SO, how could it be any way else. Here are the 2 submissions I made to the the question.
#Simulating the tertiary operator using and and or.
#and and or operators in python return the objects themselves rather than Booleans. #Thus:

In [18]: a = True

In [19]: a and 3 or 4
Out[19]: 3

In [20]: a = False

In [21]: a and 3 or 4
Out[21]: 4

#However, Py 2.5 seems to have added an explicit tertiary operator
In [22]: a = 5 if True else '6' In [23]: a Out[23]: 5
And here is the other one:
#Creating dictionary of two sequences that have related data

In [15]: t1 = (1, 2, 3)

In [16]: t2 = (4, 5, 6)

In [17]: dict (zip(t1,t2))
Out[17]: {1: 4, 2: 5, 3: 6}
Reblog this post [with Zemanta]

Monday, February 23, 2009

Fibonacii series

I absolutely love this program I wrote to solve the Project Euler 2 nd problem
fib=[]
def fibo(a=-1,b=1,upto=4000000):
    if a+b>=upto:
        return
    else:
        a,b=b,a+b
        fib.append(b)
        fibo(a,b)

fibo()
even=[i for i in fib if not i%2]
print sum(even)

The Pythonic way!

With my earlier experience of Project Euler (I used to solve in Java), after being quite well conversant in Python, I just had a re-look. I was simply pleased by the compactness, doing it in the pythonic way achieved. The question is to find the sum of all numbers between 1 and 1000 that are divisible by either 3 or 5. Here is how simply, we can do it in python:
a=[i for i in xrange(1000) if not (i%3 and i%5)]
print sum(a)

Monday, September 22, 2008

David Heinemeier Hansson at Startup School 08

<div><a href='http://www.omnisio.com'>Share and annotate your videos</a> with Omnisio!</div>

Wednesday, August 6, 2008

TopCoder SRM 413 Round1 Div 2 Problem 250

Problem Statement
Subway trains can move people quickly from one station to the next. It is known that the distance between two consecutive stations is length meters. For safety, the train can't move faster than maxVelocity meters/sec. For comfort, the absolute acceleration can't be larger than maxAcceleration meters/sec^2. The train starts with velocity 0 meters/sec, and it must stop at the next station (i.e., arrive there with a velocity of 0 meters/sec). Return the minimal possible time to get to the next station.
Definition
Class:
Subway2
Method:
minTime
Parameters:
int, int, int
Returns:
double
Method signature:
double minTime(int length, int maxAcceleration, int maxVelocity)
(be sure your method is public)

Notes
-
Your return value must be accurate to within an absolute or relative tolerance of 1E-9.
-
If the train's speed at time 0 is v0 and the acceleration is always a, then at time t the speed will be (v0 + t * a) and the train will be (v0 * t + 0.5 * a * t^2) away.
Constraints
-
length, maxAcceleration and maxVelocity will each be between 1 and 1000, inclusive.
Examples
0)

1
2
10
Returns: 1.4142135623730951
maxVelocity is very large. So the train can keep speeding up until it reaches position 0.5.
1)

1
1
1
Returns: 2.0

2)

10
1
1
Returns: 11.0
The train reaches its maximum velocity after 1 second, while traveling 0.5 meters. It then travels the next 9 meters in 9 seconds, and takes 1 second to decelerate to 0 m/s while covering the final 0.5 meters.
3)

1
10
1
Returns: 1.1

4)

778
887
384
Returns: 2.458961621570838

5)

336
794
916
Returns: 1.301036207838119

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

public class Subway2{
    public double minTime(int l,int a,int vMax){
        double t,t1,t2,v,s;
        v=Math.sqrt(2*a*l);
        v=Math.min(v,vMax);
        t1=v/a;
        s=a*t1*t1/2;
        t2=(l-2*s)/v;
        if(l>=2*s) return 2*t1+t2;
        return 2*Math.sqrt((double)l/a);
    }
}

Thursday, July 31, 2008

The Big Idea