Saturday, November 14, 2009

Update twitter status using Python script

Recently I have written a Python script to update status message in twitter using their api. Let me share the code here:

import urllib
import urllib2
import base64
import httplib
import socket

## authenticate module is used for http basic authentication. found in 'urllib2 missing manual'
def authenticate(username, password):
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
top_level_url = 'http://twitter.com/'
password_mgr.add_password(None, top_level_url, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)

return opener

## updates twitter status given the status, user name (id or email) and password
def update_status(status, user, password):
data = {'status' : status}
data = urllib.urlencode(data)

url = 'http://twitter.com/statuses/update.xml'

opener = authenticate(user, password)
result = ''
try:
handle = opener.open(url, data = data)
result = handle.read()
handle.close()
return
except Exception, detail:
print "Err ", detail

#program starts from here
main()


Hope you will find it useful. Let me know if you have any suggestion to make my code better. It will be appreciated.

Wednesday, November 11, 2009

Python UNICODE encode / decode error

Today I was trying to scrape a Spanish site and got into trouble with some Spanish characters. I had to parse some messages from that Spanish website and post into twitter using my Python script. But for some reasons Spanish characters didn't show up in twitter status updates.

I was in some trouble with the following error messages:
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 5778-5781: invalid data
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 5778: ordinal not in range(128)

Then I did Googling for some time and got this very useful link.

I used this code to get the content of the website:

import codecs
import urllib2

url = '' # put the URL here
usock = urllib2.urlopen(url)
Reader = codecs.getreader("latin_1")
fh = Reader(usock)
data = fh.read()
fh.close()
usock.close()
data = data.encode("latin_1")


Though I first used utf-8 encoding rather than latin_1, but when I got this error: "UnicodeDecodeError: 'utf8' codec can't decode bytes in position 5778-5781: invalid data" I found that the website is using latin_1 character set (from html source) not utf-8.

Don't forget to check the codecs module. Btw, I am using Python 2.5.2. :)

Saturday, October 3, 2009

Python programmer in Malaysia

One of my friend is looking for python developers located in Malaysia. If you are interested please contact him: yfaizal at gmail.com

Thanks.

Friday, August 21, 2009

Google friend connect in Love Python

I have added Google friend connect widget in this blog so the followers can socialize with each other. You can also do it using the 'follower' gadget. You can read about Google friend connect here: http://www.google.com/intl/en/press/annc/20080512_friend_connect.html

Saturday, August 1, 2009

randomize elements in a file or list using Python

Last week I had to populate a mysql table with some random user names. First problem was to get those names - which I managed writing a crawler with Python. Using the program I got a text file with around 500 names listed in alphabetical order. But I didn't want to insert those in the same order as it will look odd (you don't want to get a list of opponent player's name all starting with 'A' in a game, right?). Then I decided to write some python code to generate another file with names in random order. The logic will be simple. First load all the names in a list, and from that list get a random element and put in another list. Delete the element from the initial list. Before I started coding, I googled for a while and found an interesting solution!


import random
...
count = names.__len__()
# names is the list containing names in alphabetical order
names = random.sample(names, count)
# now names contain names in random order!
...


Another Python magic!

Monday, July 6, 2009

Should a Python programmer be paid higher than a PHP / Perl / Java programmer ?

This question might sound stupid, but I have been wondering about this. Say I hire two developers with similar experience and expertise in my company. One is for a Python project and another one is for Java project. Should they get same compensation? A couple of years back, when I heard about Python from one of my senior friend (who is a CTO of a software firm, a very experienced guy), he told me that he likes Python because, development speed is lot faster in this language. He, from his experience believes that if a project takes six months to be completed using Java, four months should be good enough if it's done using Python. He was also planning for a project where he choose Python, and his logic was, he will use four Python developers for the project and if he choose Java instead of Python, it would take 10 developers to complete the project (same time frame). Then I said to him, "so, are you going to pay higher to the Python devs?" ... he just smiled :) ...

So folks, what do you think? Should the Python devs get more? Share your thoughts.

Sunday, June 21, 2009

Python Job - Web Development

Python is slowly getting popular in Bangladesh. Today I have browsed a job site and found one job advertisement for Python programmer. A company named Roots Information Technology is looking for four Python developers. Click here for the job details.

Hopefully we will find more companies looking for Python programmers in Bangladesh in neat future.