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)

Labels