Posts

Showing posts from September, 2010

Python code to generate dates in a range

Today I needed to write a Python script where a part of it was to generate all dates in a range. I am sharing a simplified version of my code. import datetime def generate_dates(start_date, end_date): td = datetime.timedelta(hours=24) current_date = start_date while current_date <= end_date: print current_date current_date += td start_date = datetime.date(2010, 1, 25) end_date = datetime.date(2010, 3, 5) generate_dates(start_date, end_date) As always, please share if you know other methods that serve the purpose.

Insert an item to a tuple in Python

Tuples, like strings, are immutable: it is not possible to assign to the individual items of a tuple ... [from python doc]. Say you create a tuple t: >>> t = 1, 2, 3 >>> t (1, 2, 3) >>> Now you need to assign value 4 to the 4th element of the tuple: >>> t[3] = 4 Traceback (most recent call last): File " ", line 1, in TypeError: 'tuple' object does not support item assignment Now you try append like a list: >>> t.append(4) Traceback (most recent call last): File " ", line 1, in AttributeError: 'tuple' object has no attribute 'append' >>> No luck yet. So better covert the tuple to a list: >>> l = list(t) >>> l [1, 2, 3] >>> l.append(4) >>> l [1, 2, 3, 4] >>> And now convert the list to a tuple: >>> t = tuple(l) >>> t (1, 2, 3, 4) >>> Do you know of more ways to do this?

Create child process in Python - Subprocess module

Recently in one of my projects I had to create subprocess (child process) in Python. Actually I had to run multiple instances and didn't want to wait till those processes finish their job. I used the subprocess module to create those process. I am not sharing my actual code rather giving some sample code here that I wrote to understand the use of subprocess module myself which show you how to create child process (for which parent process need not wait for all the child processes to finish execution) using subprocess module. 1. create a file named test.py #!/usr/bin/env python import os, subprocess print "hello" urlList = ["http://python.org", "http://muktosoft.com", "http://bdosn.org", "http://matholympiad.org.bd", "http://bbc.co.uk", "http://cricinfo.com", "http://bdpy.org"] for url in urlList: subprocess.Popen(["python", "test2.py", url]) print "good bye\n" 2. now c

python code to retrive links from web page

There are several ways to extract / retrieve links (URL) from web page using Python. Let me discuss few ways. 1. Using Beautiful Soup. You can find code to retrieve links from web page using beautiful soup in it's documentation . The code is simple: import BeautifulSoup import urllib2 print "Enter the URL: " url = raw_input("> ") usock = urllib2.urlopen(url) soup = BeautifulSoup.BeautifulSoup(html_source) links = BeautifulSoup.SoupStrainer('a') for link in BeautifulSoup.BeautifulSoup(response, parseOnlyThese=links): if link.has_key('href'): print link['href'] 2. I saw another code in the book dive into python that uses a html parser to extract url. from sgmllib import SGMLParser class URLLister(SGMLParser): def reset(self): SGMLParser.reset(self) self.urls = [] def start_a(self, attrs): href = [v for k, v in attrs if k=='href'] if href: self.urls.extend(href) if __name__ == "__main__