The Zen of Python

The famous Zen of python was written by Tim Peters. As now I will be writing posts here about Python too, so I thought I should start with the Zen of Python.

Small Easter Egg

If you have Python Interpreter you might want to try out the following.
import this

The Zen of Python

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

Stay tuned for the some real python stuff on your way…

How to use oauth-python-twitter

Using OAuth for authentication is quite different then using Basic Auth in many ways, first major difference is that one don’t know the username until it is requested from twitter after completing the authentication process.

Details

You will need following information to start.

  • Consumer Key
  • Consumer Secret

If you do not have above, you can request them from here: http://twitter.com/oauth_clients

The following are the steps that should be performed to authenticate user and get user information from twitter, the rest is same as using python-twitter.

  • Get the Request Token from twitter
  • Get Authorization URL
  • Get the Access Token from twitter
  • Get user information

Get the Request Token from twitter

twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET)
 
request_token = twitter.getRequestToken()

Get Authorization URL

using same request_token from previous step

authorization_url = twitter.getAuthorizationURL(request_token)

Now send the user to authorization URL, for allowing access to the application.

Get the Access Token from twitter

Once the user return from twitter, we need to request the access_token from twitter for futher aunthenticated api calls on behalf of user.

NOTE: Need to create the new instance of OAuthApi using request_token from first step.

twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, request_token)
 
access_token = twitter.getAccessToken()

Get user information

This api call was not present in python-twitter and the user info call expected that there we know the username before making call, but this is not the case with OAuth, so we have to request User Information so we can use it in our application.

NOTE: Need to create the new instance of OAuthApi using access_token from last step.

twitter = OAuthApi(CONSUMER_KEY, CONSUMER_SECRET, access_token)
 
user = twitter.GetUserInfo()

Now use the rest of python-twitter api calls as you used to use them with basic authentication, for more information see the wiki at:http://code.google.com/p/python-twitter

OAuthApi, an OAuth Extension to python-twitter

If you are a python developer and you tried to develop some application for twitter, you must have came acrossed python-twitter. Which provides you a pythonic interface to twitter, by allowing you to access almost all twitter objects as python objects. python-twitter is still in devleopment, and hence still does not have the OAuth compatibility. I am being a oauth pro, deicided to write a derived class from python-twitter, to allow OAuth to be used as the authentication mechnism for python-twitter.

python-twitter is being developed by our beloved google’s employee: DeWitt Clinton

You can get the python-twitter at: python-twitter

You can get the oauth extension to python-twitter at: oauth-python-twitter

In the next following posts I will posting about how I am using it with Django specifically and how I have writter my TwitterAuthBackend for Django. The whole purpose of publishing the code is to get improvements and reviews on the code, so I have allowed non-members to review the above project on code.google.com, you are more then welcome to post your coments here or review the code on the project. Let me know if you don’t understand anything by asking questions in comments.

P.S I have used the same license as was used for python-twitter.

Happy twittering and developing 🙂

Google App Engine: Do not let your engine stop

As you all might have noticed that I was not posting for a while now, actually had been taken overy by few projects. But today I thought to post about the updates from Google App Engine. Google App Engine have released the Cookbook where you can submit your recipies or can benefit from other people’s snippets of code.

One more thing that Google App Engine team has done which is really useful, they have included the documentation with SDK, so if you are offline you will still have access to SDK. This will not let your engine stops even if you are not connected to internet.

Google App Engine is getting popularity.

If you haven’t been following Google App Engine Blog, according to latest post the Google App Engine is getting popularity and many programmers are learning python just to play with App Engine. According to Marzia Niccolai on Google App Engine Blog:

I’ve returned from the App Engine hack-a-thon in Chicago a Superfan of the App Engine community. We had lots of fun meeting App Engine developers, but why let me tell you about it? A couple of attendees wrote great summaries of the day: A Ruby on Rails developer giving Python a whirl, and a developer who worked on testing with App Engine. We saw people working on iPhone apps, OpenSocial and App Engine, and even a chat application!

So, if you want to host your application on reliable Google platform then you gotta learn Python to make your next app in Google App Engine. Google App Engine is going to be the next big thing if they started supporting rest of the languages like PHP, Ruby and Perl which they have already promised. So stay tuned.

Rapid Development with Python, Django, and Google App Engine

Google I/O the developer event that happened in SanFransisco, many were able to join it but there are thousands of people who were not able to join it Google has not forgotten them. The videos of almost all the talks has been posted but the best one is “Rapid Development wiht Python, Django and Google App Engine“.

Its a must watch video so I decided to share it here. I hope you will benefit from it. You can get the presentation slides from here.

If you like the above video then there are few other good videos too related to Google App Engine, so if you ware interested in learning Google App Engine and not only that but there are videos for other Google technologies which include Gears, Gadgets, OpenSocial and gData. Have look at Google I/O session videos posted with slides

Now YOU can sign up for Google App Engine

Wow! Great news for python developer out there, Google has open the signup for their App Engine. Now every one can signup for the App Engine account. If you haven’t got your account yet and want to be on the bleeding edge of web development, then go and signup for App Engine and have some fun with App Engine SDK then you can upload you app.

Google App Engine has also introduced to new API which includes Image manipulation and our all time favourite memcache API. So, its time to put the fun back into web development and lets develop some useful apps based on App Engine.

If you are Django Developer then you can put your Django Apps on Google App Engine too. Happy developing.

Note: If you are not a developer and want to see something developed on App Engine, then leave a comment and may be some of us will do it for fun.