errorsandexceptions

Code, etc.

Category Archives: Python

Quicksort and Mergesort in Python

I wrote these small implementations of the quicksort and mergesort algorithms in Python. They take a list of numbers and return it sorted from lowest to highest. The WordPress code tags behave very strangely.

Here is Quicksort:

# Sorting a List using Quicksort in Python
# 2011-05-16

def quickSort(toSort):
	if len(toSort) <= 1:
		return toSort

	end = len(toSort) - 1
	pivot = toSort[end]

	low = []
	high = []

	for num in toSort[:end]:
		if num <= pivot:
			low.append(num)
		else:
			high.append(num)

	sortedList = quickSort(low)
	sortedList.append(pivot)
	sortedList.extend(quickSort(high))

	return sortedList

def main():
	l = [1, 6, 7, 2, 76, 45, 23, 4, 8, 12, 11]
	sortedList = quickSort(l)
	print sortedList

if __name__ == '__main__':
	main()

And Mergesort:

# Sorting a List using Mergesort in Python
# 2011-05-16

def mergeSort(toSort):
	if len(toSort) <= 1:
		return toSort

	mIndex = len(toSort) / 2
	left = mergeSort(toSort[:mIndex])
	right = mergeSort(toSort[mIndex:])

	result = []
	while len(left) > 0 and len(right) > 0:
		if left[0] > right[0]:	
			result.append(right.pop(0))
		else:
			result.append(left.pop(0))

	if len(left) > 0:
		result.extend(mergeSort(left))
	else:
		result.extend(mergeSort(right))

	return result

def main():
	l = [1, 6, 7, 2, 76, 45, 23, 4, 8, 12, 11]
	sortedList = mergeSort(l)
	print sortedList

if __name__ == '__main__':
	main()

Using OAuth with Python Twitter Tools

I have been writing a Twitter client in Python based on Mike Verdone’s Python Twitter Tools. It’s an elegant and concise library that allows access to all the useful parts of the Twitter API from inside a Python application. Unfortunately, the documentation is bad to non-existent.

After much fiddling I figured out how to use it for OAuth authentication. I’m sharing it here to save the next person the trouble. The first step if of course to import the twitter module. I like to import the oauth_dance portion of it too. This is needed to get the token key and token secret that give the application access to your Twitter account.

import twitter

import twitter.oauth_dance

You will need the consumer key and consumer secret, which you can get by registering your application with Twitter. Once you have done that just stick them in variables. As we don’t yet have the token key or secret, we pass empty strings for them. We then use parse_oauth_tokens in oauth_dance to get the token key and token secret and assign them to variables.

app_name = "appnamehere"

key = "blahblahblahblah"

secret = "putrealdatahere"

twitterConnect = twitter.Twitter(auth=twitter.oauth.OAuth('', '', key, secret), format='')

oauth_token, oauth_token_secret = twitter.oauth_dance.parse_oauth_tokens(twitterConnect.oauth.request_token())

Now we open a web browser and point it at an URL based on the value in oauth_token. The user is asked to enter their login details and confirm that they want to allow the application to access their Twitter account. Then the user is given a PIN that we use in the next stage to complete the OAuth process.

import webbrowser

oauth_url = ('http://api.twitter.com/oauth/authorize?oauth_token=' + oauth_token)

webbrowser.open(self.oauth_url)

Because my application is based on Python and Qt, I get the PIN from a text field in a dialog box. It is an instance of Qt’s QLineEdit. It doesn’t matter how you do it as long as you get the PIN into a string that can be passed to the twitterConnect object.

oauth_verifier = lineEdit.text()

twitterConnect = twitter.Twitter(auth=twitter.oauth.OAuth(oauth_token, oauth_token_secret, key, secret), format='')

oauth_token, oauth_token_secret = twitter.oauth_dance.parse_oauth_tokens(twitterConnect.oauth.access_token(oauth_verifier=oauth_verifier))

That completes the process. Now you can write the OAuth token key and token secret to a configuration file and use them for authentication in the future.