Posts

getch() in Python - get a single character from keyboard

Today I needed to write a Python code for 'Press any key to continue' feature. First I wrote the code: raw_input("Press Any Key to Continue: ") But actually it's not any key, you have to press enter followed by any key (or just enter). Then I started searching Google, came across several links, tried some of those and finally got something which is very close to what I need and modified the code to fit into my need. Here is the link that was almost exactly what I needed: http://pyfaq.infogami.com/how-do-i-get-a-single-keypress-at-a-time Here is my getch function in Python: import sys import termios import fcntl def myGetch(): fd = sys.stdin.fileno() oldterm = termios.tcgetattr(fd) newattr = termios.tcgetattr(fd) newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO termios.tcsetattr(fd, termios.TCSANOW, newattr) oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) try: ...

How to define python source code encoding?

While using non-ASCII characters in your code, you might get this error: "SyntaxError: Non-ASCII character '\xc3' in file myprog.py on line 101, but no encoding declared;". Solution is to define encoding at the beginning of your source code. Syntax: # -*- coding: desired_encoding -*- Example: # -*- coding: iso-8859-1 -*- For details check http://www.python.org/dev/peps/pep-0263/ .

Python Console in gedit

Being an Ubuntu user gedit is my default editor for programming (sometimes I use netbeans though). Today I found a post where the author wrote about gedit plugins . I just played with it a bit and found that it has a Python Console! This is really interesting. You can also try installing the plugin: sudo apt-get install gedit-plugins Now go to: edit > preferences > plugins and select which plugins you want and enable the side pane & bottom pane from the view menue.

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, da...

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 t...

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.

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