web analytics
Skip to main content
elephant_jokes

Learning recursion through elephant jokes

(Note: This post could also be titled “Telling elephant jokes with python”)

When teaching my students how to understand recursion, I always refer to the classic elephant joke: “how do you kill a blue elephant? With a blue elephant gun”.

They look amazed at first, until they notice that the joke is indeed a recursion, since, in order to kill an elephant with a certain colour, you should make it change the colour, then try to kill it again, until it turns blue, and the recursion ends.

So, I just implemented a little python program, with an “Elephant” class and a “kill” recursive method, in order to better demonstrate how recursion works. Have fun:

class Elephant():
	def __init__(self, colour):
		self.colour = colour
		
	def change_colour(self, new_colour):
		self.colour = new_colour
		print "Elephant's colour turns to " + self.colour
		
	def hold_nose(self):
		print "Hold elephant's nose..."
		self.change_colour('blue')
		
	def tickle(self):
		print "Tickle the elephant..."
		self.change_colour("pink")

	def strangle(self):
		print "Strangle the elephant..."
		self.change_colour("red")
	
	def kill(self):
		if self.colour == 'blue':
			print "Kill the elephant with a blue elephant gun!"
		elif self.colour == 'pink':
			self.hold_nose()
			self.kill()
		elif self.colour == 'red':
			self.tickle()
			self.kill()
		elif self.colour == 'white':
			self.strangle()
			self.kill()
		else:
			print "There aren't " + self.colour + " elephants!"

e = Elephant('blue')
print "How to kill a " + e.colour + " elephant?"			
e.kill()
print ""

e = Elephant('pink')
print "How to kill a " + e.colour + " elephant?"			
e.kill()
print ""

e = Elephant('red')
print "How to kill a " + e.colour + " elephant?"			
e.kill()
print ""

e = Elephant('white')
print "How to kill a " + e.colour + " elephant?"			
e.kill()
print ""

e = Elephant('yellow')
print "How to kill a " + e.colour + " elephant?"			
e.kill()
print ""

Here you have the program results:

How to kill a blue elephant?
Kill the elephant with a blue elephant gun!

How to kill a pink elephant?
Hold elephant’s nose…
Elephant’s colour turns to blue
Kill the elephant with a blue elephant gun!

How to kill a red elephant?
Tickle the elephant…
Elephant’s colour turns to pink
Hold elephant’s nose…
Elephant’s colour turns to blue
Kill the elephant with a blue elephant gun!

How to kill a white elephant?
Strangle the elephant…
Elephant’s colour turns to red
Tickle the elephant…
Elephant’s colour turns to pink
Hold elephant’s nose…
Elephant’s colour turns to blue
Kill the elephant with a blue elephant gun!

How to kill a yellow elephant?
There aren’t yellow elephants!

Django Python

How to generate a Django password

OK, this is for anyone who needs to manually generate a Django password. I was faced with this problem because I needed to manually insert a new Django user in the database. So, how to take the desired plain text password and to convert it properly so that you can insert it in the “password” field of the auth_user table in the Django database?

To answer this question, I had to take a look at the way Django handles user passwords. Basically, I needed to look at the following Python files from Django repository: utils.py, utils/encoding.py, utils/functional.py.

I’ve grabbed just the functions that I needed and put them together in the following piece of code that you can use.

(more…)

How to check if a process is running using python

A quick Python function to see if a given process is currently running. The function accepts a string as a parameter, and checks if there is any running process that contains that string. The check is made against the FULL process name, i.e., its name and its parameters, i.e.

Thus, it is very flexible, since you can actually pass any python regular express as a parameter, and thus check if there are any processes that match that expression.

Note: I’ve used the “w” ps option so that the ps command lists the wide view, in case the process name+process parameters is too large.

import re
import subprocess

def is_running(process):

s = subprocess.Popen([“ps”, “axw”],stdout=subprocess.PIPE)
for x in s.stdout:

if re.search(process, x):

return True

return False