Vim: Spaces instead of tab characters.

Indentation is important in Python code. Period. (This is really a big, emphatic, bright period.)

Python programmers who move their code from machine to machine prefer to use “spaces as tab characters” over “normal tab characters” for indentation in their code. There is a good reason why they do that.

If you code in Vim, like I do, you might have noticed that the default behaviour of the tab character is to insert as many tab characters, when you press the tab key, as are defined in the .vimrc file. If you want to change Vim to insert spaces instead, add the following into your .vimrc file (I am assuming a tab width of 8 characters):


set tabstop=8
set shiftwidth=8
set expandtab

I picked this up from here.

I hate what I hate. (Random Musings #20)

If there is something I have come to hate more than most of the things I hate, it is being friends with people who fall down quickly to grab your feet when they are in need, convince and assure you that if you need anything, you need only ask of them, and turn colours faster than a chameleon when you try to approach them when you are actually in need of something they can help you with (however small the need be).

I hate it. I hate it. I hate it like some comparison of hatred that I can’t think of and that has no parallel (this makes it contradictory, but what the hell).

Python, and the reacquaintance.

Oh, yeah, this is a small Python script to grab comments from the source of a webpage.

from sgmllib import SGMLParser
class CommentParser(SGMLParser):
	"Extract comments from webpage."
	def reset(self):
		self.comments = []
		SGMLParser.reset(self)
		
	def handle_comment(self, text):
		"""Push comments into the comments list."""
		self.comments.append("%(text)s" % locals())
	
	def display(self):
		"""Returned parsed comments as raw text."""
		return "".join(self.comments)
	
if __name__ == "__main__":
	import urllib
	import sys
	
	try: 
		host = sys.argv[1]
	except IndexError:
		host = 'http://ayaz.pk/'
		
	try:
		w = urllib.urlopen(host)
	except IOError:
		print "The webapge '%s' cannot be retrieved." % host
		sys.exit
	else:
		comment_parser = CommentParser()
		comment_parser.feed(w.read())
		w.close()
		comment_parser.close()
		
		print comment_parser.display()
		
		sys.exit	

Python script to grab (titles, links) from wordpress feeds.

Sometime last to last year I picked up Python. I studied it for a day, and feeling overwhelmed by my love for Perl, I ditched it the next day. I won’t tell you why (yeah, it’s a secret, unless someone comes along merrily who knows the secret and comments about it), but I picked up Dive Into Python, admittedly a great book to learn Python, last week. I’ve been reading it like anything ever since. And, although still nothing more than self-proclaiming, I can safely claim I know a lot of Python (I swear, for a moment there, I was typing Perl) now. And that is a good thing.

Enough of beating around the bush. I wrote a small python script, using xml.dom.minidom, to grab titles and links of posts from wordpress blog feeds. The script is available here. It can be used either as a standalone script or imported as a module from, say, the Python interactive shell. A brief example demonstrating its use:

>>> import gettitlesfromwordpress
>>> a = gettitlesfromwordpress.get_title_link(‘https://ayaz.wordpress.com/feed/’)
KDevelop
<https://ayaz.wordpress.com/2006/12/30/kdevelop/&gt;
….

The “get_title_link()” module can take anything for a URL, a wordpress feed file on hard-disk, to a wordpress feed as a string.